Replies: 3 comments 6 replies
-
Could you go into more detail on this? The intention behind |
Beta Was this translation helpful? Give feedback.
-
I think we need to narrow on this before discussing
|
Beta Was this translation helpful? Give feedback.
-
Unfortunately, the pessimistic processing would seem to be necessary in order to support providing row/column information when reading from a sliding buffer over an input stream. The reader may not be able to re-visit the bytes in question. (To be clear: the ask here is not that
Agreed, the current implementation is a hacky workaround. It functions in the narrow use cases we've tested, but it's obviously fragile and does not follow the intended design. We're hoping to find an alternative solution. Regarding fn parse_next(&mut self, input: &mut I) -> Result<<I as Stream>::Slice, E>
// Save a copy of `input` (For us, `Self::Checkpoint` is `Self`)
let checkpoint = input.checkpoint();
// Run the parser.
match (self.parser).parse_next(input) {
Ok(_) => {
// At this point `input` has been modified.
// Its inner `offset`, `row`, and `column` fields have been updated.
// Take note of the change in 'offset'.
let offset = input.offset_from(&checkpoint);
// Discard all of the other information, including the new `row` and `column`.
input.reset(&checkpoint);
// Get a slice by applying the change in offset.
let taken = input.next_slice(offset);
// At this point, `taken` is correct, but important changes to `input` were lost.
Ok(taken)
}
Err(e) => Err(e),
}
} In our case, Conceptually, we would need something like: fn parse_next(&mut self, input: &mut I) -> Result<<I as Stream>::Slice, E>
let checkpoint_before = input.checkpoint();
match (self.parser).parse_next(input) {
Ok(_) => {
let checkpoint_after = input.checkpoint();
let offset = input.offset_from(&checkpoint_before);
input.reset(&checkpoint_before);
let taken = input.next_slice(offset);
input.reset(&checkpoint_after);
Ok(taken)
}
Err(e) => Err(e),
}
} Since this adds a second |
Beta Was this translation helpful? Give feedback.
-
I'm working on a custom implementation of
Stream
where bothSlice
andCheckpoint
areSelf
. I'm trying to add row and column count details to this buffer, with specific requirements that the currentLocation
doesn't address.While implementing this, I noticed that the
parse_next
function inimpls.rs
(line 399) stores the checkpoint and then resets and modifies the offset usingnext_slice
. I'd like to do something similar for location data (rows and columns).Currently, I've had to modify the
reset
function to preserve my custom location information:While this works for now, I'm concerned about potential issues in the future, given that
reset
is used in many places throughout winnow.I'm wondering if it would be possible to modify
next_slice
to accept a context orSlice
parameter, allowing me to pass in my row and column information there. This would eliminate the need to depend onreset
for maintaining this custom state. The signature might look something like:This change would provide more flexibility for implementations that need to track custom state beyond just the offset.
I'd appreciate your thoughts on this proposal and whether it aligns with the future direction of the winnow crate. Also, If you any other ideas on how I should do it that is helpful as well.
Beta Was this translation helpful? Give feedback.
All reactions