Index

Struct Index 

Source
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>

Source

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.

Source

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.

Source

pub fn peek_key<'a>(&'a self, cache: &'a CommonCache<K, V, R>) -> &'a K

Silently read the key at this index.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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§

Source§

impl<K: Clone, V: Clone, R: Clone + Rng> Clone for Index<K, V, R>

Source§

fn clone(&self) -> Index<K, V, R>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug, R: Debug + Rng> Debug for Index<K, V, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Hash, V: Hash, R: Hash + Rng> Hash for Index<K, V, R>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<K: PartialEq, V: PartialEq, R: PartialEq + Rng> PartialEq for Index<K, V, R>

Source§

fn eq(&self, other: &Index<K, V, R>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: Eq, V: Eq, R: Eq + Rng> Eq for Index<K, V, R>

Source§

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>
where K: Send, V: Send, R: Send,

§

impl<K, V, R> Sync for Index<K, V, R>
where K: Sync, V: Sync, R: Sync,

§

impl<K, V, R> Unpin for Index<K, V, R>
where K: Unpin, V: Unpin, R: Unpin,

§

impl<K, V, R> UnwindSafe for Index<K, V, R>
where K: UnwindSafe, V: UnwindSafe, R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V