Project

General

Profile

Actions

Feature #15560

closed

Add support for read/write offsets.

Added by ioquatix (Samuel Williams) almost 6 years ago. Updated about 2 years ago.

Status:
Closed
Target version:
-
[ruby-core:91235]

Description

It would be nice if read/write/send/recv/etc methods could accept an offset argument.

e.g.

socket = Socket.new(...)
buffer = String.b

socket.read(1024, buffer)
socket.read(1024, buffer, offset: buffer.bytesize)

The same for write, e.g.

socket = Socket.new(...)
buffer = String.b

amount = socket.write(buffer)
socket.write(buffer, offset: amount)

Could also include "size:" so that we can selectively write parts of the buffer.

Updated by shyouhei (Shyouhei Urabe) almost 6 years ago

I can imagine it is useful for TCPSocket#write to accept only a part of a buffer crafted by somebody else. But I cannot think of any actual use case when #read is useful with that extension. Do you have one?

Updated by ioquatix (Samuel Williams) almost 6 years ago

It is useful for read when user wants to read some data, and it wasn't enough (e.g. implementing gets) so you need to read some more at the end of what you already have.

Updated by ioquatix (Samuel Williams) almost 6 years ago

Also, rather than using a keyword argument, it could be a 3rd argument after buffer, since buffer must be supplied in order for this to work.

Actions #4

Updated by ioquatix (Samuel Williams) almost 6 years ago

  • Target version deleted (2.7)

Updated by ioquatix (Samuel Williams) about 2 years ago

I've added this to the IO::Buffer interface which is good enough for my requirements.

buffer = IO::Buffer.new
buffer.read(io, length, offset)
buffer.write(io, length, offset)

If we want to introduce the same model to IO.read(length, [buffer, [offset]]) and IO.write(length, [buffer, [offset]]) that would be reasonable.

Updated by ioquatix (Samuel Williams) about 2 years ago

  • Status changed from Open to Closed
  • Assignee set to ioquatix (Samuel Williams)

I tried making a PR, but I think we should give up.

IO#read(length, buffer, offset) can work okay.

But doing the same for write is very hard because the interface is more general - it tries to write all arguments. It would need to be a keyword argument, and I'm not sure how you'd make it work since you can specify multiple arguments.

I think the solution here, for more complex use cases, is just to use IO::Buffer which supports this very naturally.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0