RotatingBuffer

Struct RotatingBuffer 

Source
pub struct RotatingBuffer { /* private fields */ }
Expand description

The RotatingBuffer is a queue implementation wrapping a BytesMut.

RotatingBuffer::enqueue and RotatingBuffer::dequeue will not require memory to be shifted.

Implementations§

Source§

impl RotatingBuffer

Source

pub fn new(size: usize) -> Self

Creates a new RotatingBuffer

§PANICS

Panics if the size is less than 2.

Source

pub fn is_empty(&self) -> bool

Returns whether or not the RotatingBuffer is empty

Source

pub fn capacity(&self) -> usize

Returns the total capacity. This is the number of elements we can enqueue (without dequeueing) before we can no longer enqueue anymore elements. Once we reach this capacity, you must dequeue in order to fit into the RotatingBuffer without resizing.

Source

pub fn len(&self) -> usize

Returns the number of elements currently in the Queue.

Source

pub fn peek_pos(&self, pos: usize) -> Option<u8>

Peek the value stored at a given position.

Note: pos is the position in the queue, not necessarily the index in the buffer, and starts at 0 where 0 represents the head of the queue.

Source

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

Peeks the first value in the queue. Returns None if the queue is empty.

This method should be preferred over calling RotatingBuffer::peek_pos at position 0.

Source

pub fn peek_last(&self) -> Option<u8>

Peeks the last value in the queue. Returns None if the queue is empty.

This should be preferred over calling RotatingBuffer::peek_pos at position (last position)

Source

pub fn dequeue(&mut self) -> Option<u8>

Returns the front-most value from the Queue in a Some. If the RotatingBuffer is empty, we will return a None.

This should be fairly cheap to run, as no memory in the buffer is altered. Once an item is dequeued, every sequential item’s position is one less than it was before.

Source

pub fn at_capacity(&self) -> bool

Returns a bool representing whether the RotatingBuffer is at capacity. This means that enqueueing another value will cause an Err.

Source

pub fn enqueue(&mut self, value: u8) -> Result<(), RotatingBufferAtCapacity>

Enqueues an item into the RotatingBuffer. Returns an Err with a RotatingBufferAtCapacity if at capacity.

Enqueueing should be fairly cheap, as we initialize the internal buffer with the maximum size given in the constructor, so we will always be either replacing a pre-existing and already dequeued value, or we will be placing a value into already allocated memory.

Trait Implementations§

Source§

impl Debug for RotatingBuffer

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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, 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.