mirror of
https://github.com/openharmony/third_party_rust_tinyvec.git
synced 2026-07-19 14:23:33 -04:00
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:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user