Struct CachedFunction

Source
pub struct CachedFunction<TFunction, TInput, TReturn>
where TFunction: Fn(TInput) -> TReturn, TInput: Eq + Hash + Copy, TReturn: Copy,
{ /* private fields */ }
Expand description

Wraps an immutable function such that the results of its invocation are cached to a HashMap<TInput, TReturn>.

Implementations§

Source§

impl<TFunction, TInput, TReturn> CachedFunction<TFunction, TInput, TReturn>
where TFunction: Fn(TInput) -> TReturn, TInput: Eq + Hash + Copy, TReturn: Copy,

Source

pub fn new(function: TFunction) -> CachedFunction<TFunction, TInput, TReturn>

Returns a CachedFunction<TFunction, TInput, TReturn> that wraps the input function.

§Arguments
  • function - The function to wrap in a cache.
§Examples
use std::thread;
use std::time::Duration;
use function_cache::CachedFunction;
 
let mut cached_function = CachedFunction::new(|x: i32| {
    thread::sleep(Duration::from_secs(2));
    x
});
Source

pub fn value(&mut self, arg: TInput) -> TReturn

Gets the value of evaluating the function with the arg as input. First, the cache is checked and if the value with arg has already be computed, it will be returned from the cache.

§Arguments
  • arg - The argument to the function call.

Auto Trait Implementations§

§

impl<TFunction, TInput, TReturn> Freeze for CachedFunction<TFunction, TInput, TReturn>
where TFunction: Freeze,

§

impl<TFunction, TInput, TReturn> RefUnwindSafe for CachedFunction<TFunction, TInput, TReturn>
where TFunction: RefUnwindSafe, TInput: RefUnwindSafe, TReturn: RefUnwindSafe,

§

impl<TFunction, TInput, TReturn> Send for CachedFunction<TFunction, TInput, TReturn>
where TFunction: Send, TInput: Send, TReturn: Send,

§

impl<TFunction, TInput, TReturn> Sync for CachedFunction<TFunction, TInput, TReturn>
where TFunction: Sync, TInput: Sync, TReturn: Sync,

§

impl<TFunction, TInput, TReturn> Unpin for CachedFunction<TFunction, TInput, TReturn>
where TFunction: Unpin, TInput: Unpin, TReturn: Unpin,

§

impl<TFunction, TInput, TReturn> UnwindSafe for CachedFunction<TFunction, TInput, TReturn>
where TFunction: UnwindSafe, TInput: UnwindSafe, TReturn: UnwindSafe,

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.