]> git.proxmox.com Git - rustc.git/blame - vendor/hashbrown-0.9.1/src/scopeguard.rs
New upstream version 1.54.0+dfsg1
[rustc.git] / vendor / hashbrown-0.9.1 / src / scopeguard.rs
CommitLineData
6a06907d
XL
1// Extracted from the scopeguard crate
2use core::ops::{Deref, DerefMut};
3
4pub struct ScopeGuard<T, F>
5where
6 F: FnMut(&mut T),
7{
8 dropfn: F,
9 value: T,
10}
11
12#[cfg_attr(feature = "inline-more", inline)]
13pub fn guard<T, F>(value: T, dropfn: F) -> ScopeGuard<T, F>
14where
15 F: FnMut(&mut T),
16{
17 ScopeGuard { dropfn, value }
18}
19
20impl<T, F> Deref for ScopeGuard<T, F>
21where
22 F: FnMut(&mut T),
23{
24 type Target = T;
25 #[cfg_attr(feature = "inline-more", inline)]
26 fn deref(&self) -> &T {
27 &self.value
28 }
29}
30
31impl<T, F> DerefMut for ScopeGuard<T, F>
32where
33 F: FnMut(&mut T),
34{
35 #[cfg_attr(feature = "inline-more", inline)]
36 fn deref_mut(&mut self) -> &mut T {
37 &mut self.value
38 }
39}
40
41impl<T, F> Drop for ScopeGuard<T, F>
42where
43 F: FnMut(&mut T),
44{
45 #[cfg_attr(feature = "inline-more", inline)]
46 fn drop(&mut self) {
47 (self.dropfn)(&mut self.value)
48 }
49}