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>
impl<T: Send + 'static> ReadChunk<'_, T>
Sourcepub fn as_slices(&self) -> (&[T], &[T])
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.
Sourcepub fn commit(self, n: usize)
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);
Sourcepub fn commit_all(self)
pub fn commit_all(self)
Drops all slots of the chunk, making the space available for writing again.
Trait Implementations§
Source§impl<'a, T: Send + 'static> IntoIterator for ReadChunk<'a, T>
impl<'a, T: Send + 'static> IntoIterator for ReadChunk<'a, T>
Source§fn into_iter(self) -> Self::IntoIter
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.