Struct type_set::TypeSet

source ·
pub struct TypeSet(/* private fields */);
Expand description

A collection for heterogenous types

Note that there is currently no way to iterate over the collection, as there may be types stored that cannot be named by the calling code

Implementations§

source§

impl TypeSet

source

pub const fn new() -> Self

Create an empty TypeSet.

source

pub fn is_empty(&self) -> bool

Returns true if the TypeSet contains zero types.

source

pub fn len(&self) -> usize

Returns the number of distinct types in this TypeSet.

source

pub fn entry<T: Send + Sync + 'static>(&mut self) -> Entry<'_, T>

Gets the corresponding type in the set for in-place manipulation.

See Entry for usage.

source

pub fn insert<T: Send + Sync + 'static>(&mut self, value: T) -> Option<T>

Insert a value into this TypeSet.

If a value of this type already exists, it will be replaced and returned.

§Example
let mut set = type_set::TypeSet::new().with("hello");
let previous = set.insert("world");
assert_eq!(set.get::<&'static str>(), Some(&"world"));
assert_eq!(previous, Some("hello"));
source

pub fn with<T: Send + Sync + 'static>(self, value: T) -> Self

Chainable constructor to add a type to this TypeSet

§Example
let set = type_set::TypeSet::new().with("hello");
assert_eq!(set.get::<&'static str>(), Some(&"hello"));
source

pub fn contains<T: Send + Sync + 'static>(&self) -> bool

Check if this TypeSet contains a value for type T

§Example
let set = type_set::TypeSet::new().with("hello");
assert!(set.contains::<&'static str>());
assert!(!set.contains::<String>());
source

pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>

Immutably borrow a value that has been inserted into this TypeSet.

source

pub fn get_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T>

Attempt to mutably borrow to a value that has been inserted into this TypeSet.

§Example
let mut set = type_set::TypeSet::new().with(String::from("hello"));
if let Some(string) = set.get_mut::<String>() {
    string.push_str(" world");
}
assert_eq!(set.get::<String>().unwrap(), "hello world");
source

pub fn take<T: Send + Sync + 'static>(&mut self) -> Option<T>

Remove a value from this TypeSet.

If a value of this type exists, it will be returned.

§Example
let mut set = type_set::TypeSet::new().with("hello");
assert_eq!(set.take::<&'static str>(), Some("hello"));
assert_eq!(set.take::<&'static str>(), None);
source

pub fn get_or_insert<T: Send + Sync + 'static>(&mut self, default: T) -> &mut T

Get a value from this TypeSet or populate it with the provided default.

Identical to Entry::or_insert

If building T is expensive, use TypeSet::get_or_insert_with or Entry::or_insert_with

§Example
let mut set = type_set::TypeSet::new();
assert_eq!(set.get_or_insert("hello"), &mut "hello");
assert_eq!(set.get_or_insert("world"), &mut "hello");
source

pub fn get_or_insert_with<T: Send + Sync + 'static>( &mut self, default: impl FnOnce() -> T, ) -> &mut T

Get a value from this TypeSet or populate it with the provided default function.

Identical to Entry::or_insert_with

Prefer this to TypeSet::get_or_insert when building type T is expensive, since it will only be executed when T is absent.

§Example
let mut set = type_set::TypeSet::new();
assert_eq!(set.get_or_insert_with(|| String::from("hello")), "hello");
assert_eq!(set.get_or_insert_with::<String>(|| panic!("this is never called")), "hello");
source

pub fn get_or_insert_default<T: Default + Send + Sync + 'static>( &mut self, ) -> &mut T

Ensure a value is present by filling with Default::default

Identical to Entry::or_default.

§Example
let mut set = type_set::TypeSet::new().with(10usize);
let ten: usize = *set.get_or_insert_default();
assert_eq!(ten, 10);
source

pub fn merge(&mut self, other: TypeSet)

Merge another TypeSet into this one, replacing any collisions

§Example
let mut set_a = type_set::TypeSet::new().with(8u8).with("hello");
let set_b = type_set::TypeSet::new().with(32u32).with("world");
set_a.merge(set_b);
assert_eq!(set_a.get::<u8>(), Some(&8));
assert_eq!(set_a.get::<u32>(), Some(&32));
assert_eq!(set_a.get::<&'static str>(), Some(&"world"));

Trait Implementations§

source§

impl Debug for TypeSet

source§

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

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

impl Default for TypeSet

source§

fn default() -> TypeSet

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

§

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

§

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.