[−][src]Struct memoizer::Memoizer
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.
Methods
impl<U, V, F> Memoizer<U, V, F> where
U: Eq + Hash + Clone,
V: Clone,
F: Fn(U) -> V,
[src]
U: Eq + Hash + Clone,
V: Clone,
F: Fn(U) -> V,
pub fn new(function: F) -> Memoizer<U, V, F>
[src]
Creates a new Memoize given a function.
Examples
let mut add_two = Memoizer::new(|n| { n + 2 }); assert_eq!(4, add_two.value(2));
pub fn value(&mut self, arg: U) -> V
[src]
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
impl<U: Debug, V: Debug, F: Debug> Debug for Memoizer<U, V, F> where
U: Eq + Hash + Clone,
V: Clone,
F: Fn(U) -> V,
[src]
U: Eq + Hash + Clone,
V: Clone,
F: Fn(U) -> V,
Auto Trait Implementations
impl<U, V, F> RefUnwindSafe for Memoizer<U, V, F> where
F: RefUnwindSafe,
U: RefUnwindSafe,
V: RefUnwindSafe,
F: RefUnwindSafe,
U: RefUnwindSafe,
V: RefUnwindSafe,
impl<U, V, F> Send for Memoizer<U, V, F> where
F: Send,
U: Send,
V: Send,
F: Send,
U: Send,
V: Send,
impl<U, V, F> Sync for Memoizer<U, V, F> where
F: Sync,
U: Sync,
V: Sync,
F: Sync,
U: Sync,
V: Sync,
impl<U, V, F> Unpin for Memoizer<U, V, F> where
F: Unpin,
U: Unpin,
V: Unpin,
F: Unpin,
U: Unpin,
V: Unpin,
impl<U, V, F> UnwindSafe for Memoizer<U, V, F> where
F: UnwindSafe,
U: UnwindSafe,
V: UnwindSafe,
F: UnwindSafe,
U: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,