Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC] Document new methods of IO::Buffer and Fiber::Scheduler #7016

Merged
merged 5 commits into from Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 69 additions & 10 deletions io_buffer.c
Expand Up @@ -2413,10 +2413,10 @@ rb_io_buffer_read(VALUE self, VALUE io, size_t length, size_t offset)
}

/*
* call-seq: read(io, [length, [offset]]) -> self
* call-seq: read(io, [length, [offset]]) -> read length or -errno
*
* Read at most +length+ bytes from +io+ into the buffer, starting at
* +offset+.
* +offset+. If an error occurs, return <tt>-errno</tt>.
*
* If +length+ is not given, read until the end of the buffer.
*
Expand All @@ -2426,14 +2426,17 @@ rb_io_buffer_read(VALUE self, VALUE io, size_t length, size_t offset)
*
* Example:
*
* buffer = IO::Buffer.for('test')
* # =>
* # <IO::Buffer 0x00007fca40087c38+4 SLICE>
* # 0x00000000 74 65 73 74 test
* buffer.read(File.open('/dev/urandom', 'rb'), 4)
* # =>
* # <IO::Buffer 0x00007fca40087c38+4 SLICE>
* # 0x00000000 2a 0e 0e 0e *...
* IO::Buffer.for('test') do |buffer|
* p buffer
* # =>
* # <IO::Buffer 0x00007fca40087c38+4 SLICE>
* # 0x00000000 74 65 73 74 test
* buffer.read(File.open('/dev/urandom', 'rb'), 2)
* p buffer
* # =>
* # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE>
* # 0x00000000 05 35 73 74 .5st
* end
*/
static VALUE
io_buffer_read(int argc, VALUE *argv, VALUE self)
Expand Down Expand Up @@ -2528,6 +2531,32 @@ rb_io_buffer_pread(VALUE self, VALUE io, rb_off_t from, size_t length, size_t of
return rb_thread_io_blocking_region(io_buffer_pread_internal, &argument, descriptor);
}

/*
* call-seq: pread(io, from, length, [offset]) -> read length or -errno
*
* Read at most +length+ bytes from +io+ into the buffer, starting at
* +from+, and put it in buffer starting from specified +offset+.
* If an error occurs, return <tt>-errno</tt>.
*
* If +offset+ is not given, put it at the beginning of the buffer.
*
* Example:
*
* IO::Buffer.for('test') do |buffer|
* p buffer
* # =>
* # <IO::Buffer 0x00007fca40087c38+4 SLICE>
* # 0x00000000 74 65 73 74 test
*
* # take 2 bytes from the beginning of urandom,
* # put them in buffer starting from position 2
* buffer.pread(File.open('/dev/urandom', 'rb'), 0, 2, 2)
* p buffer
* # =>
* # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE>
* # 0x00000000 05 35 73 74 te.5
* end
*/
static VALUE
io_buffer_pread(int argc, VALUE *argv, VALUE self)
{
Expand Down Expand Up @@ -2602,6 +2631,20 @@ rb_io_buffer_write(VALUE self, VALUE io, size_t length, size_t offset)
return rb_thread_io_blocking_region(io_buffer_write_internal, &argument, descriptor);
}

/*
* call-seq: write(io, length, [offset]) -> written length or -errno
*
* Writes +length+ bytes from buffer into +io+, starting at
* +offset+ in the buffer. If an error occurs, return <tt>-errno</tt>.
*
* If +offset+ is not given, the bytes are taken from the beginning
* of the buffer.
*
* out = File.open('output.txt', 'wb')
* IO::Buffer.for('1234567').write(out, 3)
*
* This leads to +123+ being written into <tt>output.txt</tt>
*/
static VALUE
io_buffer_write(int argc, VALUE *argv, VALUE self)
{
Expand Down Expand Up @@ -2695,6 +2738,22 @@ rb_io_buffer_pwrite(VALUE self, VALUE io, rb_off_t from, size_t length, size_t o
return rb_thread_io_blocking_region(io_buffer_pwrite_internal, &argument, descriptor);
}

/*
* call-seq: pwrite(io, from, length, [offset]) -> written length or -errno
*
* Writes +length+ bytes from buffer into +io+, starting at
* +offset+ in the buffer. If an error occurs, return <tt>-errno</tt>.
*
* If +offset+ is not given, the bytes are taken from the beginning of the
* buffer. If the +offset+ is given and is beyond the end of the file, the
* gap will be filled with null (0 value) bytes.
*
* out = File.open('output.txt', File::RDWR) # open for read/write, no truncation
* IO::Buffer.for('1234567').pwrite(out, 2, 3, 1)
*
* This leads to +234+ (3 bytes, starting from position 1) being written into
* <tt>output.txt</tt>, starting from file position 2.
*/
static VALUE
io_buffer_pwrite(int argc, VALUE *argv, VALUE self)
{
Expand Down
18 changes: 18 additions & 0 deletions scheduler.c
Expand Up @@ -118,6 +118,9 @@ Init_Fiber_Scheduler(void)
rb_define_method(rb_cFiberScheduler, "io_wait", rb_fiber_scheduler_io_wait, 3);
rb_define_method(rb_cFiberScheduler, "io_read", rb_fiber_scheduler_io_read, 4);
rb_define_method(rb_cFiberScheduler, "io_write", rb_fiber_scheduler_io_write, 4);
rb_define_method(rb_cFiberScheduler, "io_pread", rb_fiber_scheduler_io_pread, 5);
rb_define_method(rb_cFiberScheduler, "io_pwrite", rb_fiber_scheduler_io_pwrite, 5);
rb_define_method(rb_cFiberScheduler, "io_select", rb_fiber_scheduler_io_select, 4);
rb_define_method(rb_cFiberScheduler, "kernel_sleep", rb_fiber_scheduler_kernel_sleep, 1);
rb_define_method(rb_cFiberScheduler, "address_resolve", rb_fiber_scheduler_address_resolve, 1);
rb_define_method(rb_cFiberScheduler, "timeout_after", rb_fiber_scheduler_timeout_after, 3);
Expand Down Expand Up @@ -490,6 +493,14 @@ rb_fiber_scheduler_io_read(VALUE scheduler, VALUE io, VALUE buffer, size_t lengt
return rb_check_funcall(scheduler, id_io_read, 4, arguments);
}


/*
* Document-method: Fiber::Scheduler#io_read
* call-seq: io_pread(io, buffer, from, length, offset) -> read length or -errno
*
* Invoked by IO::Buffer#pread. See that method for description of arguments.
*
*/
VALUE
rb_fiber_scheduler_io_pread(VALUE scheduler, VALUE io, rb_off_t from, VALUE buffer, size_t length, size_t offset)
{
Expand Down Expand Up @@ -537,6 +548,13 @@ rb_fiber_scheduler_io_write(VALUE scheduler, VALUE io, VALUE buffer, size_t leng
return rb_check_funcall(scheduler, id_io_write, 4, arguments);
}

/*
* Document-method: Fiber::Scheduler#io_pwrite
* call-seq: io_pwrite(io, buffer, from, length, offset) -> written length or -errno
*
* Invoked by IO::Buffer#pwrite. See that method for description of arguments.
*
*/
VALUE
rb_fiber_scheduler_io_pwrite(VALUE scheduler, VALUE io, rb_off_t from, VALUE buffer, size_t length, size_t offset)
{
Expand Down