secure_gate/
fixed.rs

1// src/fixed.rs
2use core::convert::From;
3use core::ops::{Deref, DerefMut};
4
5// use crate::{Expose, ExposeMut};
6
7pub struct Fixed<T>(pub T); // ← pub field
8
9impl<T> Fixed<T> {
10    pub fn new(value: T) -> Self {
11        Fixed(value)
12    }
13}
14
15impl<T> Deref for Fixed<T> {
16    type Target = T;
17    #[inline(always)]
18    fn deref(&self) -> &T {
19        &self.0
20    }
21}
22
23impl<T> DerefMut for Fixed<T> {
24    #[inline(always)]
25    fn deref_mut(&mut self) -> &mut T {
26        &mut self.0
27    }
28}
29
30impl<const N: usize> AsRef<[u8]> for Fixed<[u8; N]> {
31    #[inline(always)]
32    fn as_ref(&self) -> &[u8] {
33        &self.0
34    }
35}
36
37impl<const N: usize> AsMut<[u8]> for Fixed<[u8; N]> {
38    #[inline(always)]
39    fn as_mut(&mut self) -> &mut [u8] {
40        &mut self.0
41    }
42}
43
44impl<T> core::fmt::Debug for Fixed<T> {
45    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46        f.write_str("[REDACTED]")
47    }
48}
49
50impl<T> Fixed<T> {
51    #[inline(always)]
52    pub fn expose_secret(&self) -> &T {
53        &self.0
54    }
55
56    #[inline(always)]
57    pub fn expose_secret_mut(&mut self) -> &mut T {
58        &mut self.0
59    }
60
61    // Legacy → redirect to the new names
62    #[deprecated(since = "0.5.5", note = "use `expose_secret` instead")]
63    #[doc(hidden)]
64    #[inline(always)]
65    pub fn view(&self) -> &T {
66        self.expose_secret()
67    }
68
69    #[deprecated(since = "0.5.5", note = "use `expose_secret_mut` instead")]
70    #[doc(hidden)]
71    #[inline(always)]
72    pub fn view_mut(&mut self) -> &mut T {
73        self.expose_secret_mut()
74    }
75}
76
77impl<T> Fixed<T> {
78    pub fn into_inner(self) -> T {
79        self.0
80    }
81}
82
83impl<const N: usize> Fixed<[u8; N]> {
84    /// Create from a slice. Panics if the slice has the wrong length.
85    #[inline]
86    pub fn from_slice(bytes: &[u8]) -> Self {
87        assert_eq!(bytes.len(), N, "slice length mismatch");
88        let mut arr = [0u8; N];
89        arr.copy_from_slice(&bytes[..N]);
90        Self::new(arr)
91    }
92}
93
94impl<const N: usize> From<[u8; N]> for Fixed<[u8; N]> {
95    #[inline]
96    fn from(arr: [u8; N]) -> Self {
97        Self::new(arr)
98    }
99}