Struct scc::Bag

source ·
pub struct Bag<T, const ARRAY_LEN: usize = DEFAULT_ARRAY_LEN> { /* private fields */ }
Expand description

Bag is a lock-free concurrent unordered instance container.

Bag is a linearizable concurrent instance container where ARRAY_LEN instances are stored in a fixed-size array, and the rest are managed by its backup container; this makes a Bag especially efficient if the expected number of instances does not exceed ARRAY_LEN.

The maximum value of ARRAY_LEN is limited to usize::BITS / 2 which is the default value, and if a larger value is specified, Bag::new panics.

Implementations§

source§

impl<T, const ARRAY_LEN: usize> Bag<T, ARRAY_LEN>

source

pub fn new() -> Self

Creates a new Bag.

Panics

Panics if the specified ARRAY_LEN value is larger than usize::BITS / 2.

Examples
use scc::Bag;

let bag: Bag<usize, 16> = Bag::new();
source

pub fn push(&self, val: T)

Pushes an instance of T.

Examples
use scc::Bag;

let bag: Bag<usize> = Bag::default();

bag.push(11);
source

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

Pops an instance in the Bag if not empty.

Examples
use scc::Bag;

let bag: Bag<usize> = Bag::default();

bag.push(37);

assert_eq!(bag.pop(), Some(37));
assert!(bag.pop().is_none());
source

pub fn pop_all<B, F: FnMut(B, T) -> B>(&self, init: B, fold: F) -> B

Pops all the entries at once, and folds them into an accumulator.

Examples
use scc::Bag;

let bag: Bag<usize> = Bag::default();

bag.push(7);
bag.push(17);
bag.push(37);

assert_eq!(bag.pop_all(0, |a, v| a + v), 61);

bag.push(47);
assert_eq!(bag.pop(), Some(47));
assert!(bag.pop().is_none());
assert!(bag.is_empty());
source

pub fn is_empty(&self) -> bool

Returns true if the Bag is empty.

Examples
use scc::Bag;

let bag: Bag<usize> = Bag::default();
assert!(bag.is_empty());

bag.push(7);
assert!(!bag.is_empty());

assert_eq!(bag.pop(), Some(7));
assert!(bag.is_empty());
source

pub fn iter_mut(&mut self) -> Accessor<'_, T, ARRAY_LEN>

Iterates over contained instances for modifying them.

Examples
use scc::Bag;

let mut bag: Bag<usize> = Bag::default();

bag.push(3);
bag.push(3);

assert_eq!(bag.iter_mut().count(), 2);
bag.iter_mut().for_each(|e| { *e += 1; });

assert_eq!(bag.pop(), Some(4));
assert_eq!(bag.pop(), Some(4));
assert!(bag.pop().is_none());

Trait Implementations§

source§

impl<T: Debug, const ARRAY_LEN: usize> Debug for Bag<T, ARRAY_LEN>

source§

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

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

impl<T> Default for Bag<T, DEFAULT_ARRAY_LEN>

source§

fn default() -> Self

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

impl<T, const ARRAY_LEN: usize> Drop for Bag<T, ARRAY_LEN>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, const ARRAY_LEN: usize> RefUnwindSafe for Bag<T, ARRAY_LEN>where T: RefUnwindSafe,

§

impl<T, const ARRAY_LEN: usize> Send for Bag<T, ARRAY_LEN>where T: Send,

§

impl<T, const ARRAY_LEN: usize> Sync for Bag<T, ARRAY_LEN>where T: Sync,

§

impl<T, const ARRAY_LEN: usize> Unpin for Bag<T, ARRAY_LEN>where T: Unpin,

§

impl<T, const ARRAY_LEN: usize> UnwindSafe for Bag<T, ARRAY_LEN>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.