Skip to content

Commit e9ca4bd

Browse files
Support Duplex(io).
1 parent 5057c06 commit e9ca4bd

5 files changed

Lines changed: 28 additions & 14 deletions

File tree

lib/io/stream.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@
99

1010
# @namespace
1111
class IO
12-
# @namespace
13-
module Stream
14-
# Construct a buffered duplex stream from separate input and output endpoints.
15-
# @parameter input [IO] The readable endpoint.
16-
# @parameter output [IO] The writable endpoint.
17-
# @parameter options [Hash] Additional options passed to the buffered stream wrapper.
18-
# @returns [IO::Stream::Buffered] A buffered stream wrapping a duplex transport.
19-
def self.Duplex(input, output = input, **options)
20-
Buffered.wrap(Duplex.new(input, output), **options)
21-
end
22-
end
23-
2412
# Convert any IO-like object into a buffered stream.
2513
# @parameter io [IO] The IO object to wrap.
2614
# @returns [IO::Stream::Buffered] A buffered stream wrapper.

lib/io/stream/duplex.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,17 @@ def wait_writable(duration = @timeout)
106106
@output.wait_writable(duration)
107107
end
108108
end
109+
110+
# Construct a buffered stream from either one duplex IO-like object or two separate endpoints.
111+
# @parameter input [IO] The duplex IO object, or the readable endpoint.
112+
# @parameter output [IO | Nil] The writable endpoint, when distinct from the readable endpoint.
113+
# @parameter options [Hash] Additional options passed to the buffered stream wrapper.
114+
# @returns [IO::Stream::Buffered] A buffered stream wrapping the supplied transport.
115+
def self.Duplex(input, output = nil, **options)
116+
if output
117+
Buffered.wrap(Duplex.new(input, output), **options)
118+
else
119+
::IO.Stream(input)
120+
end
121+
end
109122
end

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- `IO::Stream::Duplex(io)` is equivalent to `IO::Stream(io)`.
6+
37
## v0.12.0
48

59
- Introduce `IO::Stream::Duplex` as a low-level duplex transport for composing separate input and output endpoints.

test/io/stream/buffered.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,8 +1073,8 @@ def after(error = nil)
10731073
server.sync_close = true
10741074

10751075
[
1076-
Async{server.accept},
1077-
Async{client.connect}
1076+
Async {server.accept},
1077+
Async {client.connect}
10781078
].each(&:wait)
10791079

10801080
@client = IO::Stream::Buffered.wrap(client)

test/io/stream/duplex.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ def make_pipes
3535
end
3636

3737
with "::Duplex" do
38+
it "wraps a single duplex IO directly" do
39+
io = StringIO.new
40+
41+
stream = IO::Stream::Duplex(io)
42+
43+
expect(stream).to be_a(IO::Stream::Buffered)
44+
expect(stream.io).to be_equal(io)
45+
end
46+
3847
it "returns a buffered stream wrapping a duplex IO" do
3948
pipes = make_pipes
4049

0 commit comments

Comments
 (0)