pub struct Consumer<T: Send + 'static> { /* private fields */ }
Expand description

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::new() (together with its counterpart, the Producer).

Individual elements can be moved out of the ring buffer with Consumer::pop(), multiple elements at once can be read with Consumer::read_chunk().

The number of slots currently available for reading can be obtained with Consumer::slots().

When the Consumer is dropped, [Producer::is_abandoned()] will return true. This can be used as a crude way to communicate to the sending thread that no more data will be consumed. When the Consumer is dropped after the Producer has already been dropped, RingBuffer::drop() will be called, freeing the allocated memory.

Implementations

Returns n slots for reading.

ReadChunk::as_slices() provides immutable access to the slots. After reading from those slots, they explicitly have to be made available to be written again by the Producer by calling ReadChunk::commit() or ReadChunk::commit_all().

Alternatively, items can be moved out of the ReadChunk using iteration because it implements IntoIterator (ReadChunk::into_iter() can be used to explicitly turn it into an Iterator). All moved items are automatically made available to be written again by the Producer.

Errors

If not enough slots are available, an error (containing the number of available slots) is returned. Use Consumer::slots() to obtain the number of available slots beforehand.

Examples

See the documentation of the chunks module.

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.

Errors

If the queue is empty, an error is returned.

Examples
use rtrb_basedrop::{PopError, RingBuffer};
use basedrop::Collector;

let collector = Collector::new();

let (mut p, mut c) = RingBuffer::new(1, &collector.handle());

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));

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

Errors

If the queue is empty, an error is returned.

Examples
use rtrb_basedrop::{PeekError, RingBuffer};
use basedrop::Collector;

let collector = Collector::new();

let (mut p, c) = RingBuffer::new(1, &collector.handle());

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));

Returns the number of slots available for reading.

Since items can be concurrently produced on another thread, the actual number of available slots may increase at any time (up to the RingBuffer::capacity()).

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_basedrop::RingBuffer;
use basedrop::Collector;

let collector = Collector::new();

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

assert_eq!(c.slots(), 0);

Returns true if there are currently no slots available for reading.

An empty ring buffer might cease to be empty at any time if the corresponding Producer is producing items in another thread.

Examples
use rtrb_basedrop::RingBuffer;
use basedrop::Collector;

let collector = Collector::new();

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

assert!(c.is_empty());

Since items can be concurrently produced on another thread, the ring buffer might not be empty for long:

if c.is_empty() {
    // The buffer might be empty, but it might as well not be
    // if an item was just produced on another thread.
}

However, if it’s not empty, another thread cannot change that:

if !c.is_empty() {
    // At least one slot is guaranteed to be available for reading.
}

Returns a read-only reference to the ring buffer.

Trait Implementations

Formats the value using the given formatter. Read more

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more

Like read, except that it reads into a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Reader has an efficient read_vectored implementation. Read more

Read all bytes until EOF in this source, placing them into buf. Read more

Read all bytes until EOF in this source, appending them to buf. Read more

Read the exact number of bytes required to fill buf. Read more

🔬 This is a nightly-only experimental API. (read_buf)

Pull some bytes from this source into the specified buffer. Read more

🔬 This is a nightly-only experimental API. (read_buf)

Read the exact number of bytes required to fill buf. Read more

Creates a “by reference” adaptor for this instance of Read. Read more

Transforms this Read instance to an Iterator over its bytes. Read more

Creates an adapter which will chain this stream with another. Read more

Creates an adapter which will read at most limit bytes from it. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.