pub struct OnceArrayWriter<T> { /* private fields */ }Expand description
Exclusive write access to a OnceArray.
The OnceArrayWriter provides methods to append elements to the uncommitted
portion of the array. The uncommitted portion is not visible to readers and
can be mutated or
discarded because the writer retains exclusive access.
Once the writer is ready to make new elements visible to readers, it can
call commit() or
commit_partial(n) to make elements
immutable and atomically visible to readers. As long as there is
remaining capacity, the writer can continue to append and commit more elements.
The API is optimized for scenarios where data is written to a series of new
OnceArrayWriter chunks as they fill, so the append APIs return a Result
handing back the unconsumed data when full, so the caller can easily continue
filling the next chunk.
Implementations§
Source§impl<T> OnceArrayWriter<T>
impl<T> OnceArrayWriter<T>
Sourcepub fn with_capacity(n: usize) -> OnceArrayWriter<T>
pub fn with_capacity(n: usize) -> OnceArrayWriter<T>
Creates a new OnceArrayWriter with the specified capacity.
Sourcepub fn reader(&self) -> &Arc<OnceArray<T>>
pub fn reader(&self) -> &Arc<OnceArray<T>>
Obtain a read-only reference to the committed part of the array.
Sourcepub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Returns the number of additional elements that can be written to the buffer before it is full.
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Obtain an immutable slice of the entire array, including committed and uncommitted parts.
Sourcepub fn uncommitted_mut(&mut self) -> &mut [T]
pub fn uncommitted_mut(&mut self) -> &mut [T]
Obtain a mutable slice of the uncommitted part of the array.
Sourcepub fn try_push(&mut self, val: T) -> Result<(), T>
pub fn try_push(&mut self, val: T) -> Result<(), T>
Attempts to append an element to the buffer.
If the buffer is full, returns Err(val), returning ownership of the value
that could not be added.
The new element is not visible to readers until a call to commit().
Sourcepub fn extend<I: IntoIterator<Item = T>>(
&mut self,
iter: I,
) -> Result<(), I::IntoIter>
pub fn extend<I: IntoIterator<Item = T>>( &mut self, iter: I, ) -> Result<(), I::IntoIter>
Attempts to append elements from iter to the buffer.
If the buffer becomes full before iter is exhausted, returns
Err(iter), returning ownership of the iterator.
Note that if the iterator exactly fills the remaining capacity, this
will return Err with an empty iterator, since the Iterator trait
does not allow checking if an iterator is exhausted without calling
next().
The new elements are not visible to readers until a call to commit().
Sourcepub fn extend_from_slice<'a>(&mut self, src: &'a [T]) -> &'a [T]where
T: Copy,
pub fn extend_from_slice<'a>(&mut self, src: &'a [T]) -> &'a [T]where
T: Copy,
Attempts to append elements from src to the array.
Returns the tail of the slice that could not be written to the buffer. If the buffer is not filled and all elements were written, this will be an empty slice.
The new elements are not visible to readers until a call to commit().
Sourcepub fn commit(&mut self)
pub fn commit(&mut self)
Makes newly written elements immutable and atomically visible to readers.
Sourcepub fn commit_partial(&mut self, n: usize)
pub fn commit_partial(&mut self, n: usize)
Makes the first n newly written elements immutable and atomically visible to readers.
Panics if n is greater than the number of initialized but uncommitted elements.