core_memo 0.1.0

Zero-cost, no_std-compatible library for lazy evaluation and memoization
Documentation
  • Coverage
  • 93.75%
    30 out of 32 items documented4 out of 31 items with examples
  • Size
  • Source code size: 35.61 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.71 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • jamadazi/core_memo
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • inodentry

Simple Memoization Library

core_memo is a simple, straightforward, zero-cost Rust library for lazy evaluation and memoization. It does not do memory allocations or dynamic dispatch and it is #![no_std]-compatible. It has no dependencies.

See the docs for more info!

Example

use core_memo::{Memoize, Memo, MemoExt};

#[derive(Debug, PartialEq, Eq)] // for assert_eq! later
struct MemoSum(i32);

impl Memoize for MemoSum {
    type Param = [i32];

    fn memoize(p: &[i32]) -> MemoSum {
        MemoSum(p.iter().sum())
    }
}


// The `Memo` type holds ownership over the parameter for the calculation
// There are also the `MemoExt` and `MemoOnce` types with different semantics

let mut memo: Memo<MemoSum, _> = Memo::new(vec![1, 2]);

// Our `memoize` method is called the first time we call `memo.get()`
assert_eq!(memo.get(), &MemoSum(3));

// Further calls to `memo.get()` return the cached value without reevaluating
assert_eq!(memo.get(), &MemoSum(3));


// We can mutate the parameter held inside the `Memo`:

// via a mutable reference
memo.param_mut().push(3);
// via a closure
memo.update_param(|p| p.push(4));

// either way, the `Memo` forgets any cached value and it will be
// reevaluated on the next call to `memo.get()`

assert_eq!(memo.get(), &MemoSum(10)); // the vec is now `[1, 2, 3, 4]`