mirror of
https://github.com/openharmony/third_party_rust_tinyvec.git
synced 2026-07-18 20:34:34 -04:00
append
This commit is contained in:
+13
-1
@@ -42,7 +42,19 @@ impl<A: Arrayish> IndexMut<usize> for ArrayishVec<A> {
|
||||
}
|
||||
|
||||
impl<A: Arrayish> ArrayishVec<A> {
|
||||
// TODO(Vec): append
|
||||
#[inline]
|
||||
pub fn append(&mut self, other: &mut Self) {
|
||||
let final_len = self.len + other.len;
|
||||
if final_len > A::CAPACITY {
|
||||
panic!("ArrayishVec: overflow!");
|
||||
}
|
||||
let target_slice = &mut self.data.slice_mut()[self.len..final_len];
|
||||
for (target_mut, app_mut) in target_slice.iter_mut().zip(other.deref_mut()) {
|
||||
replace(target_mut, replace(app_mut, A::Item::default()));
|
||||
}
|
||||
self.len = final_len;
|
||||
other.len = 0;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
|
||||
@@ -97,3 +97,19 @@ fn ArrayishVec_iteration() {
|
||||
let av2: ArrayishVec<[i32; 4]> = av.clone().into_iter().collect();
|
||||
assert_eq!(av, av2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ArrayishVec_append() {
|
||||
let mut av: ArrayishVec<[i32; 10]> = Default::default();
|
||||
av.push(1);
|
||||
av.push(2);
|
||||
av.push(3);
|
||||
let mut av2: ArrayishVec<[i32; 10]> = Default::default();
|
||||
av2.push(4);
|
||||
av2.push(5);
|
||||
av2.push(6);
|
||||
//
|
||||
av.append(&mut av2);
|
||||
assert_eq!(av.as_slice(), &[1_i32, 2, 3, 4, 5, 6]);
|
||||
assert_eq!(av2.as_slice(), &[]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user