Struct Buffer

Source
#[repr(C)]
pub struct Buffer<T> { /* private fields */ }
Expand description

The internal memory buffer used by the queue.

Buffer holds a pointer to allocated memory which represents the bounded ring buffer, as well as a head and tail atomicUsize which the producer and consumer use to track location in the ring.

Implementations§

Source§

impl<T> Buffer<T>

Source

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

Attempt to pop a value off the buffer.

If the buffer is empty, this method will not block. Instead, it will return None signifying the buffer was empty. The caller may then decide what to do next (e.g. spin-wait, sleep, process something else, etc)

§Examples
// Attempt to pop off a value
let t = buffer.try_pop();
match t {
  Some(v) => {}, // Got a value
  None => {}     // Buffer empty, try again later
}
Source

pub fn skip_n(&self, n: usize) -> usize

Attempts to pop (and discard) at most n values off the buffer.

Returns the amount of values successfully skipped.

§Safety

WARNING: This will leak at most n values from the buffer, i.e. the destructors of the objects skipped over will not be called. This function is intended to be used on buffers that contain non-Drop data, such as a Buffer<f32>.

Source

pub fn pop(&self) -> T

Pop a value off the buffer.

This method will block until the buffer is non-empty. The waiting strategy is a simple spin-wait and will repeatedly call try_pop() until a value is available. If you do not want a spin-wait burning CPU, you should call try_pop() directly and implement a different waiting strategy.

§Examples
// Block until a value is ready
let t = buffer.pop();
Source

pub fn try_push(&self, v: T) -> Option<T>

Attempt to push a value onto the buffer.

If the buffer is full, this method will not block. Instead, it will return Some(v), where v was the value attempting to be pushed onto the buffer. If the value was successfully pushed onto the buffer, None will be returned signifying success.

§Examples
// Attempt to push a value onto the buffer
let t = buffer.try_push(123);
match t {
  Some(v) => {}, // Buffer was full, try again later
  None => {}     // Value was successfully pushed onto the buffer
}
Source

pub fn push(&self, v: T)

Push a value onto the buffer.

This method will block until the buffer is non-full. The waiting strategy is a simple spin-wait and will repeatedly call try_push() until the value can be added. If you do not want a spin-wait burning CPU, you should call try_push() directly and implement a different waiting strategy.

§Examples
// Block until we can push this value onto the buffer
buffer.try_push(123);

Trait Implementations§

Source§

impl<T> Drop for Buffer<T>

Handles deallocation of heap memory when the buffer is dropped

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Sync> Sync for Buffer<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Buffer<T>

§

impl<T> !RefUnwindSafe for Buffer<T>

§

impl<T> !Send for Buffer<T>

§

impl<T> Unpin for Buffer<T>

§

impl<T> UnwindSafe for Buffer<T>
where T: RefUnwindSafe,

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.