Skip to content

Commit e277937

Browse files
Fix buffered IO signatures for Ruby 4.1
Assisted-By: devx/ab4e61c0-6c66-4d82-b517-93ef8659abb1
1 parent 5b925fa commit e277937

2 files changed

Lines changed: 65 additions & 23 deletions

File tree

‎lib/async/scheduler.rb‎

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,35 @@ def io_wait(io, events, timeout = nil)
336336
end
337337

338338
if ::IO::Event::Support.buffer?
339-
# Read from the specified IO into the buffer.
340-
#
341-
# @public Since *Async v2* and Ruby with `IO::Buffer` support.
342-
# @asynchronous May be non-blocking.
343-
#
344-
# @parameter io [IO] The IO object to read from.
345-
# @parameter buffer [IO::Buffer] The buffer to read into.
346-
# @parameter length [Integer] The minimum number of bytes to read.
347-
# @parameter offset [Integer] The offset within the buffer to read into.
348-
def io_read(io, buffer, length, offset = 0)
339+
if defined?(IO::Buffer::VERSION) && IO::Buffer::VERSION >= 3
340+
# Read at most the specified number of bytes from the IO into the buffer.
341+
#
342+
# @public Since *Async v2* and Ruby with `IO::Buffer` support.
343+
# @asynchronous May be non-blocking.
344+
#
345+
# @parameter io [IO] The IO object to read from.
346+
# @parameter buffer [IO::Buffer] The buffer to read into.
347+
# @parameter offset [Integer] The offset within the buffer to read into.
348+
# @parameter length [Integer] The maximum number of bytes to read.
349+
def io_read(io, buffer, offset, length)
350+
io_read_with_timeout(io, buffer, offset, length)
351+
end
352+
else
353+
# Read from the specified IO into the buffer.
354+
#
355+
# @public Since *Async v2* and Ruby with `IO::Buffer` support.
356+
# @asynchronous May be non-blocking.
357+
#
358+
# @parameter io [IO] The IO object to read from.
359+
# @parameter buffer [IO::Buffer] The buffer to read into.
360+
# @parameter length [Integer] The minimum number of bytes to read.
361+
# @parameter offset [Integer] The offset within the buffer to read into.
362+
def io_read(io, buffer, length, offset = 0)
363+
io_read_with_timeout(io, buffer, length, offset)
364+
end
365+
end
366+
367+
private def io_read_with_timeout(io, buffer, first, second)
349368
fiber = Fiber.current
350369

351370
if timeout = io.timeout
@@ -354,22 +373,41 @@ def io_read(io, buffer, length, offset = 0)
354373
end
355374
end
356375

357-
@selector.io_read(fiber, io, buffer, length, offset)
376+
@selector.io_read(fiber, io, buffer, first, second)
358377
ensure
359378
timer&.cancel!
360379
end
361380

362381
if RUBY_ENGINE != "ruby" || RUBY_VERSION >= "3.3.1"
363-
# Write the specified buffer to the IO.
364-
#
365-
# @public Since *Async v2* and *Ruby v3.3.1* with `IO::Buffer` support.
366-
# @asynchronous May be non-blocking.
367-
#
368-
# @parameter io [IO] The IO object to write to.
369-
# @parameter buffer [IO::Buffer] The buffer to write from.
370-
# @parameter length [Integer] The minimum number of bytes to write.
371-
# @parameter offset [Integer] The offset within the buffer to write from.
372-
def io_write(io, buffer, length, offset = 0)
382+
if defined?(IO::Buffer::VERSION) && IO::Buffer::VERSION >= 3
383+
# Write at most the specified number of bytes from the buffer to the IO.
384+
#
385+
# @public Since *Async v2* and *Ruby v3.3.1* with `IO::Buffer` support.
386+
# @asynchronous May be non-blocking.
387+
#
388+
# @parameter io [IO] The IO object to write to.
389+
# @parameter buffer [IO::Buffer] The buffer to write from.
390+
# @parameter offset [Integer] The offset within the buffer to write from.
391+
# @parameter length [Integer] The maximum number of bytes to write.
392+
def io_write(io, buffer, offset, length)
393+
io_write_with_timeout(io, buffer, offset, length)
394+
end
395+
else
396+
# Write the specified buffer to the IO.
397+
#
398+
# @public Since *Async v2* and *Ruby v3.3.1* with `IO::Buffer` support.
399+
# @asynchronous May be non-blocking.
400+
#
401+
# @parameter io [IO] The IO object to write to.
402+
# @parameter buffer [IO::Buffer] The buffer to write from.
403+
# @parameter length [Integer] The minimum number of bytes to write.
404+
# @parameter offset [Integer] The offset within the buffer to write from.
405+
def io_write(io, buffer, length, offset = 0)
406+
io_write_with_timeout(io, buffer, length, offset)
407+
end
408+
end
409+
410+
private def io_write_with_timeout(io, buffer, first, second)
373411
fiber = Fiber.current
374412

375413
if timeout = io.timeout
@@ -378,7 +416,7 @@ def io_write(io, buffer, length, offset = 0)
378416
end
379417
end
380418

381-
@selector.io_write(fiber, io, buffer, length, offset)
419+
@selector.io_write(fiber, io, buffer, first, second)
382420
ensure
383421
timer&.cancel!
384422
end

‎test/async/scheduler.rb‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@
125125
scheduler.instance_variable_set(:@selector, selector)
126126

127127
expect do
128-
scheduler.io_read(io, IO::Buffer.new(1024), 1024)
128+
if defined?(IO::Buffer::VERSION) && IO::Buffer::VERSION >= 3
129+
scheduler.io_read(io, IO::Buffer.new(1024), 0, 1024)
130+
else
131+
scheduler.io_read(io, IO::Buffer.new(1024), 1024)
132+
end
129133
end.to raise_exception(::IO::TimeoutError)
130134
ensure
131135
scheduler&.close

0 commit comments

Comments
 (0)