1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Caching of a single value that can be obtained by evaluating a closure. The closure
//! presumably represents an expensive operation, the result of which never changes.

/// Lazily evaluated value supplied by a closure.
pub struct Lazy<F, T>(State<F, T>) where F: FnOnce() -> T;

impl<F, T> Lazy<F, T> where F: FnOnce() -> T {
    /// Creates a new [`Lazy`] backed by the given closure.
    pub fn new(f: F) -> Self {
        Self(State::Uninit(Some(f)))
    }

    /// Obtains the memoized value, evaluating the closure if unset.
    pub fn get(&mut self) -> &T {
        if let State::Uninit(f) = &mut self.0 {
            let val = f.take().expect("cannot be uninitialised")();
            self.0 = State::Init(val);
        }

        self.try_get().expect("cannot be uninitialised")
    }

    /// Tries to obtain the memoized value if it is set. The closure is not evaluated
    /// if unset.
    pub fn try_get(&self) -> Option<&T> {
        match &self.0 {
            State::Uninit(_) => None,
            State::Init(val) => Some(val)
        }
    }
}

enum State<F, T> where F: FnOnce() -> T {
    Uninit(Option<F>),
    Init(T)
}

#[cfg(test)]
mod tests;