pub enum Entry<'a, K, V, P, H>where
P: ExpirePolicy,{
Occupied(OccupiedEntry<'a, K, V, P, H>),
Vacant(VacantEntry<'a, K, V, P, H>),
}Expand description
Variants§
Occupied(OccupiedEntry<'a, K, V, P, H>)
Vacant(VacantEntry<'a, K, V, P, H>)
Implementations§
Source§impl<'a, K, V, P, H> Entry<'a, K, V, P, H>
impl<'a, K, V, P, H> Entry<'a, K, V, P, H>
Sourcepub fn insert(self, value: V, init: P::Info) -> OccupiedEntry<'a, K, V, P, H>
pub fn insert(self, value: V, init: P::Info) -> OccupiedEntry<'a, K, V, P, H>
Sets the value of the entry, and returns an OccupiedEntry.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
let entry = cache.entry(0).insert("a", ());
assert_eq!(entry.key(), &0);
assert_eq!(entry.get(), &"a");
assert_eq!(cache.get(&0), Some(&"a"));Sourcepub fn or_insert(self, default: V, init: P::Info) -> &'a mut V
pub fn or_insert(self, default: V, init: P::Info) -> &'a mut V
Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.entry(0).or_insert(0, ());
assert_eq!(cache.get(&0), Some(&0));
*cache.entry(0).or_insert(200, ()) += 10;
assert_eq!(cache.get(&0), Some(&10));
sleep(Duration::from_millis(10));
cache.entry(0).or_insert(100, ());
assert_eq!(cache.get(&0), Some(&100));Sourcepub fn or_insert_with<F: FnOnce() -> V>(
self,
default: F,
init: P::Info,
) -> &'a mut V
pub fn or_insert_with<F: FnOnce() -> V>( self, default: F, init: P::Info, ) -> &'a mut V
Ensures a value is in the entry by inserting the result of the default function if empty, and returns a mutable reference to the value in the entry.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::thread::sleep;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.entry("a").or_insert_with(|| "A".to_string(), ());
assert_eq!(cache.get(&"a"), Some(&"A".to_string()));
cache
.entry("a")
.or_insert_with(|| "B".to_string(), ())
.push('B');
assert_eq!(cache.get(&"a"), Some(&"AB".to_string()));
sleep(Duration::from_millis(10));
cache.entry("a").or_insert_with(|| "C".to_string(), ());
assert_eq!(cache.get(&"a"), Some(&"C".to_string()));Sourcepub fn or_insert_with_key<F: FnOnce(&K) -> V>(
self,
default: F,
init: P::Info,
) -> &'a mut V
pub fn or_insert_with_key<F: FnOnce(&K) -> V>( self, default: F, init: P::Info, ) -> &'a mut V
Ensures a value is in the entry by inserting, if empty, the result of the default function. This method allows for generating key-derived values for insertion by providing the default function a reference to the key that was moved during the .entry(key) method call.
The reference to the moved key is provided so that cloning or copying the key is unnecessary, unlike with .or_insert_with(|| … ).
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache
.entry("endorphin")
.or_insert_with_key(|k| k.chars().count(), ());
assert_eq!(cache.get(&"endorphin"), Some(&9));Sourcepub fn key(&self) -> &K
pub fn key(&self) -> &K
Returns a reference to this entry’s key.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache =
HashMap::<&str, u32, _>::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
assert_eq!(cache.entry("library").key(), &"library");Sourcepub fn and_modify<F>(self, f: F) -> Self
pub fn and_modify<F>(self, f: F) -> Self
Provides in-place mutable access to an occupied entry before any potential inserts into the HashMap.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache
.entry("endorphin")
.and_modify(|v| unreachable!())
.or_insert(0, ());
assert_eq!(cache.get(&"endorphin"), Some(&0));
cache
.entry("endorphin")
.and_modify(|v| *v += 10)
.or_insert(123, ());
assert_eq!(cache.get(&"endorphin"), Some(&10));Sourcepub fn and_replace_entry_with<F>(self, f: F) -> Self
pub fn and_replace_entry_with<F>(self, f: F) -> Self
Provides shared access to the key and owned access to the value of an occupied entry and allows to replace or remove it based on the value of the returned option.
§Example
use endorphin::map::Entry;
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
match cache
.entry(10)
.and_replace_entry_with(|_k, _v| unreachable!())
{
Entry::Occupied(_) => unreachable!(),
Entry::Vacant(entry) => assert_eq!(entry.key(), &10),
}
cache.insert(10, 200, ());
match cache.entry(10).and_replace_entry_with(|k, v| Some(k + v)) {
Entry::Occupied(entry) => assert_eq!(entry.get(), &210),
Entry::Vacant(_) => unreachable!(),
}Source§impl<'a, K, V, P, H> Entry<'a, K, V, P, H>
impl<'a, K, V, P, H> Entry<'a, K, V, P, H>
Sourcepub fn or_default(self, init: P::Info) -> &'a mut Vwhere
V: Default,
pub fn or_default(self, init: P::Info) -> &'a mut Vwhere
V: Default,
Ensures a value is in the entry by inserting the default value if empty, and returns a mutable reference to the value in the entry.
§Example
use endorphin::policy::LazyFixedTTLPolicy;
use endorphin::HashMap;
use std::time::Duration;
let mut cache = HashMap::<_, u32, _>::new(LazyFixedTTLPolicy::new(Duration::from_millis(10)));
cache.entry("endorphin").or_default(());
assert_eq!(cache.get(&"endorphin"), Some(&u32::default()));