Skip to main content

buffa/view/
guarded.rs

1// Modified by ReallyMe Technologies Limited.
2//! View containers that own cleanup, without changing ordinary view drop checking.
3use crate::sensitive::{view_storage, Zeroize, ZeroizeOnDrop};
4use alloc::vec::Vec;
5use core::ops::Deref;
6
7/// A decoded repeated view that wipes live and outgrown collection storage.
8/// Borrowed elements retain their caller-owned input lifetime.
9#[derive(Clone, PartialEq, Eq)]
10pub struct RepeatedView<'a, T>(super::RepeatedView<'a, T>);
11impl<'a, T> RepeatedView<'a, T> {
12    /// Adopt an existing allocation without shrinking it.
13    pub fn new(values: Vec<T>) -> Self {
14        Self(super::RepeatedView::new(values))
15    }
16    /// Append while wiping an allocation before replacing it.
17    pub fn push(&mut self, value: T) {
18        view_storage::reserve(&mut self.0.elements, 1);
19        self.0.elements.push(value);
20    }
21    /// Reserve without releasing an unwiped old buffer.
22    pub fn reserve(&mut self, additional: usize) {
23        view_storage::reserve(&mut self.0.elements, additional);
24    }
25    /// Borrow the decoded elements.
26    pub fn iter(&self) -> core::slice::Iter<'_, T> {
27        self.0.iter()
28    }
29}
30impl<T> Default for RepeatedView<'_, T> {
31    fn default() -> Self {
32        Self(super::RepeatedView::default())
33    }
34}
35impl<T> Deref for RepeatedView<'_, T> {
36    type Target = [T];
37    fn deref(&self) -> &[T] {
38        &self.0
39    }
40}
41impl<T> Zeroize for RepeatedView<'_, T> {
42    fn zeroize(&mut self) {
43        view_storage::clear(&mut self.0.elements);
44    }
45}
46impl<T> Drop for RepeatedView<'_, T> {
47    fn drop(&mut self) {
48        self.zeroize();
49    }
50}
51impl<T> ZeroizeOnDrop for RepeatedView<'_, T> {}
52impl<T> core::fmt::Debug for RepeatedView<'_, T> {
53    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
54        f.write_str("RepeatedView([REDACTED])")
55    }
56}
57impl<T> From<Vec<T>> for RepeatedView<'_, T> {
58    fn from(values: Vec<T>) -> Self {
59        Self::new(values)
60    }
61}
62impl<T> FromIterator<T> for RepeatedView<'_, T> {
63    fn from_iter<I: IntoIterator<Item = T>>(source: I) -> Self {
64        let mut out = Self::default();
65        for value in source {
66            out.push(value);
67        }
68        out
69    }
70}
71impl<T> IntoIterator for RepeatedView<'_, T> {
72    type Item = T;
73    type IntoIter = crate::sensitive::ViewIntoIter<T>;
74    fn into_iter(mut self) -> Self::IntoIter {
75        Self::IntoIter::new(core::mem::take(&mut self.0.elements))
76    }
77}
78impl<'b, T> IntoIterator for &'b RepeatedView<'_, T> {
79    type Item = &'b T;
80    type IntoIter = core::slice::Iter<'b, T>;
81    fn into_iter(self) -> Self::IntoIter {
82        self.iter()
83    }
84}
85
86/// A decoded map view that wipes owned entry storage on growth and drop.
87/// Lookup and duplicate-key handling match the ordinary map view.
88#[derive(Clone, PartialEq, Eq)]
89pub struct MapView<'a, K, V>(super::MapView<'a, K, V>);
90impl<'a, K, V> MapView<'a, K, V> {
91    /// Adopt an existing allocation without shrinking it.
92    pub fn new(entries: Vec<(K, V)>) -> Self {
93        Self(super::MapView::new(entries))
94    }
95    /// Append a wire entry, retaining last-occurrence lookup semantics.
96    pub fn push(&mut self, key: K, value: V) {
97        view_storage::reserve(&mut self.0.entries, 1);
98        self.0.entries.push((key, value));
99    }
100}
101impl<K, V> Default for MapView<'_, K, V> {
102    fn default() -> Self {
103        Self(super::MapView::default())
104    }
105}
106impl<'a, K, V> Deref for MapView<'a, K, V> {
107    type Target = super::MapView<'a, K, V>;
108    fn deref(&self) -> &Self::Target {
109        &self.0
110    }
111}
112impl<K, V> Zeroize for MapView<'_, K, V> {
113    fn zeroize(&mut self) {
114        view_storage::clear(&mut self.0.entries);
115    }
116}
117impl<K, V> Drop for MapView<'_, K, V> {
118    fn drop(&mut self) {
119        self.zeroize();
120    }
121}
122impl<K, V> ZeroizeOnDrop for MapView<'_, K, V> {}
123impl<K, V> core::fmt::Debug for MapView<'_, K, V> {
124    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
125        f.write_str("MapView([REDACTED])")
126    }
127}
128impl<K, V> From<Vec<(K, V)>> for MapView<'_, K, V> {
129    fn from(entries: Vec<(K, V)>) -> Self {
130        Self::new(entries)
131    }
132}
133impl<K, V> FromIterator<(K, V)> for MapView<'_, K, V> {
134    fn from_iter<I: IntoIterator<Item = (K, V)>>(source: I) -> Self {
135        let mut out = Self::default();
136        for (key, value) in source {
137            out.push(key, value);
138        }
139        out
140    }
141}
142impl<K, V> IntoIterator for MapView<'_, K, V> {
143    type Item = (K, V);
144    type IntoIter = crate::sensitive::ViewIntoIter<(K, V)>;
145    fn into_iter(mut self) -> Self::IntoIter {
146        Self::IntoIter::new(core::mem::take(&mut self.0.entries))
147    }
148}
149impl<'b, K, V> IntoIterator for &'b MapView<'_, K, V> {
150    type Item = &'b (K, V);
151    type IntoIter = core::slice::Iter<'b, (K, V)>;
152    fn into_iter(self) -> Self::IntoIter {
153        self.iter()
154    }
155}