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

Structure for reading from multiple slots in one go.

This is returned from Consumer::read_chunk().

Implementations

Returns two slices for reading from the requested slots.

The first slice can only be empty if 0 slots have been requested. If the first slice contains all requested slots, the second one is empty.

The provided slots are not automatically made available to be written again by the Producer. This has to be explicitly done by calling commit() or commit_all(). Note that this runs the destructor of the committed items (if T implements Drop). You can “peek” at the contained values by simply not calling any of the “commit” methods.

Drops the first n slots of the chunk, making the space available for writing again.

Panics

Panics if n is greater than the number of slots in the chunk.

Examples

The following example shows that items are dropped when “committed” (which is only relevant if T implements Drop).

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

let mut collector = Collector::new();

// 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, &collector.handle());

    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
        collector.collect();
        assert_eq!(unsafe { DROP_COUNT }, 0);
    } else {
        unreachable!();
    }
    // First Thing has been dropped when "thing" went out of scope:
    collector.collect();
    assert_eq!(unsafe { DROP_COUNT }, 1);
    assert!(p.push(Thing).is_ok()); // 3

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

Drops all slots of the chunk, making the space available for writing again.

Returns the number of slots in the chunk.

Returns true if the chunk contains no slots.

Trait Implementations

Formats the value using the given formatter. Read more

Turns a ReadChunk into an iterator.

When the iterator is dropped, all iterated slots are made available for writing again. Non-iterated items remain in the ring buffer.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

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.