Trait lazy_st::Evaluate

source ·
pub trait Evaluate<T> {
    // Required method
    fn evaluate(self) -> T;
}
Expand description

Generalisation of lazy evaluation to other types than closures.

The main use case for implementing this trait is the following: Let us suppose that you construct a large number of lazy values using only one function f with different values x1, …, xn of type T, i.e. lazy!(f(x1)), …, lazy!(f(xn)). In this case, you may consider implementing Evaluate for T such that evaluate(x) yields f(x). This allows you to use Thunk::new(x) instead of lazy!(f(x)), saving time and space because any such Thunk will contain only x instead of both f and x.

Let us look at an example:

struct User(usize);

impl Evaluate<String> for User {
    fn evaluate(self) -> String {
        format!("User no. {}", self.0)
    }
}

let root = Thunk::new(User(0));
let mere_mortal = Thunk::evaluated(String::from("Someone else"));
let user = if true { root } else { mere_mortal };
assert_eq!(*user, "User no. 0");

Note that this trait is quite similar to the Into trait. Unfortunately, it seems that we cannot use Into here, because we cannot implement it for instances of FnOnce, which is necessary for Lazy.

Required Methods§

source

fn evaluate(self) -> T

Implementors§

source§

impl<A: FnOnce() -> B, B> Evaluate<B> for A