]>
Commit | Line | Data |
---|---|---|
e74abb32 XL |
1 | // Extracted from the scopeguard crate |
2 | use core::ops::{Deref, DerefMut}; | |
3 | ||
4 | pub struct ScopeGuard<T, F> | |
5 | where | |
6 | F: FnMut(&mut T), | |
7 | { | |
8 | dropfn: F, | |
9 | value: T, | |
10 | } | |
11 | ||
12 | #[inline] | |
13 | pub fn guard<T, F>(value: T, dropfn: F) -> ScopeGuard<T, F> | |
14 | where | |
15 | F: FnMut(&mut T), | |
16 | { | |
17 | ScopeGuard { dropfn, value } | |
18 | } | |
19 | ||
20 | impl<T, F> Deref for ScopeGuard<T, F> | |
21 | where | |
22 | F: FnMut(&mut T), | |
23 | { | |
24 | type Target = T; | |
25 | #[inline] | |
26 | fn deref(&self) -> &T { | |
27 | &self.value | |
28 | } | |
29 | } | |
30 | ||
31 | impl<T, F> DerefMut for ScopeGuard<T, F> | |
32 | where | |
33 | F: FnMut(&mut T), | |
34 | { | |
35 | #[inline] | |
36 | fn deref_mut(&mut self) -> &mut T { | |
37 | &mut self.value | |
38 | } | |
39 | } | |
40 | ||
41 | impl<T, F> Drop for ScopeGuard<T, F> | |
42 | where | |
43 | F: FnMut(&mut T), | |
44 | { | |
45 | #[inline] | |
46 | fn drop(&mut self) { | |
47 | (self.dropfn)(&mut self.value) | |
48 | } | |
49 | } |