Skip to content

Commit 2eb1ddb

Browse files
committed
Add missing documentation.
1 parent 75b905c commit 2eb1ddb

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

lib/io/stream.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
class IO
1212
# @namespace
1313
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.
1419
def self.Duplex(input, output = input, **options)
1520
Buffered.wrap(Duplex.new(input, output), **options)
1621
end

lib/io/stream/duplex.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
module IO::Stream
77
# A low-level duplex IO adapter that composes distinct readable and writable endpoints.
88
class Duplex
9+
# Initialize a duplex transport from separate readable and writable endpoints.
10+
# @parameter input [IO] The readable endpoint.
11+
# @parameter output [IO] The writable endpoint.
912
def initialize(input, output = input)
1013
@input = input
1114
@output = output
@@ -14,23 +17,32 @@ def initialize(input, output = input)
1417
attr :input
1518
attr :output
1619

20+
# Return the underlying IO used to represent this duplex stream.
21+
# @returns [IO] The readable endpoint if available, otherwise the writable endpoint.
1722
def to_io
1823
@input || @output
1924
end
2025

26+
# Return the maximum timeout across both endpoints.
27+
# @returns [Numeric | Nil] The effective timeout, or `nil` if no timeout is configured.
2128
def timeout
2229
[@input.timeout, @output.timeout].compact.max
2330
end
2431

32+
# Update the timeout on both endpoints.
33+
# @parameter duration [Numeric | Nil] The timeout to assign.
2534
def timeout=(duration)
2635
@input.timeout = duration
2736
@output.timeout = duration
2837
end
2938

39+
# Check whether both endpoints are closed.
40+
# @returns [Boolean] True if the duplex stream can no longer read or write.
3041
def closed?
3142
@input.closed? && @output.closed?
3243
end
3344

45+
# Close the readable endpoint.
3446
def close_read
3547
return if @input.closed?
3648

@@ -41,6 +53,7 @@ def close_read
4153
end
4254
end
4355

56+
# Close the writable endpoint.
4457
def close_write
4558
return if @output.closed?
4659

@@ -51,27 +64,44 @@ def close_write
5164
end
5265
end
5366

67+
# Check whether the readable endpoint may still produce data.
68+
# @returns [Boolean] True if the readable endpoint reports it is readable.
5469
def readable?
5570
@input.readable?
5671
end
5772

73+
# Close both endpoints.
5874
def close
5975
@output.close unless @output.closed?
6076
@input.close unless @input.closed?
6177
end
6278

79+
# Write data to the writable endpoint.
80+
# @parameter buffer [String] The data to write.
81+
# @returns [Integer] The number of bytes written.
6382
def write(buffer)
6483
@output.write(buffer)
6584
end
6685

86+
# Read data from the readable endpoint without blocking.
87+
# @parameter size [Integer] The maximum number of bytes to read.
88+
# @parameter buffer [String] The destination buffer.
89+
# @parameter exception [Boolean] Whether to raise on `:wait_readable` and EOF conditions.
90+
# @returns [String | Symbol | Nil] Data read from the endpoint, or the underlying non-blocking result.
6791
def read_nonblock(size, buffer, exception: false)
6892
@input.read_nonblock(size, buffer, exception: exception)
6993
end
7094

95+
# Wait until the readable endpoint can be read.
96+
# @parameter duration [Numeric | Nil] The maximum time to wait.
97+
# @returns [Boolean] True if the endpoint became readable.
7198
def wait_readable(duration = @timeout)
7299
@input.wait_readable(duration)
73100
end
74101

102+
# Wait until the writable endpoint can be written.
103+
# @parameter duration [Numeric | Nil] The maximum time to wait.
104+
# @returns [Boolean] True if the endpoint became writable.
75105
def wait_writable(duration = @timeout)
76106
@output.wait_writable(duration)
77107
end

lib/io/stream/shim/timeout.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77

88
class StringIO
99
unless method_defined?(:timeout)
10+
# Return the configured timeout for this in-memory stream.
11+
# @returns [Numeric | Nil] The configured timeout, if any.
1012
def timeout
1113
@timeout
1214
end
1315
end
1416

1517
unless method_defined?(:timeout=)
18+
# Store timeout state for compatibility with IO-like timeout interfaces.
19+
# @parameter duration [Numeric | Nil] The timeout to assign.
20+
# @returns [Numeric | Nil] The assigned timeout.
1621
def timeout=(duration)
1722
@timeout = duration
1823
end

0 commit comments

Comments
 (0)