mirror of
https://github.com/openharmony/third_party_rust_tinyvec.git
synced 2026-07-19 14:23:33 -04:00
Merge branch 'master' into small-updates
This commit is contained in:
@@ -657,6 +657,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]
|
||||
|
||||
@@ -65,3 +65,22 @@ fn TinyVec_resize() {
|
||||
tv.resize(20, 5);
|
||||
assert_eq!(&tv[..], &[5; 20]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn TinyVec_from_slice_impl() {
|
||||
let bigger_slice: [u8; 11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
let tinyvec: TinyVec<[u8; 10]> = TinyVec::Heap((&bigger_slice[..]).into());
|
||||
assert_eq!(TinyVec::from(&bigger_slice[..]), tinyvec);
|
||||
|
||||
let smaller_slice: [u8; 5] = [0, 1, 2, 3, 4];
|
||||
let tinyvec: TinyVec<[u8; 10]> = TinyVec::Inline(ArrayVec::from_array_len(
|
||||
[0, 1, 2, 3, 4, 0, 0, 0, 0, 0],
|
||||
5,
|
||||
));
|
||||
assert_eq!(TinyVec::from(&smaller_slice[..]), tinyvec);
|
||||
|
||||
let same_size: [u8; 4] = [0, 1, 2, 3];
|
||||
let tinyvec: TinyVec<[u8; 4]> =
|
||||
TinyVec::Inline(ArrayVec::from_array_len(same_size, 4));
|
||||
assert_eq!(TinyVec::from(&same_size[..]), tinyvec);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user