File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99
1010# @namespace
1111class 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.
Original file line number Diff line number Diff 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
109122end
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments