ErasedSendSet

Struct ErasedSendSet 

Source
pub struct ErasedSendSet { /* private fields */ }
Expand description

Like ErasedSet but with a Send bound.

§Example

use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
set.insert(ClickEvent(128, 256));
set.insert(KeyDownEvent('z'));

assert_eq!(set.get::<ClickEvent>(), Some(&ClickEvent(128, 256)));

assert_eq!(set.insert(KeyDownEvent('e')), Some(KeyDownEvent('z')));

set.remove::<ClickEvent>();

assert_eq!(set.len(), 1);

Implementations§

Source§

impl ErasedSendSet

Source

pub fn new() -> Self

Creates an empty ErasedSendSet.

The set is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

§Examples
use erased_set::ErasedSendSet;

let set = ErasedSendSet::new();
Source

pub fn is_empty(&self) -> bool

Returns true if the set contains no instances of any type.

§Examples
use erased_set::ErasedSendSet;

let set = ErasedSendSet::new();
assert!(set.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of types in the set.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
assert_eq!(set.len(), 0);
set.insert("a");
assert_eq!(set.len(), 1);
Source

pub fn clear(&mut self)

Clears the set. Keep allocated memory for reuse.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
set.insert("a");
set.clear();
assert!(set.is_empty());
Source

pub fn contains<T>(&self) -> bool
where T: Any,

Returns true if the set contains an instance of T.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
set.insert("a");
assert!(set.contains::<&str>());
Source

pub fn get<T>(&self) -> Option<&T>
where T: Any + Send,

Returns a reference to an instance of T.

If the set does not have an instance of T, None is returned.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
set.insert("a");
assert_eq!(set.get::<&str>(), Some(&"a"));
assert_eq!(set.get::<bool>(), None);
Source

pub fn get_or_insert<T>(&mut self, value: T) -> &T
where T: Any + Send,

Inserts the given value into the set if it is not present, then returns a reference to the value in the set.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
assert_eq!(set.get_or_insert("abc"), &"abc");
assert_eq!(set.get_or_insert("def"), &"abc");
Source

pub fn get_or_insert_with<T>(&mut self, f: impl FnOnce() -> T) -> &T
where T: Any + Send,

Inserts a value computed from f into the set if it does not contain a value of type T, then returns a reference to the value in the set.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
assert_eq!(set.get_or_insert_with(|| String::from("abc")), &"abc");
assert_eq!(set.get_or_insert_with(|| String::from("def")), &"abc");
Source

pub fn get_mut<T>(&mut self) -> Option<&mut T>
where T: Any + Send,

Returns a mutable reference to an instance of T.

If the set does not have an instance of T, None is returned.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
set.insert("a");
if let Some(x) = set.get_mut::<&str>() {
    *x = "b";
}
assert_eq!(set.get::<&str>(), Some(&"b"));
Source

pub fn insert<T>(&mut self, value: T) -> Option<T>
where T: Any + Send,

Insert an instance of type T into the set.

Returns the replaced value or None.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
assert_eq!(set.insert("a"), None);
assert_eq!(set.insert("b"), Some("a"));
Source

pub fn remove<T>(&mut self) -> Option<T>
where T: Any + Send,

Remove and return an instance of type T from the set.

If the set did not have this type present, None is returned.

§Examples
use erased_set::ErasedSendSet;

let mut set = ErasedSendSet::new();
set.insert("a");
assert_eq!(set.remove::<&str>(), Some("a"));
Source

pub fn type_ids(&self) -> impl Iterator<Item = &TypeId>

Gets an iterator over the TypeIds of stored elements, in arbitrary order.

Source

pub fn debug_type_names(&self) -> impl Iterator<Item = &'static str> + '_

Gets an iterator over the names of the stored types, in arbitrary order.

Trait Implementations§

Source§

impl Debug for ErasedSendSet

Available on crate feature send only.
Source§

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

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

impl Default for ErasedSendSet

Source§

fn default() -> ErasedSendSet

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

Auto Trait Implementations§

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.