ConsBranch

Struct ConsBranch 

Source
pub struct ConsBranch<T> { /* private fields */ }
Available on crate features collections and cons only.
Expand description

A references counted, linked list implemented similar to a cons list. This type is useful as an list of immutable items with cheap, shallow cloning, that can share nodes with other instances.

When cloned, only the ‘head’ of the tree is cloned (just an Rc), with all of the elements included in both list. As a result, the data structure is only mutable from the head, where elements can be pushed or popped. This cheap cloning is helpful for implementing procedures such that include rollbacks or branching.

Implementations§

Source§

impl<T> ConsBranch<T>

Source

pub const fn new() -> ConsBranch<T>

Creates a new, empty ConsTree.

Source

pub fn push(&mut self, value: T)

Pushes a new element onto the start of this ConsTree, updating this list’s head without affecting any overlapping lists (shallow clones).

Source

pub fn get_head(&self) -> Option<&T>

Returns a reference to the element contained within the head node.

Source

pub const fn is_empty(&self) -> bool

Returns true if this ConsTree contains no elements.

Source

pub fn iter(&self) -> Iter<'_, T>

Produces a borrowed Iterator<Item = &T> over all elements in this list, both unique and shared.

Source

pub fn into_iter_rc(&self) -> RcIter<T>

Produces an Iterator<Item = Rc<ConsTreeNode<T>>> over all of the underlying Rc instances in this list.

Each referenced element is considered ‘shared’ for the lifetime of the Rc produced by this iterator. To return a ConsTree to being unique, this iterator and all produced Rcs need to be dropped.

Source

pub fn is_unique(&self) -> bool

Returns true if this entire list is unique (doesn’t share any items with another list).

If this method returns true, into_iter_unique will produce every item in the list.

Source

pub fn is_head_unique(&self) -> bool

Returns true if the head element of this list is unique.

Source

pub fn pop_if_unique(&mut self) -> Option<T>

Pops the head element of this list, if it is unique. Otherwise, self remains unchanged.

Source

pub fn split_off_unique(&mut self) -> ConsBranch<T>

Removes all unique items from this list and returns them as another ConsTree.

Source

pub const fn into_iter_unique(self) -> UniqueIter<T>

Produces an Iterator<Item = T> over the elements in this list, producing owned values until a shared element is found.

This iterator does no cloning and produces owned items by completely ignoring any elements that are shared by other lists.

If called on every clone of a single initial ConsTree, every element of the tree will be returned by an iterator only once.

Source§

impl<T: Clone> ConsBranch<T>

Source

pub fn pop_to_owned(&mut self) -> Option<T>

Pops the head element from this list, cloning if it is shared by another ConsTree. Regardless of if a clone is required, the head of this list will be updated.

Source

pub const fn into_iter_owned(self) -> OwnedIter<T>

Produces an Iterator<Item = T> over all elements in this list, returning owned items by cloning any shared elements.

Source

pub fn deep_clone(&self) -> ConsBranch<T>

Produces a deep clone of this ConsTree. The result has a clone of every element in this list, without sharing any. The result is unique.

Trait Implementations§

Source§

impl<T> Clone for ConsBranch<T>

Source§

fn clone(&self) -> Self

Creates a cheap (shallow) clone of this ConsTree, with all the same underlying elements. After cloning, all elements of the list are considered ‘shared’ between the original list and the clone.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ConsBranch<T>

Source§

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

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

impl<T> Default for ConsBranch<T>

Source§

fn default() -> Self

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

impl<T> From<Rc<ConsNode<T>>> for ConsBranch<T>

Source§

fn from(value: Rc<ConsNode<T>>) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for ConsBranch<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ConsBranch<T>

§

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

§

impl<T> !Send for ConsBranch<T>

§

impl<T> !Sync for ConsBranch<T>

§

impl<T> Unpin for ConsBranch<T>

§

impl<T> UnwindSafe for ConsBranch<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.