Buffer

Struct Buffer 

Source
pub struct Buffer<const N: usize> { /* private fields */ }
Expand description

A Buffer<N> consists of a [u8; N] array along with two usize indices into the array. N must be a power of two. (If you need more flexibility with sizing, consider using a bbqueue::BBBuffer instead.) A Buffer<N> can hold N bytes of data and guarantees FIFO ordering. The only way to use a Buffer is to split it into a Producer and a Consumer, which may then be passed to different threads or contexts.

Implementations§

Source§

impl<const N: usize> Buffer<N>

Source

pub const fn new() -> Self

Return a new, empty buffer. The memory backing the buffer is zero-initialized.

Examples found in repository?
examples/two_threads.rs (line 28)
27fn main() {
28    let mut b = fring::Buffer::<N>::new();
29    let (p, c) = b.split();
30    std::thread::scope(|s| {
31        s.spawn(|| {
32            producer(p);
33        });
34        consumer(c);
35    });
36}
Source

pub fn split(&mut self) -> (Producer<'_, N>, Consumer<'_, N>)

Split the Buffer into a Producer and a Consumer. This function is the only safe way to create a Producer or a Consumer. This function requires a mutable (i.e. exclusive) reference to the buffer, and the lifetime of that reference is equal to the lifetimes of the producer and consumer which are returned. Therefore, for a given buffer, only one producer and one consumer can exist at one time.

Examples found in repository?
examples/two_threads.rs (line 29)
27fn main() {
28    let mut b = fring::Buffer::<N>::new();
29    let (p, c) = b.split();
30    std::thread::scope(|s| {
31        s.spawn(|| {
32            producer(p);
33        });
34        consumer(c);
35    });
36}
Source

pub unsafe fn producer(&self) -> Producer<'_, N>

Return a Producer associated with this buffer. UNSAFE: the caller must ensure that at most one Producer for this buffer exists at any time.

Source

pub unsafe fn consumer(&self) -> Consumer<'_, N>

Return a Consumer associated with this buffer. UNSAFE: the caller must ensure that at most one Consumer for this buffer exists at any time.

Trait Implementations§

Source§

impl<const N: usize> Send for Buffer<N>

Source§

impl<const N: usize> Sync for Buffer<N>

Buffer<N> is Send and Sync because accesses to its internal data are only possible via a single Producer and a single Consumer at any time.

Auto Trait Implementations§

§

impl<const N: usize> !Freeze for Buffer<N>

§

impl<const N: usize> !RefUnwindSafe for Buffer<N>

§

impl<const N: usize> Unpin for Buffer<N>

§

impl<const N: usize> UnwindSafe for Buffer<N>

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.