Skip to main content

mnemosyne_arena/scratch/aligned_vec/
convert.rs

1//! Conversion and collection traits for [`AlignedVec`].
2
3use super::AlignedVec;
4use super::ScratchElement;
5
6impl<T: ScratchElement> From<&[T]> for AlignedVec<T> {
7    /// Creates an `AlignedVec<T>` by copying elements from a slice.
8    ///
9    /// Equivalent to [`AlignedVec::from_slice`] and provided here to satisfy
10    /// the standard `From<&[T]>` convention that lets `into()` calls work
11    /// uniformly at conversion boundaries.
12    #[inline]
13    fn from(slice: &[T]) -> Self {
14        Self::from_slice(slice)
15    }
16}
17
18impl<T: ScratchElement> From<alloc::vec::Vec<T>> for AlignedVec<T> {
19    /// Converts a `Vec<T>` into an `AlignedVec<T>` by copying the elements.
20    ///
21    /// The source `Vec` is dropped after the copy. A true zero-copy
22    /// conversion is not possible in general because `Vec` uses the global
23    /// allocator while `AlignedVec` requires a specific alignment guarantee
24    /// that the global allocator does not provide. When Mnemosyne is the
25    /// global allocator the performance difference is one copy operation;
26    /// use [`AlignedVec::from_slice`] directly when you already have a slice.
27    #[inline]
28    fn from(v: alloc::vec::Vec<T>) -> Self {
29        Self::from_slice(&v)
30    }
31}
32
33impl<T: ScratchElement> From<AlignedVec<T>> for alloc::vec::Vec<T> {
34    /// Converts an `AlignedVec<T>` into a `Vec<T>` by copying the elements.
35    ///
36    /// Delegates to [`AlignedVec::into_vec`].
37    #[inline]
38    fn from(v: AlignedVec<T>) -> alloc::vec::Vec<T> {
39        v.into_vec()
40    }
41}
42
43impl<T: ScratchElement + PartialEq> PartialEq<alloc::vec::Vec<T>> for AlignedVec<T> {
44    #[inline]
45    fn eq(&self, other: &alloc::vec::Vec<T>) -> bool {
46        self.as_slice() == other.as_slice()
47    }
48}
49
50impl<T: ScratchElement + PartialEq> PartialEq<AlignedVec<T>> for alloc::vec::Vec<T> {
51    #[inline]
52    fn eq(&self, other: &AlignedVec<T>) -> bool {
53        self.as_slice() == other.as_slice()
54    }
55}
56
57impl<T: ScratchElement + PartialEq, const N: usize> PartialEq<[T; N]> for AlignedVec<T> {
58    #[inline]
59    fn eq(&self, other: &[T; N]) -> bool {
60        self.as_slice() == other.as_slice()
61    }
62}
63
64impl<T: ScratchElement> core::convert::AsRef<[T]> for AlignedVec<T> {
65    #[inline]
66    fn as_ref(&self) -> &[T] {
67        self.as_slice()
68    }
69}
70
71impl<T: ScratchElement> core::convert::AsMut<[T]> for AlignedVec<T> {
72    #[inline]
73    fn as_mut(&mut self) -> &mut [T] {
74        self.as_mut_slice()
75    }
76}
77
78impl<T: ScratchElement> core::borrow::Borrow<[T]> for AlignedVec<T> {
79    #[inline]
80    fn borrow(&self) -> &[T] {
81        self.as_slice()
82    }
83}
84
85impl<T: ScratchElement> core::borrow::BorrowMut<[T]> for AlignedVec<T> {
86    #[inline]
87    fn borrow_mut(&mut self) -> &mut [T] {
88        self.as_mut_slice()
89    }
90}
91
92impl<T: ScratchElement> core::iter::FromIterator<T> for AlignedVec<T> {
93    #[inline]
94    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
95        let iter = iter.into_iter();
96        let (lo, _) = iter.size_hint();
97        let mut buf = AlignedVec::with_capacity(lo);
98        for item in iter {
99            buf.push(item);
100        }
101        buf
102    }
103}
104
105impl<T: ScratchElement> Extend<T> for AlignedVec<T> {
106    #[inline]
107    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
108        self.extend_from_iter(iter);
109    }
110}
111
112impl<'a, T: ScratchElement> Extend<&'a T> for AlignedVec<T> {
113    #[inline]
114    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
115        for &item in iter {
116            self.push(item);
117        }
118    }
119}
120
121impl<T: ScratchElement> From<AlignedVec<T>> for alloc::boxed::Box<[T]> {
122    /// Converts an `AlignedVec<T>` into a boxed slice.
123    ///
124    /// Copies the initialized elements into a heap allocation managed by the
125    /// global allocator. This is a data copy because `AlignedVec` and `Box<[T]>`
126    /// use different allocators (and possibly different alignments).
127    #[inline]
128    fn from(v: AlignedVec<T>) -> alloc::boxed::Box<[T]> {
129        v.into_vec().into_boxed_slice()
130    }
131}
132
133impl<T: ScratchElement> From<alloc::boxed::Box<[T]>> for AlignedVec<T> {
134    /// Converts a boxed slice into an `AlignedVec<T>` by copying.
135    #[inline]
136    fn from(b: alloc::boxed::Box<[T]>) -> Self {
137        Self::from_slice(&b)
138    }
139}