cl_aux/structures/
auto_clear.rs

1use crate::Clear;
2use core::{
3  borrow::{Borrow, BorrowMut},
4  ops::{Deref, DerefMut},
5};
6
7/// Any mutable item wrapped in this structure is automatically cleaned when dropped.
8#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
9pub struct AutoClear<T>(T)
10where
11  T: Clear;
12
13impl<T> AsMut<T> for AutoClear<T>
14where
15  T: Clear,
16{
17  #[inline]
18  fn as_mut(&mut self) -> &mut T {
19    self
20  }
21}
22
23impl<T> AsRef<T> for AutoClear<T>
24where
25  T: Clear,
26{
27  #[inline]
28  fn as_ref(&self) -> &T {
29    self
30  }
31}
32
33impl<T> Borrow<T> for AutoClear<T>
34where
35  T: Clear,
36{
37  #[inline]
38  fn borrow(&self) -> &T {
39    self
40  }
41}
42
43impl<T> BorrowMut<T> for AutoClear<T>
44where
45  T: Clear,
46{
47  #[inline]
48  fn borrow_mut(&mut self) -> &mut T {
49    self
50  }
51}
52
53impl<T> Deref for AutoClear<T>
54where
55  T: Clear,
56{
57  type Target = T;
58
59  #[inline]
60  fn deref(&self) -> &Self::Target {
61    &self.0
62  }
63}
64
65impl<T> DerefMut for AutoClear<T>
66where
67  T: Clear,
68{
69  #[inline]
70  fn deref_mut(&mut self) -> &mut Self::Target {
71    &mut self.0
72  }
73}
74
75impl<T> Drop for AutoClear<T>
76where
77  T: Clear,
78{
79  #[inline]
80  fn drop(&mut self) {
81    self.0.clear();
82  }
83}
84
85impl<T> From<T> for AutoClear<T>
86where
87  T: Clear,
88{
89  #[inline]
90  fn from(from: T) -> Self {
91    Self(from)
92  }
93}