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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![cfg_attr(nightly, feature(fnbox))]

use std::cell::RefCell;
use std::ops::Deref;
use std::mem::transmute;
use std::mem::replace;

#[cfg(nightly)]
use std::boxed::FnBox;

#[cfg(nightly)]
pub struct Lazy<T, F = Box<FnBox() -> T>> where F: FnOnce() -> T {
    store: RefCell<LazyStorage<T, F>>,
}

#[cfg(not(nightly))]
pub struct Lazy<T, F> where F: FnOnce() -> T {
    store: RefCell<LazyStorage<T, F>>,
}

enum LazyStorage<T, F> where F: FnOnce() -> T {
    Func(F),
    Stored(Option<T>),
}

impl<T, F> LazyStorage<T, F> where F: FnOnce() -> T {
    fn as_ref(&self) -> Option<&T> {
        match *self {
            LazyStorage::Stored(Some(ref t)) => Some(t),
            _ => None,
        }
    }

    fn consume(&mut self) -> Option<T> {
        match *self {
            LazyStorage::Stored(ref mut opt) => replace(opt, None),
            _ => None,
        }
    }
}

impl<T, F> Lazy<T, F> where F: FnOnce() -> T {
    pub fn new(f: F) -> Lazy<T, F> {
        Lazy {
            store: RefCell::new(LazyStorage::Func(f)),
        }
    }

    pub fn consume(self) -> T {
        self.consume_fn();

        self.store.borrow_mut().consume().unwrap()
    }

    fn consume_fn(&self) {
        use self::LazyStorage::*;

        if let Stored(_) = *self.store.borrow() {
            return;
        }

        let f = match replace(
            &mut *self.store.borrow_mut(),
            Stored(None)
        ) {
            Func(f) => f,
            _ => unreachable!(),
        };

        *self.store.borrow_mut() = Stored(Some(f()));
    }
}

impl<T, F> Deref for Lazy<T, F> where F: FnOnce() -> T {
    type Target = T;

    fn deref(&self) -> &T {
        self.consume_fn();
        unsafe { transmute(self.store.borrow().as_ref().unwrap()) }
    }
}