spinoso_array/array/smallvec/
convert.rs

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