Struct PersistentStack

Source
pub struct PersistentStack<T>(/* private fields */);
Expand description

§Concurrent persistent stack

Supportted operations:

  • clone O(1)
  • push O(1)
  • pop O(1)
  • iterate O(n)
use persistent_stack::PersistentStack;

let mut s1 = PersistentStack::new();
s1.push(1);
let mut s2 = s1.clone();
std::thread::spawn(move || {
    s2.push(2);
    assert_eq!(s2.iter().copied().collect::<Vec<_>>(), vec![2, 1]);
    std::thread::sleep(std::time::Duration::from_millis(20));
    s2.push(4);
    assert_eq!(s2.iter().copied().collect::<Vec<_>>(), vec![4, 2, 1]);
});
s1.push(3);
assert_eq!(s1.iter().copied().collect::<Vec<_>>(), vec![3, 1]);
std::thread::sleep(std::time::Duration::from_millis(20));
s1.push(5);
assert_eq!(s1.iter().copied().collect::<Vec<_>>(), vec![5, 3, 1]);

Implementations§

Source§

impl<T> PersistentStack<T>

Source

pub fn new() -> PersistentStack<T>

Creates new empty persistent stack

use persistent_stack::PersistentStack;

let s = PersistentStack::<i32>::new();
assert!(s.into_iter().next().is_none())
Source

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

Pushes data to end of self (affects only current copy of stack)

use persistent_stack::PersistentStack;
use std::sync::Arc;

let mut s1 = PersistentStack::new();
s1.push(1);
let mut s2 = s1.clone();
s2.push(2);
assert_eq!(s1.into_iter().collect::<Vec<_>>(), [&1]);
assert_eq!(s2.into_iter().collect::<Vec<_>>(), [&2, &1]);
Source

pub fn pop(&mut self) -> Result<T, PersistentStackPopError>

Pops value from stack and tries to return it

Returns Ok(data) if only this copy of stack owned data

Returns Err(CantUnwrap) if there are other copies, which own data

Returns Err(RootIsReached) if nothing to pop

use persistent_stack::{PersistentStack, PersistentStackPopError::*};

let mut s1 = PersistentStack::new();
s1.push(1);
s1.push(2);
let mut s2 = s1.clone();
s1.push(3);
s2.push(4);
assert_eq!(s2.pop(), Ok(4)); // only s2 owned 4
assert_eq!(s2.pop(), Err(CantUnwrap)); // s1 also owns 2
let mut s3 = s2.clone();
assert_eq!(s2.pop(), Err(CantUnwrap)); // s1 also owns 1
assert_eq!(s2.pop(), Err(RootIsReached)); // There are no elements in s2
s3.push(5);
assert_eq!(s3.iter().copied().collect::<Vec<_>>(), vec![5, 1]); // s3 is cloned, when s2 was [1]
Source

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

Creates iterator over self by reference

use persistent_stack::PersistentStack;

let mut s = PersistentStack::new();
s.push(1);
s.push(2);
s.push(3);
assert_eq!(s.iter().collect::<Vec<_>>(), [&3, &2, &1]);
s.push(4); // s didn't move out

Trait Implementations§

Source§

impl<T> Clone for PersistentStack<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Default for PersistentStack<T>

Source§

fn default() -> PersistentStack<T>

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

impl<'a, T> IntoIterator for &'a PersistentStack<T>

Source§

type IntoIter = PersistentStackIter<'a, T>

Which kind of iterator are we turning this into?
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for PersistentStack<T>

§

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

§

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

§

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

§

impl<T> Unpin for PersistentStack<T>

§

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