[][src]Struct rtrb::Consumer

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

The consumer 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 Producer).

Examples

use rtrb::RingBuffer;

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

Implementations

impl<T> Consumer<T>[src]

pub fn pop(&mut self) -> Result<T, PopError>[src]

Attempts to pop an element from the queue.

The element is moved out of the ring buffer and its slot is made available to be filled by the Producer again. If the queue is empty, an error is returned.

Examples

use rtrb::{PopError, RingBuffer};

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

assert_eq!(p.push(10), Ok(()));
assert_eq!(c.pop(), Ok(10));
assert_eq!(c.pop(), Err(PopError::Empty));

To obtain an Option<T>, use .ok() on the result.

assert_eq!(p.push(20), Ok(()));
assert_eq!(c.pop().ok(), Some(20));

pub fn peek(&self) -> Result<&T, PeekError>[src]

Attempts to read an element from the queue without removing it.

If the queue is empty, an error is returned.

Examples

use rtrb::{PeekError, RingBuffer};

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

assert_eq!(c.peek(), Err(PeekError::Empty));
assert_eq!(p.push(10), Ok(()));
assert_eq!(c.peek(), Ok(&10));
assert_eq!(c.peek(), Ok(&10));

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

Returns n slots for reading.

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

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

The provided slots are not automatically made available to be written again by the Producer. This has to be explicitly done by calling ReadChunk::commit(), ReadChunk::commit_iterated() or ReadChunk::commit_all(). You can "peek" at the contained values by simply not calling any of the "commit" methods.

Examples

Items are dropped when ReadChunk::commit(), ReadChunk::commit_iterated() or ReadChunk::commit_all() is called (which is only relevant if T implements Drop).

use rtrb::RingBuffer;

// Static variable to count all drop() invocations
static mut DROP_COUNT: i32 = 0;
#[derive(Debug)]
struct Thing;
impl Drop for Thing {
    fn drop(&mut self) { unsafe { DROP_COUNT += 1; } }
}

// Scope to limit lifetime of ring buffer
{
    let (mut p, mut c) = RingBuffer::new(2).split();

    assert!(p.push(Thing).is_ok()); // 1
    assert!(p.push(Thing).is_ok()); // 2
    if let Ok(thing) = c.pop() {
        // "thing" has been *moved* out of the queue but not yet dropped
        assert_eq!(unsafe { DROP_COUNT }, 0);
    } else {
        unreachable!();
    }
    // First Thing has been dropped when "thing" went out of scope:
    assert_eq!(unsafe { DROP_COUNT }, 1);
    assert!(p.push(Thing).is_ok()); // 3

    if let Ok(chunk) = c.read_chunk(2) {
        let (first, second) = chunk.as_slices();
        assert_eq!(first.len(), 1);
        assert_eq!(second.len(), 1);
        assert_eq!(unsafe { DROP_COUNT }, 1);
        chunk.commit(1); // Drops only one of the two Things
        assert_eq!(unsafe { DROP_COUNT }, 2);
    } else {
        unreachable!();
    }
    // The last Thing is still in the queue ...
    assert_eq!(unsafe { DROP_COUNT }, 2);
}
// ... and it is dropped when the ring buffer goes out of scope:
assert_eq!(unsafe { DROP_COUNT }, 3);

See the crate-level documentation for more examples.

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

Returns the number of slots available for reading.

To check for a single available slot, using Consumer::is_empty() 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!(c.slots(), 0);

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

Returns true if there are no slots available for reading.

Examples

use rtrb::RingBuffer;

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

assert!(c.is_empty());

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

Returns a read-only reference to the ring buffer.

Trait Implementations

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

impl<T: Eq> Eq for Consumer<T>[src]

impl<T: PartialEq> PartialEq<Consumer<T>> for Consumer<T>[src]

impl Read for Consumer<u8>[src]

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

impl<T> StructuralEq for Consumer<T>[src]

impl<T> StructuralPartialEq for Consumer<T>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for Consumer<T>

impl<T> !Sync for Consumer<T>

impl<T> Unpin for Consumer<T>

impl<T> UnwindSafe for Consumer<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.