blob: 03a334321412ea414e4c4f7df0c2ba0431a908c1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/// Drop guard that executes the provided callback function when dropped.
pub struct OnDrop<F = Box<dyn FnOnce() + Send>>
where
F: FnOnce() + Send,
{
callback: Option<F>,
}
impl<F: FnOnce() + Send> Drop for OnDrop<F> {
fn drop(&mut self) {
if let Some(callback) = self.callback.take() {
callback();
}
}
}
impl<F: FnOnce() + Send> OnDrop<F> {
pub fn new(callback: F) -> Self {
Self {
callback: Some(callback),
}
}
}
|