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>
impl<'a, K, V> Entries<'a, K, V>
Sourcepub fn or_insert(self, default: V) -> Vec<V>
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);Sourcepub fn or_insert_with<F: FnOnce(&[K]) -> Vec<V>>(self, default: F) -> Vec<V>
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]);Sourcepub fn or_try_insert_with<F: FnOnce(&[K]) -> Result<Vec<V>, Box<dyn Error>>>(
self,
default: F,
) -> Result<Vec<V>, Box<dyn Error>>
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§
Auto Trait Implementations§
impl<'a, K, V> Freeze for Entries<'a, K, V>
impl<'a, K, V> RefUnwindSafe for Entries<'a, K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<'a, K, V> Send for Entries<'a, K, V>
impl<'a, K, V> Sync for Entries<'a, K, V>
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> 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
Mutably borrows from an owned value. Read more