secure_gate/
fixed.rs

1// src/fixed.rs
2use core::convert::From;
3use core::ops::{Deref, DerefMut};
4
5use 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<T> core::fmt::Debug for Fixed<T> {
31    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
32        f.write_str("[REDACTED]")
33    }
34}
35
36impl<T> Fixed<T> {
37    pub fn view(&self) -> Expose<'_, T> {
38        Expose(&self.0)
39    }
40
41    pub fn view_mut(&mut self) -> ExposeMut<'_, T> {
42        ExposeMut(&mut self.0)
43    }
44}
45
46impl<T> Fixed<T> {
47    pub fn into_inner(self) -> T {
48        self.0
49    }
50}
51
52// // From impls for common sizes (no orphan rule issue)
53// macro_rules! impl_from_array {
54//     ($($N:literal),*) => {$(
55//         impl From<[u8; $N]> for Fixed<[u8; $N]> {
56//             fn from(arr: [u8; $N]) -> Self {
57//                 Self::new(arr)
58//             }
59//         }
60//     )*}
61// }
62// impl_from_array!(12, 16, 24, 32, 64);
63
64impl<const N: usize> Fixed<[u8; N]> {
65    /// Create from a slice. Panics if the slice has the wrong length.
66    #[inline]
67    pub fn from_slice(bytes: &[u8]) -> Self {
68        assert_eq!(bytes.len(), N, "slice length mismatch");
69        let mut arr = [0u8; N];
70        arr.copy_from_slice(&bytes[..N]);
71        Self::new(arr)
72    }
73}
74
75impl<const N: usize> From<[u8; N]> for Fixed<[u8; N]> {
76    #[inline]
77    fn from(arr: [u8; N]) -> Self {
78        Self::new(arr)
79    }
80}