[][src]Struct rtrb::Producer

pub struct Producer<T> { /* fields omitted */ }

The producer side of a RingBuffer.

Can be moved between threads, but references from different threads are not allowed (i.e. it is Send but not Sync).

Can only be created with RingBuffer::split() (together with its counterpart, the Consumer).

Examples

use rtrb::RingBuffer;

let (producer, consumer) = RingBuffer::<f32>::new(1000).split();

Implementations

impl<T> Producer<T>[src]

pub fn push(&mut self, value: T) -> Result<(), PushError<T>>[src]

Attempts to push an element into the queue.

The element is moved into the ring buffer and its slot is made available to be read by the Consumer. If the queue is full, the element is returned back as an error.

Examples

use rtrb::{RingBuffer, PushError};

let (mut p, c) = RingBuffer::new(1).split();

assert_eq!(p.push(10), Ok(()));
assert_eq!(p.push(20), Err(PushError::Full(20)));

pub fn write_chunk(&mut self, n: usize) -> Result<WriteChunk<'_, T>, ChunkError> where
    T: Copy + Default
[src]

Returns n slots (initially containing their Default value) for writing.

If not enough slots are available, an error (containing the number of available slots) is returned.

The elements can be accessed with WriteChunk::as_mut_slices() or by iterating over (a &mut to) the WriteChunk.

The provided slots are not automatically made available to be read by the Consumer. This has to be explicitly done by calling WriteChunk::commit(), WriteChunk::commit_iterated() or WriteChunk::commit_all().

The type parameter T has a trait bound of Copy, which makes sure that no destructors are called at any time (because it implies !Drop).

For an unsafe alternative that has no restrictions on T, see Producer::write_chunk_uninit().

Examples

See the crate-level documentation for examples.

pub fn write_chunk_uninit(
    &mut self,
    n: usize
) -> Result<WriteChunkUninit<'_, T>, ChunkError>
[src]

Returns n (uninitialized) slots for writing.

If not enough slots are available, an error (containing the number of available slots) is returned.

The elements can be accessed with WriteChunkUninit::as_mut_slices() or by iterating over (a &mut to) the WriteChunkUninit.

The provided slots are not automatically made available to be read by the Consumer. This has to be explicitly done by calling WriteChunkUninit::commit(), WriteChunkUninit::commit_iterated() or WriteChunkUninit::commit_all().

Safety

This function itself is safe, but accessing the returned slots might not be, as well as invoking some methods of WriteChunkUninit.

For a safe alternative that provides Default-initialized slots, see Producer::write_chunk().

pub fn slots(&self) -> usize[src]

Returns the number of slots available for writing.

To check for a single available slot, using Producer::is_full() is often quicker (because it might not have to check an atomic variable).

Examples

use rtrb::RingBuffer;

let (p, c) = RingBuffer::<f32>::new(1024).split();

assert_eq!(p.slots(), 1024);

pub fn is_full(&self) -> bool[src]

Returns true if there are no slots available for writing.

Examples

use rtrb::RingBuffer;

let (p, c) = RingBuffer::<f32>::new(1).split();

assert!(!p.is_full());

pub fn buffer(&self) -> &RingBuffer<T>[src]

Returns a read-only reference to the ring buffer.

Trait Implementations

impl<T: Debug> Debug for Producer<T>[src]

impl<T: Send> Send for Producer<T>[src]

impl Write for Producer<u8>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for Producer<T>

impl<T> !Sync for Producer<T>

impl<T> Unpin for Producer<T>

impl<T> UnwindSafe for Producer<T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.