pub struct Index<K, V, R: Rng = StdRng> { /* private fields */ }
Expand description
An index into a CommonCache
.
This should be used when an Entry
is not sufficient due to life time
problems. Note however that all indexes to a cache will be invalidated
whenever the cache is altered in any way. Including insertian of new
elements and promotion of existing elements. This is because the
index is just a pointer to a specific level and index in that level of the
cache. If some element is inserted or promoted, a few other elements will
randomly be moved down some levels in the cache, causing their indexes
to be invalid and potentially point to other items. However, this happens
randomly, so tests might not recognize such a bug, and it will not be
reproduceable.
The solution is to use an internal counter, (generation), which increments each time the cache is altered. Each index has the generation of the cache when the index was created, and if the index is used with a newer version of the cache it will be invalid.
Implementations§
Source§impl<K: Eq + Hash, V, R: Rng> Index<K, V, R>
impl<K: Eq + Hash, V, R: Rng> Index<K, V, R>
Sourcepub fn entry(self, cache: &mut CommonCache<K, V, R>) -> Entry<'_, K, V, R>
pub fn entry(self, cache: &mut CommonCache<K, V, R>) -> Entry<'_, K, V, R>
Get an entry corresponding to this index.
§Panics
Panics if the generation of the cache and this index differs, I.E that something has been inserted or promoted in the cache since this index was created.
Might also panic when trying to read the entry if the item corresponding to this index has been removed.
Sourcepub fn peek_key_value<'a>(
&'a self,
cache: &'a CommonCache<K, V, R>,
) -> (&'a K, &'a V)
pub fn peek_key_value<'a>( &'a self, cache: &'a CommonCache<K, V, R>, ) -> (&'a K, &'a V)
Read the key and value at the index without touching the rest of the cache. This operation will hence not be taken into account when considering which elements are most commonly used.
Sourcepub fn peek_key<'a>(&'a self, cache: &'a CommonCache<K, V, R>) -> &'a K
pub fn peek_key<'a>(&'a self, cache: &'a CommonCache<K, V, R>) -> &'a K
Silently read the key at this index.
Sourcepub fn peek_value<'a>(&'a self, cache: &'a CommonCache<K, V, R>) -> &'a V
pub fn peek_value<'a>(&'a self, cache: &'a CommonCache<K, V, R>) -> &'a V
Read the value at the index without touching the rest of the cache. This operation will hence not be taken into account when considering which elements are most commonly used.
Sourcepub fn peek_key_value_mut<'a>(
&'a self,
cache: &'a mut CommonCache<K, V, R>,
) -> (&'a K, &'a mut V)
pub fn peek_key_value_mut<'a>( &'a self, cache: &'a mut CommonCache<K, V, R>, ) -> (&'a K, &'a mut V)
Read the item at this index mutably without touching the rest of the cache. This operation will not be taken into account when considering which elements are most commonly used.
Note that this does not count as altering the cache so the index is still valid after this.
Sourcepub fn peek_value_mut<'a>(
&'a self,
cache: &'a mut CommonCache<K, V, R>,
) -> &'a mut V
pub fn peek_value_mut<'a>( &'a self, cache: &'a mut CommonCache<K, V, R>, ) -> &'a mut V
Read the value at this index mutably without touching the rest of the cache. This operation will not be taken into account when considering which elements are most commonly used.
Note that this does not count as altering the cache so the index is still valid after this.
Sourcepub fn get_key_value(self, cache: &mut CommonCache<K, V, R>) -> (&K, &mut V)
pub fn get_key_value(self, cache: &mut CommonCache<K, V, R>) -> (&K, &mut V)
Get the key and value at this index and promote the item to a higher level in the cache.
This function will promote the item to a higher level in the cache and based on some probability move other items down in the cache.
The index will be invalidated after this operation.
Sourcepub fn get_value(self, cache: &mut CommonCache<K, V, R>) -> &mut V
pub fn get_value(self, cache: &mut CommonCache<K, V, R>) -> &mut V
Get the value at this index and promote this index to a higher level in the cache.
This function will promote this index to a higher level in the cache and based on some probability move other items down in the cache.
Trait Implementations§
impl<K: Eq, V: Eq, R: Eq + Rng> Eq for Index<K, V, R>
impl<K, V, R: Rng> StructuralPartialEq for Index<K, V, R>
Auto Trait Implementations§
impl<K, V, R> Freeze for Index<K, V, R>
impl<K, V, R> RefUnwindSafe for Index<K, V, R>
impl<K, V, R> Send for Index<K, V, R>
impl<K, V, R> Sync for Index<K, V, R>
impl<K, V, R> Unpin for Index<K, V, R>
impl<K, V, R> UnwindSafe for Index<K, V, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.