[go: up one dir, main page]

Struct ConSet

Source
pub struct ConSet<T, S = RandomState>
where T: Clone + Hash + Eq + 'static,
{ /* private fields */ }
Expand description

A concurrent lock-free set.

Note that due to the limitations described in the crate level docs, values returned by looking up (or misplacing or removing) are always copied using the Clone trait. Therefore, the set is more suitable for types that are cheap to copy (eg. u64 or IpAddr).

If you intend to store types that are more expensive to make copies of or are not Clone, you can wrap them in an Arc (eg. Arc<str>).

use contrie::ConSet;
use crossbeam_utils::thread;

let set = ConSet::new();

thread::scope(|s| {
    s.spawn(|_| {
        set.insert("hello");
    });
    s.spawn(|_| {
        set.insert("world");
    });
}).unwrap();

assert_eq!(Some("hello"), set.get("hello"));
assert_eq!(Some("world"), set.get("world"));
assert_eq!(None, set.get("universe"));
set.remove("world");
assert_eq!(None, set.get("world"));
use contrie::set::{ConSet};
let set: ConSet<usize> = ConSet::new();

set.insert(0);
set.insert(1);

assert!(set.contains(&1));

set.remove(&1);
assert!(!set.contains(&1));

set.remove(&0);
assert!(set.is_empty());

Implementations§

Source§

impl<T> ConSet<T, RandomState>
where T: Clone + Hash + Eq + 'static,

Source

pub fn new() -> Self

Creates a new empty set.

Source§

impl<T, S> ConSet<T, S>
where T: Clone + Hash + Eq + 'static, S: BuildHasher,

Source

pub fn with_hasher(hasher: S) -> Self

Creates a new empty set with the given hasher.

Source

pub fn insert(&self, value: T) -> Option<T>

Inserts a new value into the set.

It returns the previous value, if any was present.

Source

pub fn get<Q>(&self, key: &Q) -> Option<T>
where Q: ?Sized + Eq + Hash, T: Borrow<Q>,

Looks up a value in the set.

This creates a copy of the original value.

Source

pub fn contains<Q>(&self, key: &Q) -> bool
where Q: ?Sized + Eq + Hash, T: Borrow<Q>,

Checks if a value identified by the given key is present in the set.

Note that by the time you can act on it, the presence of the value can change (eg. other thread can add or remove it in the meantime).

Source

pub fn remove<Q>(&self, key: &Q) -> Option<T>
where Q: ?Sized + Eq + Hash, T: Borrow<Q>,

Removes an element identified by the given key, returning it.

Source

pub fn is_empty(&self) -> bool

Checks if the set is currently empty.

Note that due to being concurrent, the use-case of this method is mostly for debugging purposes, because the state can change between reading the value and acting on it.

Source§

impl<T, S> ConSet<T, S>
where T: Clone + Hash + Eq + 'static,

Source

pub fn iter(&self) -> Iter<'_, T, S>

Returns an iterator through the elements of the set.

Trait Implementations§

Source§

impl<T, S> Debug for ConSet<T, S>
where T: Debug + Clone + Hash + Eq + 'static,

Source§

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

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

impl<T> Default for ConSet<T, RandomState>
where T: Clone + Hash + Eq + 'static,

Source§

fn default() -> Self

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

impl<'a, T, S> Extend<T> for &'a ConSet<T, S>
where T: Clone + Hash + Eq + 'static, S: BuildHasher,

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = T>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, S> Extend<T> for ConSet<T, S>
where T: Clone + Hash + Eq + 'static, S: BuildHasher,

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = T>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> FromIterator<T> for ConSet<T>
where T: Clone + Hash + Eq + 'static,

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<T> FromParallelIterator<T> for ConSet<T>
where T: Clone + Hash + Eq + Send + Sync,

Source§

fn from_par_iter<I>(iter: I) -> Self
where I: IntoParallelIterator<Item = T>,

Creates an instance of the collection from the parallel iterator par_iter. Read more
Source§

impl<'a, T, S> IntoIterator for &'a ConSet<T, S>
where T: Clone + Hash + Eq + 'static,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T, S> ParallelExtend<T> for &'a ConSet<T, S>
where T: Clone + Hash + Eq + Send + Sync, S: BuildHasher + Sync,

Source§

fn par_extend<I>(&mut self, par_iter: I)
where I: IntoParallelIterator<Item = T>,

Extends an instance of the collection with the elements drawn from the parallel iterator par_iter. Read more
Source§

impl<T, S> ParallelExtend<T> for ConSet<T, S>
where T: Clone + Hash + Eq + Send + Sync, S: BuildHasher + Sync,

Source§

fn par_extend<I>(&mut self, par_iter: I)
where I: IntoParallelIterator<Item = T>,

Extends an instance of the collection with the elements drawn from the parallel iterator par_iter. Read more

Auto Trait Implementations§

§

impl<T, S = RandomState> !Freeze for ConSet<T, S>

§

impl<T, S> RefUnwindSafe for ConSet<T, S>

§

impl<T, S> Send for ConSet<T, S>
where S: Send, T: Send,

§

impl<T, S> Sync for ConSet<T, S>
where S: Sync, T: Sync,

§

impl<T, S> Unpin for ConSet<T, S>
where S: Unpin, T: Unpin,

§

impl<T, S> UnwindSafe for ConSet<T, S>
where S: UnwindSafe, 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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.