scc::stack

Struct Stack

Source
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>

Source

pub fn push(&self, val: T) -> Shared<Entry<T>>

Pushes an instance of T.

Returns a Shared holding a strong reference to the newly pushed entry.

§Examples
use scc::Stack;

let stack: Stack<usize> = Stack::default();

assert_eq!(**stack.push(11), 11);
Source

pub fn push_if<F: FnMut(Option<&Entry<T>>) -> bool>( &self, val: T, cond: F, ) -> Result<Shared<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

pub fn peek<'g>(&self, guard: &'g Guard) -> Option<&'g Entry<T>>

Returns a guarded reference to the newest entry.

Returns None if the Stack is empty. The returned reference can survive as long as the associated Guard is alive.

§Examples
use scc::ebr::Guard;
use scc::Stack;

let stack: Stack<usize> = Stack::default();

assert!(stack.peek(&Guard::new()).is_none());

stack.push(37);
stack.push(3);

assert_eq!(**stack.peek(&Guard::new()).unwrap(), 3);
Source§

impl<T> Stack<T>

Source

pub unsafe fn push_unchecked(&self, val: T) -> Shared<Entry<T>>

Pushes an instance of T without checking the lifetime of T.

Returns a Shared 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");
Source

pub unsafe fn push_if_unchecked<F: FnMut(Option<&Entry<T>>) -> bool>( &self, val: T, cond: F, ) -> Result<Shared<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() });
Source

pub fn pop(&self) -> Option<Shared<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());
Source

pub fn pop_all(&self) -> Self

Pops all the entries at once, and passes each one of the popped entries to the supplied closure.

§Examples
use scc::Stack;

let stack: Stack<usize> = Stack::default();

stack.push(37);
stack.push(3);

let popped = stack.pop_all();

stack.push(1);

assert_eq!(stack.pop().map(|e| **e), Some(1));
assert!(stack.pop().is_none());
assert!(stack.is_empty());

assert_eq!(popped.pop().map(|e| **e), Some(3));
assert_eq!(popped.pop().map(|e| **e), Some(37));
assert!(popped.pop().is_none());
Source

pub fn pop_if<F: FnMut(&Entry<T>) -> bool>( &self, cond: F, ) -> Result<Option<Shared<Entry<T>>>, Shared<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());
Source

pub fn peek_with<R, F: FnOnce(Option<&Entry<T>>) -> R>(&self, reader: F) -> R

Peeks the newest entry.

§Examples
use scc::Stack;

let stack: Stack<usize> = Stack::default();

assert!(stack.peek_with(|v| v.is_none()));

stack.push(37);
stack.push(3);

assert_eq!(stack.peek_with(|v| **v.unwrap()), 3);
Source

pub fn len(&self) -> usize

Returns the number of entries in the Stack.

This method iterates over all the entries in the Stack to count them, therefore its time complexity is O(N).

§Examples
use scc::Stack;

let stack: Stack<usize> = Stack::default();
assert_eq!(stack.len(), 0);

stack.push(7);
stack.push(11);
assert_eq!(stack.len(), 2);

stack.pop();
stack.pop();
assert_eq!(stack.len(), 0);
Source

pub fn is_empty(&self) -> bool

Returns true if the Stack is empty.

§Examples
use scc::Stack;

let stack: Stack<usize> = Stack::default();
assert!(stack.is_empty());

stack.push(7);
assert!(!stack.is_empty());
Source

pub fn iter<'g>(&self, guard: &'g Guard) -> Iter<'g, T>

Returns an Iter.

§Examples
use scc::ebr::Guard;
use scc::Stack;

let stack: Stack<usize> = Stack::default();
assert_eq!(stack.iter(&Guard::new()).count(), 0);

stack.push(7);
stack.push(11);
stack.push(17);

let guard = Guard::new();
let mut iter = stack.iter(&guard);
assert_eq!(*iter.next().unwrap(), 17);
assert_eq!(*iter.next().unwrap(), 11);
assert_eq!(*iter.next().unwrap(), 7);
assert!(iter.next().is_none());

Trait Implementations§

Source§

impl<T: Clone> Clone for Stack<T>

Source§

fn clone(&self) -> Self

Returns a copy 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: Debug> Debug for Stack<T>

Source§

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

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

impl<T> Default for Stack<T>

Source§

fn default() -> Self

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

impl<T> Drop for Stack<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Stack<T>

§

impl<T> RefUnwindSafe for Stack<T>

§

impl<T> Send for Stack<T>
where T: Send,

§

impl<T> Sync for Stack<T>
where T: Sync,

§

impl<T> Unpin for Stack<T>

§

impl<T> UnwindSafe for Stack<T>
where T: UnwindSafe,

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.