Struct TRBuffer

Source
pub struct TRBuffer<T> { /* private fields */ }
Expand description

A TRBuffer is similar to a circular buffer, but data is inserted in three revolving regions of the buffer space. This allows reads to return contiguous blocks of memory, even if they span a region that would normally include a wrap-around in a circular buffer. It’s especially useful for APIs requiring blocks of contiguous memory, eliminating the need to copy data into an interim buffer before use.

§Example

use triskell::TRBuffer;

// Creates a TRBuffer of u8 and allocates 8 elements.
let mut buffer: TRBuffer<u8> = TRBuffer::with_capacity(8);
{
  // Reserves 4 slots at the back for insert
  let reserved = buffer.reserve_back(4).unwrap();
  reserved[0] = 2;
  reserved[1] = 12;
  reserved[2] = 34;
  reserved[3] = 7;
}
// Stores the values into an available region
buffer.commit(4);
{
  // Gets the front data stored in the region as a contiguous block
  let block = buffer.read_front().unwrap();
  assert_eq!(block[0], 2);
  assert_eq!(block[1], 12);
  assert_eq!(block[2], 34);
  assert_eq!(block[3], 7);
}
// Release the first two front elements of the block
buffer.free_front(2);
{
  let block = buffer.read_front().unwrap();
  assert_eq!(block[0], 34);
  assert_eq!(block[1], 7);
}

Implementations§

Source§

impl<T> TRBuffer<T>

Source

pub fn new() -> Self

Construct a new empty TRBuffer.

Source

pub fn with_capacity(len: usize) -> Self

Construct a new empty TRBuffer with at least the specified capacity.

The TRBuffer will be able to hold at least capacity elements without reallocating.

Source

pub fn capacity(&self) -> usize

Return the total number of elements the TRBuffer can hold without reallocating.

Source

pub fn set_allocation_strategy( &mut self, allocation_strategy: AllocationStrategy, )

Define the allocation strategy.

Source

pub fn is_empty(&self) -> bool

Returns true if the TRBuffer contains no elements.

Source

pub fn len(&self) -> usize

Returns the number of elements in the TRBuffer.

Source

pub fn clear(&mut self)

Clears all regions and reservations.

Source

pub fn reserve_front(&mut self, len: usize) -> Result<&mut [T], TriskellError>

Reserves len bytes to be prepended and return a mutable slice of T.

If there is not enough space, reallocates the buffer.

Source

pub fn reserve_back(&mut self, len: usize) -> Result<&mut [T], TriskellError>

Reserves len bytes to be appended and return a mutable slice of T.

If there is not enough space, reallocates the buffer.

Source

pub fn commit(&mut self, len: usize)

Commits the data in the reservation, allowing it to be read later

Source

pub fn read_front(&self) -> Option<&[T]>

Retrieves available (committed) data as a contiguous block (FIFO).

Returns None if there is no data available Must called free_front() to free data.

Source

pub fn read_front_mut(&mut self) -> Option<&mut [T]>

Source

pub fn read_front_indexes(&self) -> Option<(usize, usize)>

Source

pub fn read_back(&self) -> Option<&[T]>

Retrieves available (committed) data as a contiguous block (LIFO).

Returns None if there is no data available Must called free_back() to free data.

Source

pub fn read_back_mut(&mut self) -> Option<&mut [T]>

Source

pub fn read_back_indexes(&self) -> Option<(usize, usize)>

Source

pub fn get(&self, start: usize, end: usize) -> &[T]

Source

pub fn get_mut(&mut self, start: usize, end: usize) -> &mut [T]

Source

pub fn free_front(&mut self, len: usize)

Remove len bytes at the beginning of the buffer.

The next time read() is called, it will not include these elements.

Source

pub fn free_back(&mut self, len: usize)

Remove len bytes at the end of the buffer.

The next time read() is called, it will not include these elements.

Trait Implementations§

Source§

impl<T: Debug> Debug for TRBuffer<T>

Source§

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

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

impl<T: Default> Default for TRBuffer<T>

Source§

fn default() -> TRBuffer<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for TRBuffer<T>

§

impl<T> RefUnwindSafe for TRBuffer<T>
where T: RefUnwindSafe,

§

impl<T> Send for TRBuffer<T>
where T: Send,

§

impl<T> Sync for TRBuffer<T>
where T: Sync,

§

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

§

impl<T> UnwindSafe for TRBuffer<T>
where T: UnwindSafe,

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.