pub struct Stack<T: 'static> { /* private fields */ }Expand description
Stack is a lock-free concurrent last-in-first-out container.
Implementations§
source§impl<T: 'static> Stack<T>
impl<T: 'static> Stack<T>
sourcepub fn push_if<F: FnMut(Option<&Entry<T>>) -> bool>(
&self,
val: T,
cond: F
) -> Result<Arc<Entry<T>>, T>
pub fn push_if<F: FnMut(Option<&Entry<T>>) -> bool>( &self, val: T, cond: F ) -> Result<Arc<Entry<T>>, T>
Pushes an instance of T if the newest entry satisfies the given condition.
Errors
Returns an error along with the supplied instance if the condition is not met.
Examples
use scc::Stack;
let stack: Stack<usize> = Stack::default();
stack.push(11);
assert!(stack.push_if(17, |e| e.map_or(false, |x| **x == 11)).is_ok());
assert!(stack.push_if(29, |e| e.map_or(false, |x| **x == 11)).is_err());sourcepub fn pop(&self) -> Option<Arc<Entry<T>>>
pub fn pop(&self) -> Option<Arc<Entry<T>>>
Pops the newest entry.
Returns None if the Stack is empty.
Examples
use scc::Stack;
let stack: Stack<usize> = Stack::default();
stack.push(37);
stack.push(3);
stack.push(1);
assert_eq!(stack.pop().map(|e| **e), Some(1));
assert_eq!(stack.pop().map(|e| **e), Some(3));
assert_eq!(stack.pop().map(|e| **e), Some(37));
assert!(stack.pop().is_none());sourcepub fn pop_if<F: FnMut(&Entry<T>) -> bool>(
&self,
cond: F
) -> Result<Option<Arc<Entry<T>>>, Arc<Entry<T>>>
pub fn pop_if<F: FnMut(&Entry<T>) -> bool>( &self, cond: F ) -> Result<Option<Arc<Entry<T>>>, Arc<Entry<T>>>
Pops the newest entry if the entry satisfies the given condition.
Returns None if the Stack is empty.
Errors
Returns an error along with the newest entry if the given condition is not met.
Examples
use scc::Stack;
let stack: Stack<usize> = Stack::default();
stack.push(3);
stack.push(1);
assert!(stack.pop_if(|v| **v == 3).is_err());
assert_eq!(stack.pop().map(|e| **e), Some(1));
assert_eq!(stack.pop_if(|v| **v == 3).ok().and_then(|e| e).map(|e| **e), Some(3));
assert!(stack.is_empty());sourcepub fn peek_with<'b, R, F: FnOnce(&'b Entry<T>) -> R>(
&self,
reader: F,
barrier: &'b Barrier
) -> Option<R>
pub fn peek_with<'b, R, F: FnOnce(&'b Entry<T>) -> R>( &self, reader: F, barrier: &'b Barrier ) -> Option<R>
Peeks the newest entry with the supplied Barrier.
Returns None if the Stack is empty.
Examples
use scc::ebr::Barrier;
use scc::Stack;
let stack: Stack<usize> = Stack::default();
assert!(stack.peek_with(|v| **v, &Barrier::new()).is_none());
stack.push(37);
stack.push(3);
assert_eq!(stack.peek_with(|v| **v, &Barrier::new()), Some(3));