-
-
Notifications
You must be signed in to change notification settings - Fork 233
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
Option for reading between two offsets #75
Conversation
Thank you very much for your contribution! From a first glance, the changes look great, but I didn't have the time for a full review yet. Could be a few more days. |
let mut discard = vec![0u8; offset as usize]; | ||
reader | ||
.read_exact(&mut discard) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, that seems a bit hacky, but I also couldn't find a better solution after a quick search.
I'm not sure if we should leave it like this. If somebody wants to skip the first few MB or even GB of a file/input, we would actually allocate a buffer of this size on the heap which could easily fail. Even if it doesn't fail, it seems wrong to allocate a huge buffer for simply discarding everything inside it.
If that helps, I think we could actually use the BufRead
trait instead of just Read
. We would simply need to create a BufReader
for the input file. The StdinLock
already implements BufRead
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we want is Seek
: https://doc.rust-lang.org/stable/std/io/struct.BufReader.html#impl-Seek
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a bit of research on this and Stdin doesn't appear to be seekable no matter what I do. I'll continue exploring other options for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe read to a 1024 bytes or so big slice multiple times as necessary and then read to a smaller slice as necessary.
I'm going to close this for now, with #88 merged. Happy to reopen it if there is any further comments/progress. |
I have tried to implement the syntax suggested in the discussion on issue #16.
I did not add support for negative offsets simply because I do not see a simple way to implement it for
Stdin
. If required, I suppose it could be implemented with a buffering solution that readsM - N
bytes into a buffer until it reaches the end ofStdin
before passing it to a reader.My solution differs from #43 by not modifying the Printer itself, merely the Reader that it receives. This means that it works just as well for
Stdin
as for a file. A consequence of this is that the bytes are no longer automatically "aligned" in the output, but that just might be what you want in this situation.This is my first contribution of Rust code and I welcome suggestions that can help me improve! :)