Entries

Struct Entries 

Source
pub struct Entries<'a, K: 'a, V: 'a> { /* private fields */ }
Expand description

A view into multiple Entry in a cache.

Implementations§

Source§

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

Source

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

Ensures a value is in the entries by inserting the default if empty, and returns the value in the entries.

Examples

use cache_cache::Cache;

let mut target_positions = Cache::keep_last();

target_positions.insert(11, 90);

target_positions.entries(&[10, 11, 12]).or_insert(0);
assert_eq!(target_positions[&10], 0);
assert_eq!(target_positions[&11], 90);
assert_eq!(target_positions[&12], 0);
Source

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

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

In contrary to HashMap API, the default function takes the missing keys 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;
use std::{error::Error, time::Duration};

let mut present_position = Cache::with_expiry_duration(Duration::from_millis(10));

present_position.insert(10, 0.0);

let pos = present_position
    .entries(&[10, 11, 12])
    .or_insert_with(|ids| ids.iter().map(|&id| id as f64 * 10.0).collect());

assert_eq!(pos, vec![0.0, 110.0, 120.0]);
Source

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

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

Examples

use cache_cache::Cache;
use std::{error::Error, time::Duration};

fn get_position(ids: &[u8]) -> Result<Vec<f64>, Box<dyn Error>> {
    // For simplicity, this function always work.
    // But it's a mockup for a real world scenario where hardware IO can fail.
    Ok(ids.iter().map(|&id| id as f64 * 10.0).collect())
}

let mut present_position = Cache::with_expiry_duration(Duration::from_millis(10));

present_position.insert(10, 0.0);

let pos = present_position
    .entries(&[10, 11, 12])
    .or_try_insert_with(get_position);

assert!(pos.is_ok());
assert_eq!(pos.unwrap(), vec![0.0, 110.0, 120.0]);

Trait Implementations§

Source§

impl<'a, K: Debug + 'a, V: Debug + 'a> Debug for Entries<'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 Entries<'a, K, V>

§

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

§

impl<'a, K, V> Send for Entries<'a, K, V>
where K: Sync + Send, V: Send,

§

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

§

impl<'a, K, V> Unpin for Entries<'a, K, V>

§

impl<'a, K, V> !UnwindSafe for Entries<'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.