Created
December 14, 2014 18:11
-
-
Save rrichardson/fc15e8ce371619f45c80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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