pub struct Stack<T> { /* 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());source§impl<T> Stack<T>
impl<T> Stack<T>
sourcepub unsafe fn push_unchecked(&self, val: T) -> Arc<Entry<T>>
pub unsafe fn push_unchecked(&self, val: T) -> Arc<Entry<T>>
Pushes an instance of T without checking the lifetime of T.
Returns an Arc holding a strong reference to the newly pushed entry.
Safety
T::drop can be run after the Stack is dropped, therefore it is safe only if T::drop
does not access short-lived data or std::mem::needs_drop is false for T,
Examples
use scc::Stack;
let hello = String::from("hello");
let stack: Stack<&str> = Stack::default();
assert_eq!(unsafe { **stack.push_unchecked(hello.as_str()) }, "hello");sourcepub unsafe fn push_if_unchecked<F: FnMut(Option<&Entry<T>>) -> bool>(
&self,
val: T,
cond: F
) -> Result<Arc<Entry<T>>, T>
pub unsafe fn push_if_unchecked<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 without
checking the lifetime of T.
Errors
Returns an error along with the supplied instance if the condition is not met.
Safety
T::drop can be run after the Stack is dropped, therefore it is safe only if T::drop
does not access short-lived data or std::mem::needs_drop is false for T,
Examples
use scc::Stack;
let hello = String::from("hello");
let stack: Stack<&str> = Stack::default();
assert!(unsafe { stack.push_if_unchecked(hello.as_str(), |e| e.is_none()).is_ok() });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());