Memoirs

Crates.ioMemoirs
lib.rsMemoirs
version0.0.1
sourcesrc
created_at2014-12-03 12:31:46.174321
updated_at2015-12-11 23:54:10.497015
descriptionMemoization functions for Rust
homepage
repositoryhttps://github.com/Inspiravetion/Memoirs
max_upload_size
id450
size1,121,092
(Inspiravetion)

documentation

README

Memoirs

Memoization Functions for Rust

#![feature(phase)]
#![feature(unboxed_closures)]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]

#[phase(plugin)]
extern crate memoirs_mac;
extern crate memoirs;

#[phase(plugin)]
extern crate lazy_static;

static mut count : int = 0;

//not thread safe
memoize!(
    fn double(i : int) -> int { 
        unsafe { count += 1 };
        2 * i
    }
)

//protected by std::mutex::Mutex
memoize_sync!(
    fn triple(i : int) -> int {
	unsafe { count += 1 };
	3 * i
    }
)

#[test]
fn basic_test() {    
    assert!(2 == double(1));
    assert!(4 == double(2));
    assert!(6 == double(3));

    assert!(unsafe { count } == 3);

    assert!(2 == double(1));
    assert!(4 == double(2));
    assert!(6 == double(3));

    assert!(unsafe { count } == 3);

    assert!(8 == double(4));
    assert!(8 == double(4));

    assert!(unsafe { count } == 4);
}
Commit count: 13

cargo fmt