Entry

Enum Entry 

Source
pub enum Entry<'a, K: 'a, V: 'a> {
    Occupied(OccupiedEntry<K, V>),
    Vacant(VacantEntry<'a, K, V>),
}
Expand description

A view into a single entry in a cache, which may either be vacant or occupied.

This enum is constructed from the entry method on Cache.

Variants§

§

Occupied(OccupiedEntry<K, V>)

An occupied entry.

§

Vacant(VacantEntry<'a, K, V>)

A vacant entry.

Implementations§

Source§

impl<'a, K, V> Entry<'a, K, V>
where K: Hash + Eq + Copy, V: Clone + Copy,

Source

pub fn or_insert(self, default: V) -> V

Ensures a value is in the entry by inserting the default if empty, and returns a copy of the value in the entry.

Examples

use cache_cache::Cache;

let mut target_positions = Cache::keep_last();

target_positions.entry(10).or_insert(0);
assert_eq!(target_positions[&10], 0);
Source

pub fn or_insert_with<F: FnOnce(&K) -> V>(self, default: F) -> V

Ensures a value is in the entry by inserting the result of the default function if empty, and returns a copy of the value in the entry.

In contrary to HashMap API, the default function takes the key as argument. As shown in the example, it makes the IO call simpler. It does require the key to implement the Copy trait though.

Examples

use cache_cache::Cache;

let mut torque_enable: Cache<u8, bool> = Cache::keep_last();

torque_enable.entry(20).or_insert_with(|id| false);
assert_eq!(torque_enable[&20], false);
Source

pub fn or_try_insert_with<F: FnOnce(&K) -> Result<V, Box<dyn Error>>>( self, default: F, ) -> Result<V, Box<dyn Error>>

Tries inserting a value in the entry (if empty) with the default function and returns a Result of the value in the entry or the error encounter by the default function.

Examples

use cache_cache::Cache;
use std::{error::Error, fmt};

#[derive(Debug)]
struct MyDummyIOError;
impl fmt::Display for MyDummyIOError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         write!(f, "my io error")
    }
}
impl Error for MyDummyIOError {}

fn enable_torque(id: &u8) -> Result<bool, Box<dyn Error>> {
    // Send hardware command that could fail, something like
    // serial_send_torque_on_command(...)?;

    // For example purposes, we suppose here that our method:
    // * will work for id within 0...10
    // * fail for other
    if *id > 10 {
        Err(Box::new(MyDummyIOError))
    }
    else {
        Ok(true)
    }
}

let mut torque_enable = Cache::keep_last();

let res = torque_enable.entry(5).or_try_insert_with(enable_torque);
assert!(res.is_ok());
assert_eq!(res.unwrap(), true);

let res = torque_enable.entry(20).or_try_insert_with(enable_torque);
assert!(res.is_err());
Source

pub fn key(&self) -> &K

Returns a reference to this entry’s key.

Examples

use cache_cache::Cache;

let mut cache: Cache<&str, u32> = Cache::keep_last();
assert_eq!(cache.entry("speed").key(), &"speed");

Trait Implementations§

Source§

impl<'a, K: Debug + 'a, V: Debug + 'a> Debug for Entry<'a, K, V>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, K, V> Freeze for Entry<'a, K, V>
where K: Freeze, V: Freeze,

§

impl<'a, K, V> RefUnwindSafe for Entry<'a, K, V>

§

impl<'a, K, V> Send for Entry<'a, K, V>
where K: Send, V: Send,

§

impl<'a, K, V> Sync for Entry<'a, K, V>
where K: Sync, V: Sync,

§

impl<'a, K, V> Unpin for Entry<'a, K, V>
where K: Unpin, V: Unpin,

§

impl<'a, K, V> !UnwindSafe for Entry<'a, K, V>

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> 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, 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.