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
impl TypeSet
sourcepub fn entry<T: Send + Sync + 'static>(&mut self) -> Entry<'_, T>
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.
sourcepub fn insert<T: Send + Sync + 'static>(&mut self, value: T) -> Option<T>
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"));
sourcepub fn with<T: Send + Sync + 'static>(self, value: T) -> Self
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"));
sourcepub fn contains<T: Send + Sync + 'static>(&self) -> bool
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>());
sourcepub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T>
Immutably borrow a value that has been inserted into this TypeSet
.
sourcepub fn get_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T>
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");
sourcepub fn take<T: Send + Sync + 'static>(&mut self) -> Option<T>
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);
sourcepub fn get_or_insert<T: Send + Sync + 'static>(&mut self, default: T) -> &mut T
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");
sourcepub fn get_or_insert_with<T: Send + Sync + 'static>(
&mut self,
default: impl FnOnce() -> T,
) -> &mut T
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");
sourcepub fn get_or_insert_default<T: Default + Send + Sync + 'static>(
&mut self,
) -> &mut T
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);
sourcepub fn merge(&mut self, other: TypeSet)
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"));