pub enum Entry<'a, T> {
Vacant(VacantEntry<'a, T>),
Occupied(OccupiedEntry<'a, T>),
}
Expand description
A view into a single type in the TypeSet
, which may be either vacant or occupied.
This type is constructed by TypeSet::entry
§Examples
This is a somewhat contrived example that demonstrates matching on the Entry
. Often,
Entry::or_insert
, Entry::or_insert_with
, and Entry::and_modify
can achieve
comparable results. See those functions for further usage examples.
use type_set::{TypeSet, entry::Entry};
let mut set = TypeSet::new().with("hello");
let (previous, current) = match set.entry::<&'static str>() {
Entry::Vacant(vacant_entry) => {
let current = vacant_entry.insert("entry was vacant");
(None, current)
}
Entry::Occupied(mut occupied_entry) => {
let previous = occupied_entry.insert("entry was occupied");
(Some(previous), occupied_entry.into_mut())
}
};
assert_eq!(previous, Some("hello"));
assert_eq!(*current, "entry was occupied");
Variants§
Vacant(VacantEntry<'a, T>)
A view into the location a T would be stored in the TypeSet
. See VacantEntry
Occupied(OccupiedEntry<'a, T>)
A view into the location a T is currently stored in the TypeSet
. See OccupiedEntry
Implementations§
source§impl<'a, T: Send + Sync + 'static> Entry<'a, T>
impl<'a, T: Send + Sync + 'static> Entry<'a, T>
sourcepub fn or_insert(self, default: T) -> &'a mut T
pub fn or_insert(self, default: T) -> &'a mut T
Ensures a value is in the Entry
by inserting the provided default
value if the Entry was
previously vacant. Returns a mutable reference to the value.
Prefer Entry::or_insert_with
if constructing a T is expensive.
§Example
let mut set = type_set::TypeSet::new();
assert_eq!(*set.entry().or_insert("hello"), "hello");
assert_eq!(set.get::<&'static str>(), Some(&"hello"));
assert_eq!(*set.entry().or_insert("world"), "hello");
assert_eq!(set.get::<&'static str>(), Some(&"hello"));
sourcepub fn or_insert_with(self, default: impl FnOnce() -> T) -> &'a mut T
pub fn or_insert_with(self, default: impl FnOnce() -> T) -> &'a mut T
Ensures a value is in the Entry
by inserting the provided value returned by the default
function if the Entry
was previously vacant. Returns a mutable reference to the value.
Prefer this to Entry::or_insert
if constructing a T is expensive.
§Example
let mut set = type_set::TypeSet::new();
assert_eq!(*set.entry().or_insert_with(|| String::from("hello")), "hello");
assert_eq!(set.get::<String>(), Some(&String::from("hello")));
assert_eq!(*set.entry::<String>().or_insert_with(|| panic!("never called")), "hello");
assert_eq!(set.get::<String>(), Some(&String::from("hello")));
sourcepub fn and_modify(self, f: impl FnOnce(&mut T)) -> Self
pub fn and_modify(self, f: impl FnOnce(&mut T)) -> Self
Provides in-place mutable access to an occupied entry before any potential inserts into the
set using Entry::or_insert
or Entry::or_insert_with
.
§Example
let mut set = type_set::TypeSet::new().with(String::from("hello"));
let value = set.entry::<String>()
.and_modify(|s| s.push_str(" world"))
.or_insert_with(|| String::from("greetings"));
assert_eq!(value, "hello world");
set.take::<String>();
let value = set.entry::<String>()
.and_modify(|s| s.push_str(" world"))
.or_insert_with(|| String::from("greetings"));
assert_eq!(value, "greetings");
sourcepub fn unwrap_occupied(self) -> OccupiedEntry<'a, T>
pub fn unwrap_occupied(self) -> OccupiedEntry<'a, T>
sourcepub fn unwrap_vacant(self) -> VacantEntry<'a, T>
pub fn unwrap_vacant(self) -> VacantEntry<'a, T>
sourcepub fn into_mut(self) -> Option<&'a mut T>
pub fn into_mut(self) -> Option<&'a mut T>
Returns a mutable reference to the contained type, if this entry is occupied
sourcepub fn into_occupied(self) -> Option<OccupiedEntry<'a, T>>
pub fn into_occupied(self) -> Option<OccupiedEntry<'a, T>>
Returns an OccupiedEntry
or None
if this entry is vacant.
sourcepub fn into_vacant(self) -> Option<VacantEntry<'a, T>>
pub fn into_vacant(self) -> Option<VacantEntry<'a, T>>
Returns a VacantEntry
or None
if this entry is occupied.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether this Entry
is a VacantEntry
sourcepub fn insert(self, value: T) -> Option<T>
pub fn insert(self, value: T) -> Option<T>
Insert a value into this Entry
.
If the Entry is already an OccupiedEntry
, the previously contained value will be
returned
source§impl<'a, T: Default + Send + Sync + 'static> Entry<'a, T>
impl<'a, T: Default + Send + Sync + 'static> Entry<'a, T>
sourcepub fn or_default(self) -> &'a mut T
pub fn or_default(self) -> &'a mut T
Ensures a value is in the Entry by inserting the default value if vacant, and returns a mutable reference to the value.
Equivalent to .or_insert_with(Default::default)
§Example
let mut set = type_set::TypeSet::new();
assert_eq!(*set.entry::<&'static str>().or_default(), "");
set.insert("hello");
assert_eq!(*set.entry::<&'static str>().or_default(), "hello");