1use core::convert::From;
3use core::ops::{Deref, DerefMut};
4
5use crate::{Expose, ExposeMut};
6
7pub struct Fixed<T>(pub T); impl<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
52impl<const N: usize> Fixed<[u8; N]> {
65 #[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}