Skip to content

Instantly share code, notes, and snippets.

@rrichardson
Created December 14, 2014 18:11
Show Gist options
  • Save rrichardson/fc15e8ce371619f45c80 to your computer and use it in GitHub Desktop.
Save rrichardson/fc15e8ce371619f45c80 to your computer and use it in GitHub Desktop.
pub struct Destructed<'a, T> {
fun: Box<for <'b> FnOnce<(&'b T,), ()> + 'a>,
active: bool,
val: T
}
impl<'a, T> Destructed<'a, T> {
pub fn new(val: T, f: Box<for <'b> FnOnce<(&'b T,), ()> + 'a>) -> Destructed<'a, T> {
Destructed { val: val, fun: f, active: true }
}
pub fn inner(&'a self) -> &'a T {
&self.val
}
pub fn release(&mut self) {
self.active = false;
}
}
#[unsafe_destructor]
impl<'a, T> Drop for Destructed<'a, T> {
fn drop(&mut self) {
if self.active {
(*self.fun).call_once((&self.val,));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment