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