Struct MergeCell

Source
pub struct MergeCell<T> { /* private fields */ }
Expand description

A memory location that allows repeated merging.

This type acts like an “accumulator” for merging. It allows merging many values and defering merge errors for later.

MergeCell deliberately does not implement Merge. It is not intended to be merged with other values, rather it expects to have values merged into it.

§Example

let mut cell = MergeCell::empty();

cell.merge(vec![1, 2]);
cell.merge(vec![]);
cell.merge(vec![0, 4, 8]);

let merged = cell.try_finish().unwrap().unwrap();
assert_eq!(merged, &[1, 2, 0, 4, 8]);

Implementations§

Source§

impl<T> MergeCell<T>

Source

pub fn empty() -> Self

Create a new empty MergeCell.

Source

pub fn new(value: T) -> Self

Create a new MergeCell that is initialized with value.

Source

pub fn is_empty(&self) -> bool

Check whether the cell is empty.

The cell is empty if and only if it was created with empty() and no values have been merge()d.

Source

pub fn has_errored(&self) -> bool

Check whether a previous merge() operation has failed.

Source

pub fn finish(self) -> Result<T, Error>

Destruct the MergeCell and get back the final merged value.

Returns the result of all of the merge() operations on the cell.

§Panics

If the MergeCell is empty. For the non-panicking version of this method, see: try_finish().

§Example
let mut cell = MergeCell::empty();

cell.merge(vec![1, 2]);
cell.merge(vec![]);
cell.merge(vec![0, 4, 8]);

let merged = cell.finish().unwrap();
assert_eq!(merged, &[1, 2, 0, 4, 8]);
Source

pub fn try_finish(self) -> Option<Result<T, Error>>

Destruct the MergeCell and get back the final merged value.

This is the non-panicking version of finish().

Returns None if the cell is empty. Otherwise, it is functionally the same as finish().

§Example
let mut cell = MergeCell::empty();

cell.merge(vec![1, 2]);
cell.merge(vec![]);
cell.merge(vec![0, 4, 8]);

let merged = cell.try_finish().unwrap().unwrap();
assert_eq!(merged, &[1, 2, 0, 4, 8]);
Source§

impl<T> MergeCell<T>
where T: Merge,

Source

pub fn merge(&mut self, other: T)

Merge other into the cell.

This function will fill the cell if it is empty.

Trait Implementations§

Source§

impl<T: Debug> Debug for MergeCell<T>

Source§

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

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

impl<T> Default for MergeCell<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for MergeCell<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for MergeCell<T>

§

impl<T> Send for MergeCell<T>
where T: Send,

§

impl<T> Sync for MergeCell<T>
where T: Sync,

§

impl<T> Unpin for MergeCell<T>
where T: Unpin,

§

impl<T> !UnwindSafe for MergeCell<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.