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>
impl<T> PersistentStack<T>
Sourcepub fn new() -> PersistentStack<T>
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())
Sourcepub fn push(&mut self, data: T)
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]);
Sourcepub fn pop(&mut self) -> Result<T, PersistentStackPopError>
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]
Sourcepub fn iter(&self) -> PersistentStackIter<'_, T> ⓘ
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>
impl<T> Clone for PersistentStack<T>
Source§impl<T> Default for PersistentStack<T>
impl<T> Default for PersistentStack<T>
Source§fn default() -> PersistentStack<T>
fn default() -> PersistentStack<T>
Returns the “default value” for a type. Read more
Source§impl<'a, T> IntoIterator for &'a PersistentStack<T>
impl<'a, T> IntoIterator for &'a PersistentStack<T>
Auto Trait Implementations§
impl<T> Freeze for PersistentStack<T>
impl<T> RefUnwindSafe for PersistentStack<T>where
T: RefUnwindSafe,
impl<T> Send for PersistentStack<T>
impl<T> Sync for PersistentStack<T>
impl<T> Unpin for PersistentStack<T>
impl<T> UnwindSafe for PersistentStack<T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more