[][src]Struct high_mem_utils::LazyCache

pub struct LazyCache<P: Ord, V: Clone, C: Fn(P) -> V> {
    pub closure: C,
    // some fields omitted
}

A lazy-iniatialiazed cache for a Fn closure with a constant constructor.

Fields

closure: C

Methods

impl<P: Ord + Clone, V: Clone, C: Fn(P) -> V> LazyCache<P, V, C>[src]

pub const fn new(closure: C) -> Self[src]

Construct a cache for a closure that is initialized in the first call to call_cache.

This is particularly useful for fn pointers of recurrently used functions because it can be used on statics,althought you need make them mutable for actually call call_cache.

pub const fn with_cache(cache: BTreeMap<P, V>, closure: C) -> Self[src]

Construct a cache for a closure providing an iniatialized cache.

pub const unsafe fn with_cache_unchecked(
    cache: MaybeUninit<BTreeMap<P, V>>,
    closure: C,
    init: bool
) -> Self
[src]

Cosntruct a cache for a closure providing a maybe unininitialized one along with the init state.

If you do know that init is true the with_cache is preferred.

Safety

This function is unsafe due to being unable to prove that the init value is correct and true only for initialiazed caches.

Considering that a bad use can lead to try to read or destruct uninitializated memory the use of this function is discouraged and it only exist to give flexibility while maintaining the fields private.

pub fn call_cache(&mut self, arg: P) -> V[src]

Calls the inner closure with the given arg,after initializing the cache if it did not before.

Examples

use high_mem_utils::LazyCache;
 
fn foo(num: u32) -> u32 {
    num
}
 
let mut cache = LazyCache::new(foo);
 
assert_eq!(cache.call_cache(2), 2);
assert_eq!(cache.call_cache(2), 2);
assert_eq!(cache.call_cache(4), 4);

pub fn pop(&mut self, arg: &P) -> Option<V>[src]

This method removes an argument from the cache and returns the value or None if there's no one or the cache is uninitialized.

Examples

use high_mem_utils::LazyCache;
 
fn foo(num: u32) -> u32 {
    num
}
 
let mut cache = LazyCache::new(foo);
 
assert_eq!(cache.call_cache(2), 2);
assert_eq!(cache.pop(&2), Some(2));
assert_eq!(cache.pop(&2), None);

pub fn clear(&mut self)[src]

Clears the cache,removing all their values.This is no-op if the cache is not initialized.

pub fn is_init(&self) -> bool[src]

Returns true if the cache is initialized,not neccesarily filled with an argument. Use !is_empty for that purpose.

pub fn is_empty(&self) -> bool[src]

Returns true if the cache has no arguments or if it's not initialized.

pub fn cache<F: Fn(&mut BTreeMap<P, V>)>(&mut self, f: F)[src]

This method calls a closure that receives a mutable reference to the cache,allowing using methods provided by BTreeMap.

Examples

use high_mem_utils::LazyCache;
 
let mut cache = LazyCache::new(|x| x);
 
cache.cache(|c| {c.insert(2, 2);});
assert_eq!(cache.pop(&2), Some(2));
assert_eq!(cache.call_cache(2), 2);
assert_eq!(cache.pop(&2), Some(2));
assert_eq!(cache.pop(&2), None);

Trait Implementations

impl<P: Ord, V: Clone, C: Fn(P) -> V> Drop for LazyCache<P, V, C>[src]

This impl drops the cache only if it's initialiazed.

Auto Trait Implementations

impl<P, V, C> RefUnwindSafe for LazyCache<P, V, C> where
    C: RefUnwindSafe,
    P: RefUnwindSafe,
    V: RefUnwindSafe

impl<P, V, C> Send for LazyCache<P, V, C> where
    C: Send,
    P: Send,
    V: Send

impl<P, V, C> Sync for LazyCache<P, V, C> where
    C: Sync,
    P: Sync,
    V: Sync

impl<P, V, C> Unpin for LazyCache<P, V, C> where
    C: Unpin,
    P: Unpin,
    V: Unpin

impl<P, V, C> UnwindSafe for LazyCache<P, V, C> where
    C: UnwindSafe,
    P: RefUnwindSafe + UnwindSafe,
    V: RefUnwindSafe + UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.