-
Notifications
You must be signed in to change notification settings - Fork 39
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
Add SeekFrom::Start, SeekFrom::Current, and nested support to take_seek #292
base: master
Are you sure you want to change the base?
Conversation
This change adds support for the missing SeekFrom operations. It refactors the internals a bit. We now keep a range of start..end values that are accessible to the TakeSeek. I dropped manually computed stream position and instead call the inner's `stream_position()`. This does mean that `limit` needs to take in a `&mut`. I was also going to change the return type to `Result<u64>`, but that would force all callers to handle the `Result`, which I felt like it was a bigger API change. I fixed the test added in 38b3598 because `StartFrom::Start(0)` used to reset the cursor to the start of the underlying buffer. Fixes jam1garner#291.
Hi, thanks for your work on this! If I understand these changes, this patch looks to make In particular, In any case, the solution for this one problem with the patch is pretty straightforward since My experience with sub-streams where the origin matters has always been in the context of reading something like an offset table, so I would probably recommend adding a second convenience function that takes a Let me know your thoughts. Thanks! |
Take the following example: #[test]
fn test_stream_len() {
let mut buf = [0; 8];
let mut data = Cursor::new("\x00\x01\x02\x03\x04\x05\x06\x07\x08");
data.seek(SeekFrom::Start(1)).unwrap();
let mut section = data.take_seek(4);
assert_eq!(section.stream_len().unwrap(), 4); // <--- Performs X = Current(0), End(0), Start(X).
assert_eq!(inner_section.read(&mut buf).unwrap(), 4);
assert_eq!(&buf, b"\x01\x02\x03\x04\x00\x00\x00\x00");
} Before the fix in 38b3598, the test would fail because the returned I think if you want to combine Take + Seek, then we need to handle the different
Unfortunately it stems from rust-lang/rust#59359.
In my case, I'm reading a GPT partition table, and I want to parse the contents in each partition. My partition parsers need to seek within the partition using
I think that's a good idea. Maybe Thanks for the review! |
Hi,
I’m trying not to be pedantically irritating here, so apologies in advance if it comes across that way, it is not my intent. A user can call After writing this, I realise that the fix that I committed is wrong, since the end of
In the same way that offsets recorded within a file may be thought of as absolute or relative, I’m not sure there is any definitively correct non-confusing choice. It would also be confusing to see some error about failing to parse at byte 64, when binrw is actually trying to parse byte 60064 of the original stream, just because the user was trying to limit how many bytes to read.
At the risk of going on ad nauseam, the design intent of Given the problems with conflicts in the Hopefully this makes sense, but please let me know if there is some other thing I am not thinking about.
Sure, something with a Thanks, |
This change adds support for the missing SeekFrom operations. It
refactors the internals a bit. We now keep a range of start..end values
that are accessible to the TakeSeek. I dropped manually computed stream
position and instead call the inner's
stream_position()
. This doesmean that
limit
needs to take in a&mut
. I was also going to changethe return type to
Result<u64>
, but that would force all callers tohandle the
Result
, which I felt like it was a bigger API change.I fixed the test added in 38b3598
because
StartFrom::Start(0)
used to reset the cursor to the start ofthe underlying buffer.
Fixes #291.