[][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 fn call_cache(&mut self, arg: P) -> V[src]

calls the inner closure with the givem arg after init the cache if it did not before.

Panics

If the given closure panic the variable reading them from the MaybeUninit is guranteed to not drop and the Drop impl will do the job.

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

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

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

Trait Implementations

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

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.