Skip to main content

OnceArrayWriter

Struct OnceArrayWriter 

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

Source

pub fn with_capacity(n: usize) -> OnceArrayWriter<T>

Creates a new OnceArrayWriter with the specified capacity.

Source

pub fn reader(&self) -> &Arc<OnceArray<T>>

Obtain a read-only reference to the committed part of the array.

Source

pub fn remaining_capacity(&self) -> usize

Returns the number of additional elements that can be written to the buffer before it is full.

Source

pub fn as_slice(&self) -> &[T]

Obtain an immutable slice of the entire array, including committed and uncommitted parts.

Source

pub fn uncommitted_mut(&mut self) -> &mut [T]

Obtain a mutable slice of the uncommitted part of the array.

Source

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

Source

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

Source

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

Source

pub fn commit(&mut self)

Makes newly written elements immutable and atomically visible to readers.

Source

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.

Source

pub fn revert(&mut self)

Discards any uncommitted elements, reverting the buffer to the last committed state.

Trait Implementations§

Source§

impl<T> Drop for OnceArrayWriter<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<Vec<T>> for OnceArrayWriter<T>

Source§

fn from(vec: Vec<T>) -> OnceArrayWriter<T>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for OnceArrayWriter<T>

§

impl<T> RefUnwindSafe for OnceArrayWriter<T>
where T: RefUnwindSafe,

§

impl<T> Send for OnceArrayWriter<T>
where T: Sync + Send,

§

impl<T> Sync for OnceArrayWriter<T>
where T: Sync + Send,

§

impl<T> Unpin for OnceArrayWriter<T>

§

impl<T> UnsafeUnpin for OnceArrayWriter<T>

§

impl<T> UnwindSafe for OnceArrayWriter<T>
where T: RefUnwindSafe,

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.