Giter Site home page Giter Site logo

rust-lazy's Introduction

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!.

rust-lazy's People

Contributors

andresilva avatar huonw avatar reem avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.