Add From<&[T]> and From<&mut [T]> for TinyVec

This adds the From trait impls for `&[T]` and `&mut [T]` to `TinyVec`,
though unfortunately until we get lazy normalization, it currently
requires that the `A: Array<item = T>` bound also be `Default`, so
it does not work for array sizes > 32.
This commit is contained in:
Wesley Norris
2020-01-23 17:58:27 -05:00
parent 554903f05f
commit 6ea2d1afcb
+31
View File
@@ -665,6 +665,37 @@ impl<A: Array> From<ArrayVec<A>> for TinyVec<A> {
}
}
impl<T, A> From<&'_ [T]> for TinyVec<A>
where
T: Clone + Default,
A: Array<Item = T> + Default,
{
#[inline]
#[must_use]
fn from(slice: &[T]) -> Self {
if slice.len() > A::CAPACITY {
TinyVec::Heap(slice.into())
} else {
let mut arr = ArrayVec::new();
arr.extend_from_slice(slice);
TinyVec::Inline(arr)
}
}
}
impl<T, A> From<&'_ mut [T]> for TinyVec<A>
where
T: Clone + Default,
A: Array<Item = T> + Default,
{
#[inline]
#[must_use]
fn from(slice: &mut [T]) -> Self {
Self::from(&*slice)
}
}
impl<A: Array + Default> FromIterator<A::Item> for TinyVec<A> {
#[inline]
#[must_use]