Struct Memoizer

Source
pub struct Memoizer<U, V, F>
where U: Eq + Hash + Clone, V: Clone, F: Fn(U) -> V,
{ /* private fields */ }
Expand description

The eponymous struct. Can only memoize function that takes a single argument and returns a single value, if you need more than this, you can use vectors, arrays or structs of your own to pass in more than one value.

Implementations§

Source§

impl<U, V, F> Memoizer<U, V, F>
where U: Eq + Hash + Clone, V: Clone, F: Fn(U) -> V,

Source

pub fn new(function: F) -> Memoizer<U, V, F>

Creates a new Memoize given a function.

§Examples
let mut add_two = Memoizer::new(|n| {
   	n + 2
   });
assert_eq!(4, add_two.value(2));
Source

pub fn value(&mut self, arg: U) -> V

Returns the value for the memoized function. If the function has already been called before, it will use the previous value. This means Memoizer should only be used for injective functions.

§Examples

#[derive(Debug, Clone, Hash)]
    struct Dummy {
        pub id: usize,
        pub word: String,
    }

    /* PartialEq & Eq required for HashMap */
    impl PartialEq for Dummy {
        fn eq(&self, other: &Dummy) -> bool {
            self.id == other.id && self.word == other.word
        }
    }

   impl Eq for Dummy {}

let d = Dummy {
    id: 1,
    word: String::from("girls"),
};
let mut calc = Memoizer::new(|d: &Dummy| d.id + d.word.len());

 assert_eq!(6, calc.value(&d));
 assert_eq!(6, calc.value(&d));

Trait Implementations§

Source§

impl<U, V, F> Debug for Memoizer<U, V, F>
where U: Eq + Hash + Clone + Debug, V: Clone + Debug, F: Fn(U) -> V + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<U, V, F> Freeze for Memoizer<U, V, F>
where F: Freeze,

§

impl<U, V, F> RefUnwindSafe for Memoizer<U, V, F>

§

impl<U, V, F> Send for Memoizer<U, V, F>
where F: Send, U: Send, V: Send,

§

impl<U, V, F> Sync for Memoizer<U, V, F>
where F: Sync, U: Sync, V: Sync,

§

impl<U, V, F> Unpin for Memoizer<U, V, F>
where F: Unpin, U: Unpin, V: Unpin,

§

impl<U, V, F> UnwindSafe for Memoizer<U, V, F>
where F: UnwindSafe, U: UnwindSafe, V: 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.