Struct RingBuffer

Source
pub struct RingBuffer<'a, T> { /* private fields */ }
Expand description

Ring buffer for allocating varying length byte slices in a sequential, FIFO fashion.

Allocations are represented by RingSlices, which are lifetimeless handles to this buffer, and can be used to get a &mut [u8] that will hold the memory until the RingSlice is passed into RingBuffer::free. The slices must be freed in the same order as they were allocated. As such, dropping a RingSlice will cause the slice to never be reclaimed, which will “clog” the ring buffer.

The sum of the lengths of the slices allocated from this buffer, when full, may be less than the total capacity, since the individual slices are contiguous and can’t span across the end of the backing buffer. These gaps could be prevented with memory mapping trickery in the future.

Implementations§

Source§

impl<T> RingBuffer<'_, T>

Source

pub fn new( allocator: &'static LinearAllocator<'_>, capacity: usize, ) -> Option<RingBuffer<'static, T>>

Allocates a new ring buffer with the given capacity.

Source

pub unsafe fn from_mut(buffer: &mut [MaybeUninit<T>]) -> RingBuffer<'_, T>

Creates a new ring buffer with the given buffer.

§Safety

All allocations made from this RingBuffer must be passed back into RingBuffer::free before it is dropped, as the backing memory is only borrowed for its lifetime, and the Box references could leak.

Source

pub fn capacity(&self) -> usize

Returns how many elements can be allocated at maximum without freeing.

Source§

impl<T: Zeroable> RingBuffer<'_, T>

Source

pub fn allocate(&mut self, len: usize) -> Option<RingSlice<T>>

Allocates and zeroes out a slice of the given length if there’s enough contiguous free space.

Source

pub fn free(&mut self, slice: RingSlice<T>) -> Result<(), RingSlice<T>>

Reclaims the memory occupied by the given slice. Returns the slice back in an Err if the slice isn’t the current head of the allocated span, and the memory is not reclaimed.

§Panics

Panics if the RingSlice was allocated from a different RingBuffer.

Source§

impl<T> RingBuffer<'_, T>

Source

pub fn allocate_box(&mut self, value: T) -> Result<RingBox<T>, T>

Allocates space for one T if there’s free space, and boxes it.

Source

pub fn free_box(&mut self, boxed: RingBox<T>) -> Result<(), RingBox<T>>

Reclaims the memory occupied by the given box. Returns the box back in an Err if the slice isn’t the current head of the allocated span, and the memory is not reclaimed.

§Panics

Panics if the RingBox was allocated from a different RingBuffer.

Trait Implementations§

Source§

impl<T: Sync> Sync for RingBuffer<'_, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for RingBuffer<'a, T>

§

impl<'a, T> RefUnwindSafe for RingBuffer<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> !Send for RingBuffer<'a, T>

§

impl<'a, T> Unpin for RingBuffer<'a, T>

§

impl<'a, T> !UnwindSafe for RingBuffer<'a, 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> 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.