pub struct LookaheadBuf<T: Copy, const CAP: usize> { /* private fields */ }Expand description
Fixed-capacity ring buffer for parser lookahead.
Slots are MaybeUninit because the token type is expected to be
Copy and the buffer tracks occupancy through head and len.
Only head..head + len indices (modulo CAP) are considered
initialised at any moment.
CAP must be a power of two; the indexing arithmetic uses a
bitmask (& (CAP - 1)) instead of a modulo for speed. A debug-build
assertion rejects non-power-of-two capacities during construction.
Implementations§
Source§impl<T: Copy, const CAP: usize> LookaheadBuf<T, CAP>
impl<T: Copy, const CAP: usize> LookaheadBuf<T, CAP>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty buffer. CAP must be a power of two; violating
this in debug builds trips a panic on first use.
§Panics
In debug builds, panics if CAP is not a power of two.
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Whether the buffer is full. Callers that rely on the ring never overflowing (no expansion is possible) should check this before pushing.
Sourcepub fn push_back(&mut self, value: T)
pub fn push_back(&mut self, value: T)
Push a token at the back.
Hard-panics on overflow: the ring has no heap fallback and
overwriting the oldest slot would silently corrupt the buffer.
Callers must size CAP large enough for the deepest lookahead
the parser ever performs.
§Panics
Panics if CAP tokens have been pushed without any being consumed.
Trait Implementations§
Auto Trait Implementations§
impl<T, const CAP: usize> Freeze for LookaheadBuf<T, CAP>where
T: Freeze,
impl<T, const CAP: usize> RefUnwindSafe for LookaheadBuf<T, CAP>where
T: RefUnwindSafe,
impl<T, const CAP: usize> Send for LookaheadBuf<T, CAP>where
T: Send,
impl<T, const CAP: usize> Sync for LookaheadBuf<T, CAP>where
T: Sync,
impl<T, const CAP: usize> Unpin for LookaheadBuf<T, CAP>where
T: Unpin,
impl<T, const CAP: usize> UnsafeUnpin for LookaheadBuf<T, CAP>where
T: UnsafeUnpin,
impl<T, const CAP: usize> UnwindSafe for LookaheadBuf<T, CAP>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more