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 with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create an empty TypeSet with space for at least capacity distinct types.
Preallocating avoids the incremental reallocations that occur as types are inserted, which is
worthwhile when the number of types is known ahead of time or when reusing a TypeSet across
many fill/clear cycles.
§Example
let mut set = type_set::TypeSet::with_capacity(2);
set.insert("hello");
set.insert(1usize);
assert_eq!(set.get::<&'static str>(), Some(&"hello"));Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Remove all types from this TypeSet, retaining the allocated capacity for reuse.
This is the cheap way to reuse a TypeSet allocation: clearing and refilling avoids
reallocating the backing storage that constructing a fresh TypeSet would require.
§Example
let mut set = type_set::TypeSet::new().with("hello").with(1usize);
set.clear();
assert!(set.is_empty());
assert_eq!(set.get::<&'static str>(), None);Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserve capacity for at least additional more distinct types.
§Example
let mut set = type_set::TypeSet::new();
set.reserve(4);
set.insert("hello");Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink the capacity of this TypeSet as much as possible.
§Example
let mut set = type_set::TypeSet::with_capacity(16);
set.insert("hello");
set.shrink_to_fit();
assert_eq!(set.get::<&'static str>(), Some(&"hello"));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"));