Skip to main content

mnemosyne_arena/scratch/aligned_buf/
traits.rs

1use super::AlignedBuf;
2use crate::scratch::element::ScratchElement;
3
4impl<T: ScratchElement, const N: usize> Default for AlignedBuf<T, N> {
5    #[inline]
6    fn default() -> Self {
7        Self::new()
8    }
9}
10
11impl<T: ScratchElement + core::fmt::Debug, const N: usize> core::fmt::Debug for AlignedBuf<T, N> {
12    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
13        f.debug_list().entries(self.as_slice().iter()).finish()
14    }
15}
16
17impl<T: ScratchElement + PartialEq, const N: usize> PartialEq for AlignedBuf<T, N> {
18    #[inline]
19    fn eq(&self, other: &Self) -> bool {
20        self.as_slice() == other.as_slice()
21    }
22}
23
24impl<T: ScratchElement + Eq, const N: usize> Eq for AlignedBuf<T, N> {}
25
26impl<T: ScratchElement + PartialOrd, const N: usize> PartialOrd for AlignedBuf<T, N> {
27    #[inline]
28    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
29        self.as_slice().partial_cmp(other.as_slice())
30    }
31}
32
33impl<T: ScratchElement + Ord, const N: usize> Ord for AlignedBuf<T, N> {
34    #[inline]
35    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
36        self.as_slice().cmp(other.as_slice())
37    }
38}
39
40impl<T: ScratchElement + core::hash::Hash, const N: usize> core::hash::Hash for AlignedBuf<T, N> {
41    #[inline]
42    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
43        self.as_slice().hash(state);
44    }
45}
46
47impl<T: ScratchElement, const N: usize> core::ops::Deref for AlignedBuf<T, N> {
48    type Target = [T];
49
50    #[inline]
51    fn deref(&self) -> &[T] {
52        self.as_slice()
53    }
54}
55
56impl<T: ScratchElement, const N: usize> core::ops::DerefMut for AlignedBuf<T, N> {
57    #[inline]
58    fn deref_mut(&mut self) -> &mut [T] {
59        self.as_mut_slice()
60    }
61}
62
63impl<T: ScratchElement, const N: usize> AsRef<[T]> for AlignedBuf<T, N> {
64    #[inline]
65    fn as_ref(&self) -> &[T] {
66        self.as_slice()
67    }
68}
69
70impl<T: ScratchElement, const N: usize> AsMut<[T]> for AlignedBuf<T, N> {
71    #[inline]
72    fn as_mut(&mut self) -> &mut [T] {
73        self.as_mut_slice()
74    }
75}
76
77impl<T: ScratchElement, const N: usize> core::borrow::Borrow<[T]> for AlignedBuf<T, N> {
78    #[inline]
79    fn borrow(&self) -> &[T] {
80        self.as_slice()
81    }
82}
83
84impl<T: ScratchElement, const N: usize> core::borrow::BorrowMut<[T]> for AlignedBuf<T, N> {
85    #[inline]
86    fn borrow_mut(&mut self) -> &mut [T] {
87        self.as_mut_slice()
88    }
89}
90
91impl<T: ScratchElement + PartialEq, const N: usize> PartialEq<[T]> for AlignedBuf<T, N> {
92    #[inline]
93    fn eq(&self, other: &[T]) -> bool {
94        self.as_slice() == other
95    }
96}
97
98impl<'a, T: ScratchElement, const N: usize> IntoIterator for &'a AlignedBuf<T, N> {
99    type Item = &'a T;
100    type IntoIter = core::slice::Iter<'a, T>;
101
102    #[inline]
103    fn into_iter(self) -> Self::IntoIter {
104        self.as_slice().iter()
105    }
106}
107
108impl<'a, T: ScratchElement, const N: usize> IntoIterator for &'a mut AlignedBuf<T, N> {
109    type Item = &'a mut T;
110    type IntoIter = core::slice::IterMut<'a, T>;
111
112    #[inline]
113    fn into_iter(self) -> Self::IntoIter {
114        self.as_mut_slice().iter_mut()
115    }
116}
117
118impl<T: ScratchElement, const N: usize> core::iter::FromIterator<T> for AlignedBuf<T, N> {
119    /// Collects up to `N` elements; any beyond `N` are silently dropped.
120    ///
121    /// This mirrors [`from_slice_truncating`][AlignedBuf::from_slice_truncating]:
122    /// the buffer's fixed capacity cannot grow.
123    #[inline]
124    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
125        let mut buf = Self::new();
126        for item in iter {
127            if !buf.try_push(item) {
128                break;
129            }
130        }
131        buf
132    }
133}
134
135impl<T: ScratchElement, const N: usize> From<[T; N]> for AlignedBuf<T, N> {
136    #[inline]
137    fn from(arr: [T; N]) -> Self {
138        Self::from_array(arr)
139    }
140}
141
142impl<T: ScratchElement, const N: usize> From<AlignedBuf<T, N>> for [T; N] {
143    /// Converts a full buffer into an array.
144    ///
145    /// # Panics
146    ///
147    /// Panics if `buf.len() != N`.
148    #[inline]
149    fn from(buf: AlignedBuf<T, N>) -> [T; N] {
150        assert!(
151            buf.len == N,
152            "AlignedBuf: cannot convert to [T; N], len {} != {N}",
153            buf.len
154        );
155        // SAFETY: `buf.len == N` means all N slots are initialized.
156        // `T: ScratchElement: Copy` so reading each by copy is valid.
157        core::array::from_fn(|i| unsafe { buf.data[i].assume_init_read() })
158    }
159}