Skip to main content

Stack

Struct Stack 

Source
pub struct Stack<T> { /* private fields */ }
Expand description

A concurrent and lock-free stack using Treiber’s algorithm.

The Treiber Stack is a simple concurrent data structure that uses the fine-grained “compare-and-swap” concurrency primitive.

§Examples

use extended_collections::sync::Stack;

let mut s = Stack::new();

s.push(0);
s.push(1);
assert_eq!(s.len(), 2);

assert_eq!(s.try_pop(), Some(1));
assert_eq!(s.try_pop(), Some(0));
assert_eq!(s.len(), 0);

Implementations§

Source§

impl<T> Stack<T>

Source

pub fn new() -> Self

Constructs a new, empty Stack<T>.

§Examples
use extended_collections::sync::Stack;

let s: Stack<u32> = Stack::new();
Source

pub fn push(&self, value: T)

Pushes an item onto the stack.

§Examples
use extended_collections::sync::Stack;

let mut s = Stack::new();
s.push(0);
Source

pub fn try_pop(&self) -> Option<T>

Attempts to pop the top element of the stack. Returns None if it was unable to pop the top element.

§Examples
use extended_collections::sync::Stack;

let mut s = Stack::new();

s.push(0);

assert_eq!(s.try_pop(), Some(0));
assert_eq!(s.try_pop(), None);
Source

pub fn len(&self) -> usize

Returns the approximate number of elements in the stack.

§Examples
use extended_collections::sync::Stack;

let mut s = Stack::new();
assert_eq!(s.len(), 0);

s.push(0);
assert_eq!(s.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the approximate number of elements in the stack is zero.

§Examples
use extended_collections::sync::Stack;

let mut s = Stack::new();
assert!(s.is_empty());

s.push(0);
assert!(!s.is_empty());

Trait Implementations§

Source§

impl<T> Default for Stack<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for Stack<T>

§

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

§

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

§

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

§

impl<T> Unpin for Stack<T>

§

impl<T> UnsafeUnpin for Stack<T>

§

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