Struct ReadChunk

Source
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§

Source§

impl<T: Send + 'static> ReadChunk<'_, T>

Source

pub fn as_slices(&self) -> (&[T], &[T])

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.

Source

pub fn commit(self, n: usize)

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

pub fn commit_all(self)

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

Source

pub fn len(&self) -> usize

Returns the number of slots in the chunk.

Source

pub fn is_empty(&self) -> bool

Returns true if the chunk contains no slots.

Trait Implementations§

Source§

impl<'a, T: Debug + Send + 'static> Debug for ReadChunk<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: Send + 'static> IntoIterator for ReadChunk<'a, T>

Source§

fn into_iter(self) -> Self::IntoIter

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.

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = ReadChunkIntoIter<'a, T>

Which kind of iterator are we turning this into?
Source§

impl<T: Send + 'static> Send for ReadChunk<'_, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for ReadChunk<'a, T>

§

impl<'a, T> !RefUnwindSafe for ReadChunk<'a, T>

§

impl<'a, T> !Sync for ReadChunk<'a, T>

§

impl<'a, T> Unpin for ReadChunk<'a, T>

§

impl<'a, T> !UnwindSafe for ReadChunk<'a, T>

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.