Skip to content

Instantly share code, notes, and snippets.

@sdleffler
Created May 28, 2017 22:38
Show Gist options
  • Save sdleffler/bd93ff6eedbbbebf9c3cbf684b2a60e0 to your computer and use it in GitHub Desktop.
Save sdleffler/bd93ff6eedbbbebf9c3cbf684b2a60e0 to your computer and use it in GitHub Desktop.
impl<T> LazyRef for Thunk<T> {
#[inline]
fn defer<'a, F: FnBox() -> T + 'a>(f: F) -> Thunk<T>
where T: 'a
{
let thunk =
unsafe { Box::from_raw(Box::into_raw(Box::new(f)) as *mut (FnBox() -> T + 'static)) };
Thunk {
flag: Cell::new(Flag::Deferred),
data: UnsafeCell::new(Cache { deferred: thunk }),
}
}
#[inline]
fn force(&self) {
match self.flag.get() {
Flag::Deferred => {
unsafe {
(*self.data.get()).evaluate_thunk();
}
self.flag.set(Flag::Evaluated);
}
Flag::Evaluated => {}
Flag::Empty => unsafe { unreachable() },
}
}
}
error[E0310]: the parameter type `F` may not live long enough
--> src/unsync.rs:144:36
|
144 | unsafe { Box::from_raw(Box::into_raw(Box::new(f)) as *mut (FnBox() -> T + 'static)) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `F: 'static`...
note: ...so that the type `F` will meet its required lifetime bounds
--> src/unsync.rs:144:36
|
144 | unsafe { Box::from_raw(Box::into_raw(Box::new(f)) as *mut (FnBox() -> T + 'static)) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment