pub struct WriteChunkUninit<'a, T: Send + 'static> { /* private fields */ }
Expand description
Structure for writing into multiple (uninitialized) slots in one go.
This is returned from Producer::write_chunk_uninit()
.
Implementations§
Source§impl<T: Send + 'static> WriteChunkUninit<'_, T>
impl<T: Send + 'static> WriteChunkUninit<'_, T>
Sourcepub fn as_mut_slices(
&mut self,
) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>])
pub fn as_mut_slices( &mut self, ) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>])
Returns two slices for writing to 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 extension trait CopyToUninit
can be used to safely copy data into those slices.
After writing to the slots, they are not automatically made available
to be read by the Consumer
.
This has to be explicitly done by calling commit()
or commit_all()
.
If items are written but not committed afterwards,
they will not become available for reading and
they will be leaked (which is only relevant if T
implements Drop
).
Sourcepub unsafe fn commit_all(self)
pub unsafe fn commit_all(self)
Makes the whole chunk available for reading.
§Safety
The user must make sure that all elements have been initialized.
Sourcepub fn fill_from_iter<I>(self, iter: I) -> usizewhere
I: IntoIterator<Item = T>,
pub fn fill_from_iter<I>(self, iter: I) -> usizewhere
I: IntoIterator<Item = T>,
Moves items from an iterator into the (uninitialized) slots of the chunk.
The number of moved items is returned.
All moved items are automatically made availabe to be read by the Consumer
.
§Examples
If the iterator contains too few items, only a part of the chunk is made available for reading:
use rtrb_basedrop::{RingBuffer, PopError};
use basedrop::Collector;
let collector = Collector::new();
let (mut p, mut c) = RingBuffer::new(4, &collector.handle());
if let Ok(chunk) = p.write_chunk_uninit(3) {
assert_eq!(chunk.fill_from_iter([10, 20]), 2);
} else {
unreachable!();
}
assert_eq!(p.slots(), 2);
assert_eq!(c.pop(), Ok(10));
assert_eq!(c.pop(), Ok(20));
assert_eq!(c.pop(), Err(PopError::Empty));
If the chunk size is too small, some items may remain in the iterator.
To be able to keep using the iterator after the call,
&mut
(or Iterator::by_ref()
) can be used.
use rtrb_basedrop::{RingBuffer, PopError};
use basedrop::Collector;
let collector = Collector::new();
let (mut p, mut c) = RingBuffer::new(4, &collector.handle());
let mut it = vec![10, 20, 30].into_iter();
if let Ok(chunk) = p.write_chunk_uninit(2) {
assert_eq!(chunk.fill_from_iter(&mut it), 2);
} else {
unreachable!();
}
assert_eq!(c.pop(), Ok(10));
assert_eq!(c.pop(), Ok(20));
assert_eq!(c.pop(), Err(PopError::Empty));
assert_eq!(it.next(), Some(30));
Trait Implementations§
Source§impl<'a, T> From<WriteChunkUninit<'a, T>> for WriteChunk<'a, T>
impl<'a, T> From<WriteChunkUninit<'a, T>> for WriteChunk<'a, T>
Source§fn from(chunk: WriteChunkUninit<'a, T>) -> Self
fn from(chunk: WriteChunkUninit<'a, T>) -> Self
Fills all slots with the Default
value.