Trait core_memo::Memoize[][src]

pub trait Memoize {
    type Param: ?Sized;
    fn memoize(p: &Self::Param) -> Self;
}

Represents a computation that is to be memoized

To use this library, you should define a custom type representing the output of your computation and implement this trait on it to specify how to compute it. You then wrap your custom type in a Memo, MemoExt, or MemoOnce, depending on the semantics you need.

Notes

If you need more that one input parameter for your computation, you can use a tuple for the Param type or define a custom type for it.

If your computation does not use any input, Param can be () or !.

Param can also be an unsized type like str or [T] to work on slices.

Example

use core_memo::{Memoize, Memo};

#[derive(Debug, PartialEq, Eq)]
struct Repeater(String);

impl Memoize for Repeater {
    type Param = (String, usize);

    fn memoize(p: &Self::Param) -> Self {
        let (string, count) = p;
        let mut r = String::new();
        for _i in 0..*count {
            r.push_str(&string);
        }
        Repeater(r)
    }
}

let mut memo: Memo<Repeater, _> = Memo::new(("abc".into(), 3));

assert_eq!(memo.get().0, "abcabcabc");

Associated Types

Required Methods

Implementors