Lazy Evaluation for Rust.
Switch branches/tags
Nothing to show
Clone or download
reem Merge pull request #8 from huonw/less-static
Relax the 'static bound.
Latest commit 7dcf8b9 Mar 24, 2015
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Relax the 'static bound. Mar 24, 2015
tests Fixes for latest rust Mar 10, 2015
.gitignore (chore) Ignored Cargo.lock. Aug 9, 2014
.travis.yml (chore) Use language: rust for travis. Oct 30, 2014
Cargo.toml
README.md (doc) Remove old syntax from the README. Feb 25, 2015

README.md

Lazy

Lazy evaluation in Rust.

Example

fn expensive() -> i32 {
    println!("I am only evaluated once!"); 7
}

fn main() {
    let a = lazy!(expensive());

    // Thunks are just smart pointers!
    assert_eq!(*a, 7); // "I am only evaluated once." is printed here

    let b = [*a, *a]; // Nothing is printed.
    assert_eq!(b, [7, 7]);
}

API

lazy!($expr)

Expands to Thunk::new(|| { $expr })

Thunk::new(|| -> T)

Takes an FnOnce closure, creates a delayed computation.

Thunk::force()

Forces the evaluation of the thunk so subsequent accesses are cheap. Values are stored unboxed.

Thunk::unwrap()

Consumes and forces the evaluation of the thunk and returns the contained value.

Thunk::deref() / Thunk::deref_mut()

Gets the value out of the thunk by evaluating the closure or grabbing it from the cache. Allows you to call methods on the thunk as if it was an instance of the contained valued through auto-deref.

There is also an equivalent API for SyncThunk, which is Send + Sync and usable for safe, concurrent laziness, except that they are created using sync_lazy! or by doing use lazy::SyncThunk as Thunk and using lazy!.