spinoso_array/array/tinyvec/
convert.rs

1use alloc::borrow::Cow;
2use alloc::boxed::Box;
3use alloc::rc::Rc;
4use alloc::sync::Arc;
5use alloc::vec::Vec;
6
7use tinyvec::TinyVec;
8
9use crate::array::INLINE_CAPACITY;
10use crate::array::tinyvec::TinyArray;
11use crate::array::vec::Array;
12
13impl<T> From<Vec<T>> for TinyArray<T>
14where
15    T: Default,
16{
17    #[inline]
18    fn from(values: Vec<T>) -> Self {
19        Self(values.into_iter().collect())
20    }
21}
22
23impl<T> From<TinyArray<T>> for Vec<T>
24where
25    T: Clone + Default,
26{
27    #[inline]
28    fn from(values: TinyArray<T>) -> Self {
29        values.into_vec()
30    }
31}
32
33impl<'a, T> From<&'a [T]> for TinyArray<T>
34where
35    T: Clone + Default,
36{
37    #[inline]
38    fn from(values: &'a [T]) -> Self {
39        Self(TinyVec::from(values))
40    }
41}
42
43impl<'a, T> From<&'a mut [T]> for TinyArray<T>
44where
45    T: Clone + Default,
46{
47    #[inline]
48    fn from(values: &'a mut [T]) -> Self {
49        Self(TinyVec::from(values))
50    }
51}
52
53impl<T> From<Box<[T]>> for TinyArray<T>
54where
55    T: Default,
56{
57    #[inline]
58    fn from(values: Box<[T]>) -> Self {
59        Self(Vec::from(values).into_iter().collect())
60    }
61}
62
63impl<T> From<TinyArray<T>> for Box<[T]>
64where
65    T: Clone + Default,
66{
67    #[inline]
68    fn from(values: TinyArray<T>) -> Self {
69        values.into_boxed_slice()
70    }
71}
72
73impl<'a, T> From<Cow<'a, [T]>> for TinyArray<T>
74where
75    T: Clone + Default,
76{
77    #[inline]
78    fn from(values: Cow<'a, [T]>) -> Self {
79        match values {
80            Cow::Borrowed(slice) => slice.into(),
81            Cow::Owned(vec) => vec.into(),
82        }
83    }
84}
85
86impl<T> From<TinyArray<T>> for Cow<'_, [T]>
87where
88    T: Clone + Default,
89{
90    #[inline]
91    fn from(values: TinyArray<T>) -> Self {
92        values.into_vec().into()
93    }
94}
95
96impl<T> From<TinyArray<T>> for Rc<[T]>
97where
98    T: Clone + Default,
99{
100    #[inline]
101    fn from(values: TinyArray<T>) -> Self {
102        values.into_vec().into()
103    }
104}
105
106impl<T> From<TinyArray<T>> for Arc<[T]>
107where
108    T: Clone + Default,
109{
110    #[inline]
111    fn from(values: TinyArray<T>) -> Self {
112        values.into_vec().into()
113    }
114}
115
116impl<T, const N: usize> From<[T; N]> for TinyArray<T>
117where
118    T: Default,
119{
120    #[inline]
121    fn from(values: [T; N]) -> Self {
122        Self(values.into_iter().collect())
123    }
124}
125
126impl<T, const N: usize> From<&[T; N]> for TinyArray<T>
127where
128    T: Clone + Default,
129{
130    #[inline]
131    fn from(values: &[T; N]) -> Self {
132        Self(TinyVec::from(&values[..]))
133    }
134}
135
136impl<T> From<TinyVec<[T; INLINE_CAPACITY]>> for TinyArray<T>
137where
138    T: Default,
139{
140    #[inline]
141    fn from(values: TinyVec<[T; INLINE_CAPACITY]>) -> Self {
142        Self(values)
143    }
144}
145
146impl<T> From<TinyArray<T>> for TinyVec<[T; INLINE_CAPACITY]>
147where
148    T: Default,
149{
150    #[inline]
151    fn from(values: TinyArray<T>) -> Self {
152        values.into_inner()
153    }
154}
155
156impl<T> From<Array<T>> for TinyArray<T>
157where
158    T: Default,
159{
160    #[inline]
161    fn from(values: Array<T>) -> Self {
162        Self::from(values.into_vec())
163    }
164}
165
166impl<T> FromIterator<T> for TinyArray<T>
167where
168    T: Default,
169{
170    #[inline]
171    fn from_iter<I>(iter: I) -> Self
172    where
173        I: IntoIterator<Item = T>,
174    {
175        Self(iter.into_iter().collect())
176    }
177}
178
179impl<'a, T> FromIterator<&'a T> for TinyArray<T>
180where
181    T: 'a + Clone + Default,
182{
183    #[inline]
184    fn from_iter<I>(iter: I) -> Self
185    where
186        I: IntoIterator<Item = &'a T>,
187    {
188        Self(iter.into_iter().cloned().collect())
189    }
190}