positioned-io icon indicating copy to clipboard operation
positioned-io copied to clipboard

Add an easy API to get a slice from a Cursor

Open RReverser opened this issue 4 years ago • 7 comments

Right now, the only Slice constructors require passing in an explicit offset; however, when working with Cursor or SizeCursor, it would be more natural to be able to

  1. only pass a length you wish to read
  2. get a Slice of the given length back
  3. have the Cursor position auto-advanced

in a single call.

RReverser avatar Oct 23 '20 21:10 RReverser

As far as I'm concerned this is already done as the Cursor has Read implementation that does all that.

ruseinov avatar Aug 20 '23 11:08 ruseinov

Hm it's been 3 years since I submitted that issue, so I don't remember details, but I believe it was about zero-copied slices of the original file whereas standard Read trait only allows you to read (copy) bytes into a preexisting buffer.

RReverser avatar Aug 20 '23 18:08 RReverser

Hm it's been 3 years since I submitted that issue, so I don't remember details, but I believe it was about zero-copied slices of the original file whereas standard Read trait only allows you to read (copy) bytes into a preexisting buffer.

No idea, there's nothing in the description about zero-copy slices. We should fix the description then perhaps?

ruseinov avatar Aug 20 '23 19:08 ruseinov

Also, one of the strong points of ReadAt trait is the fact that reads don't require a mutable reference. That allows for parallel reads using the same handle. I'm not sure how zero-copy could be achieved here to be honest, as under the hood Cursor is using ReadAt trait to deal with the reads. So that would require a new api and a specific implementation for Slice. Which does not seem like what this library is trying to solve, as the whole idea of it is to be able to read the data again and again at a random offset, if I'm understanding it correctly.

ruseinov avatar Aug 20 '23 19:08 ruseinov

No idea, there's nothing in the description about zero-copy slices.

It does mention specifically the Slice type, which in this crate is a very specific type for view / window / zero-copy slice: https://docs.rs/positioned-io/latest/positioned_io/struct.Slice.html

RReverser avatar Aug 21 '23 01:08 RReverser

No idea, there's nothing in the description about zero-copy slices.

It does mention specifically the Slice type, which in this crate is a very specific type for view / window / zero-copy slice: https://docs.rs/positioned-io/latest/positioned_io/struct.Slice.html

As far as I can tell there is no difference between the Read implementation of Cursor and ReadAt implementation of Slice that is wrapped by a Cursor. It does exactly the same thing - calling Slice::read_at under the hood and advancing the position. What am I missing?

ruseinov avatar Aug 21 '23 08:08 ruseinov

Well... yes, they're the same, but I'm not sure why they're relevant to this issue - I didn't mention either Read or ReadAt in the issue description / feature request. I asked for specific behaviour of "get a Slice of the given length back", which neither of those traits currently give.

RReverser avatar Aug 21 '23 10:08 RReverser