Skip to content

Commit

Permalink
fix(stream)!: Dont return input on peek_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 29, 2025
1 parent 34bd53f commit 5a3541a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/stream/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ impl<'i> Stream for &'i BStr {
*self = BStr::from_bytes(next);
slice
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
let (slice, _next) = self.split_at(offset);
slice
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down
5 changes: 5 additions & 0 deletions src/stream/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ impl<'i> Stream for &'i Bytes {
*self = Bytes::from_bytes(next);
slice
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
let (slice, _next) = self.split_at(offset);
slice
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down
4 changes: 4 additions & 0 deletions src/stream/locating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ impl<I: Stream> Stream for LocatingSlice<I> {
fn next_slice(&mut self, offset: usize) -> Self::Slice {
self.input.next_slice(offset)
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
self.input.peek_slice(offset)
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down
30 changes: 20 additions & 10 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,7 @@ pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::D
///
fn next_slice(&mut self, offset: usize) -> Self::Slice;
/// Split off a slice of tokens from the input
#[inline(always)]
fn peek_slice(&self, offset: usize) -> (Self, Self::Slice)
where
Self: Clone,
{
let mut peek = self.clone();
let slice = peek.next_slice(offset);
(peek, slice)
}
fn peek_slice(&self, offset: usize) -> Self::Slice;

/// Advance to the end of the stream
#[inline(always)]
Expand All @@ -208,7 +200,7 @@ pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::D
where
Self: Clone,
{
self.peek_slice(self.eof_offset()).1
self.peek_slice(self.eof_offset())
}

/// Save the current parse location within the stream
Expand Down Expand Up @@ -281,6 +273,11 @@ where
*self = next;
slice
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
let (slice, _next) = self.split_at(offset);
slice
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down Expand Up @@ -361,6 +358,11 @@ impl<'i> Stream for &'i str {
*self = next;
slice
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
let (slice, _next) = self.split_at(offset);
slice
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down Expand Up @@ -443,6 +445,14 @@ where
self.1 = end_offset;
(s, start_offset, end_offset)
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
let byte_offset = (offset + self.1) / 8;
let end_offset = (offset + self.1) % 8;
let s = self.0.peek_slice(byte_offset);
let start_offset = self.1;
(s, start_offset, end_offset)
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down
4 changes: 4 additions & 0 deletions src/stream/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ impl<I: Stream> Stream for Partial<I> {
fn next_slice(&mut self, offset: usize) -> Self::Slice {
self.input.next_slice(offset)
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
self.input.peek_slice(offset)
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down
4 changes: 4 additions & 0 deletions src/stream/recoverable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ where
fn next_slice(&mut self, offset: usize) -> Self::Slice {
self.input.next_slice(offset)
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
self.input.peek_slice(offset)
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down
4 changes: 4 additions & 0 deletions src/stream/stateful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ impl<I: Stream, S: crate::lib::std::fmt::Debug> Stream for Stateful<I, S> {
fn next_slice(&mut self, offset: usize) -> Self::Slice {
self.input.next_slice(offset)
}
#[inline(always)]
fn peek_slice(&self, offset: usize) -> Self::Slice {
self.input.peek_slice(offset)
}

#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
Expand Down
11 changes: 5 additions & 6 deletions src/stream/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ fn test_bit_stream_empty() {
let actual = i.offset_at(1);
assert_eq!(actual, Err(Needed::new(1)));

let (actual_input, actual_slice) = i.peek_slice(0);
assert_eq!(actual_input, (&b""[..], 0));
let actual_slice = i.peek_slice(0);
assert_eq!(actual_slice, (&b""[..], 0, 0));
}

Expand Down Expand Up @@ -105,15 +104,15 @@ fn bit_stream_inner(byte_len: usize, start: usize) {
let to_offset = curr_i.offset_from(&i);
assert_eq!(curr_offset, to_offset);

let (slice_i, _) = i.peek_slice(curr_offset);
assert_eq!(curr_i, slice_i);
let actual_slice = i.peek_slice(curr_offset);
let expected_slice = i.clone().peek_slice(curr_offset);
assert_eq!(actual_slice, expected_slice);

let at_offset = i.offset_at(curr_offset).unwrap();
assert_eq!(curr_offset, at_offset);

let eof_offset = curr_i.eof_offset();
let (next_eof_i, eof_slice) = curr_i.peek_slice(eof_offset);
assert_eq!(next_eof_i, (&b""[..], 0));
let eof_slice = curr_i.peek_slice(eof_offset);
let eof_slice_i = (eof_slice.0, eof_slice.1);
assert_eq!(eof_slice_i, curr_i);

Expand Down

0 comments on commit 5a3541a

Please # to comment.