pub struct ReadChunk<'a, T> { /* private fields */ }Expand description
Structure for reading from multiple slots in one go.
This is returned from Consumer::read_chunk().
Implementations§
Source§impl<T> ReadChunk<'_, T>
impl<T> 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 as_mut_slices(&mut self) -> (&mut [T], &mut [T])
pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])
Returns two mutable slices for reading from the requested slots.
This has the same semantics as as_slices(),
except that it returns mutable slices and requires a mutable reference
to the chunk.
In the vast majority of cases, mutable access is not required when reading data and the immutable version should be preferred. However, there are some scenarios where it might be desirable to perform operations on the data in-place without copying it to a separate buffer (e.g. streaming decryption), in which case this version can be used.
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::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);
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) {
assert_eq!(chunk.len(), 2);
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);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> IntoIterator for ReadChunk<'a, T>
impl<'a, T> IntoIterator for ReadChunk<'a, T>
Source§fn into_iter(self) -> <ReadChunk<'a, T> as IntoIterator>::IntoIter
fn into_iter(self) -> <ReadChunk<'a, T> as IntoIterator>::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.