Struct WriteChunkUninit

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

Source

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).

Source

pub unsafe fn commit(self, n: usize)

Makes the first n slots of the chunk available for reading.

§Panics

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

§Safety

The user must make sure that the first n elements have been initialized.

Source

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.

Source

pub fn fill_from_iter<I>(self, iter: I) -> usize
where 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));
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 WriteChunkUninit<'a, T>

Source§

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

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

impl<'a, T> From<WriteChunkUninit<'a, T>> for WriteChunk<'a, T>
where T: Default + Send + 'static,

Source§

fn from(chunk: WriteChunkUninit<'a, T>) -> Self

Fills all slots with the Default value.

Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'a, T> !UnwindSafe for WriteChunkUninit<'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.