spinoso_array/array/vec/
convert.rs

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