Struct PeekableBuffer

Source
pub struct PeekableBuffer<T>
where T: Clone,
{ /* private fields */ }

Implementations§

Source§

impl<T> PeekableBuffer<T>
where T: Clone,

Source

pub fn new<E>(elements: E) -> Self
where E: AsRef<[T]>,

Creates a PeekableBuffer object that owns all elements of &[T] via cloning.

Source

pub fn is_at_end(&self) -> bool

Returns true if the stream has reached the end.

Source

pub fn len(&self) -> usize

Returns the number of elements in the stream.

Source

pub fn lookaround(&self, offset: i64) -> Option<&T>

Returns a reference to the element offset positions away from the element currently being pointed to by the cursor. If the computed offset is outside the bounds of the stream, None is returned.

Source

pub fn peek(&self) -> Option<&T>

Returns a reference to the element just after the element currently being pointed to by the cursor. This is equivalent to calling lookaround(1).

Source

pub fn current(&self) -> Option<&T>

Returns a reference to the element currently being pointed to by the cursor. This is equivalent to calling lookaround(0).

Source

pub fn shift(&mut self, offset: i64)

Shifts the cursor by offset positions. The computed offset will be within the range [0, len()]. If the computed offset is less than 0, the cursor will point to the first element. If the computed offset is greater than len() - 1, the cursor will point to the end and is_at_end() returns true.

Source

pub fn advance(&mut self)

A convenience method that advances the cursor by 1. If the stream is at the end, no action is taken. This is equivalent to calling shift(1).

Source

pub fn set_pos(&mut self, pos: usize) -> usize

Sets the zero-indexed position of the cursor. If the given pos is outside of the range of the stream length, the cursor will be set to len().

Source

pub fn pos(&self) -> usize

Returns the current zero-indexed position of the cursor. The returned value is in the range [0, len()].

Source

pub fn consume(&mut self) -> Option<&T>

Returns a reference to the element currently being pointed to by the cursor, then advances the pointer by 1.

Source

pub fn take_while<P>(&mut self, predicate: P) -> impl Iterator<Item = &T>
where P: Fn(&T) -> bool,

Returns an iterator containing elements that satisfy the given predicate, starting from the element currently pointed to by the stream pointer, up to either the end of the stream, or when the predicate returns fall, whichever comes first.

Source

pub fn take(&mut self, n: usize) -> impl Iterator<Item = &T>

Returns an iterator containing all elements in the range [pos(), pos() + n). The cursor will point to either the end of the stream, or the element at pos() + n, whichever is first. If pos() + n is outside the bounds of the stream, then the rest of the stream is consumed and is_at_end() returns true.

Source

pub fn accept<P>(&self, predicate: P) -> bool
where P: Fn(&T) -> bool,

Returns true if the current element matches the given predicate, false otherwise. The function will return false if the stream is at the end regardless of the predicate.

Source

pub fn slice_from(&self, from_inc: usize) -> PeekableBuffer<T>

Returns a new PeekableBuffer containing all elements from the range [from_inc, len() - 1], cloning the required elements into their own iterable. If from_inc is outside the range of the stream, an empty PeekableBuffer is returned.

Source

pub fn slice_between(&self, from_inc: usize, to_exc: usize) -> PeekableBuffer<T>

Returns a new PeekableBuffer containing all elements from the range [from_inc, to_exc - 1], cloning the required elements into their own iterable. If from_inc > to_exc, this function will panic. Otherwise, if from_inc is greater than len(), an empty PeekableBuffer is returned. If from_inc == to_exc, an empty PeekableBuffer is returned as well.

Trait Implementations§

Source§

impl<T> Clone for PeekableBuffer<T>
where T: Clone + Clone,

Source§

fn clone(&self) -> PeekableBuffer<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for PeekableBuffer<T>
where T: Clone + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> IntoIterator for PeekableBuffer<T>
where T: Clone,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> PartialEq for PeekableBuffer<T>
where T: Clone + PartialEq,

Source§

fn eq(&self, other: &PeekableBuffer<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Eq for PeekableBuffer<T>
where T: Clone + Eq,

Source§

impl<T> StructuralPartialEq for PeekableBuffer<T>
where T: Clone,

Auto Trait Implementations§

§

impl<T> Freeze for PeekableBuffer<T>

§

impl<T> !RefUnwindSafe for PeekableBuffer<T>

§

impl<T> !Send for PeekableBuffer<T>

§

impl<T> !Sync for PeekableBuffer<T>

§

impl<T> Unpin for PeekableBuffer<T>
where T: Unpin,

§

impl<T> !UnwindSafe for PeekableBuffer<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.