add truncate method and tests based on the Vec examples

This commit is contained in:
Lokathor
2020-01-06 20:05:08 -07:00
parent bf21f0e170
commit c063024184
2 changed files with 52 additions and 3 deletions
+19
View File
@@ -5,6 +5,7 @@
use core::ops::{Deref, DerefMut};
use core::mem::replace;
use core::mem::needs_drop;
extern crate alloc;
use alloc::vec::Vec;
@@ -113,4 +114,22 @@ impl<T: Default> TinyVec<T> {
}
}
}
pub fn truncate(&mut self, new_len: usize) {
match &mut self.0 {
Payload::Inline { len, data } => {
if needs_drop::<T>() {
while *len > new_len {
replace(&mut data[*len - 1], T::default());
*len -= 1;
}
} else {
*len = (*len).min(new_len);
}
}
Payload::Heap(ref mut vec) => {
vec.truncate(new_len)
}
}
}
}
+33 -3
View File
@@ -1,10 +1,11 @@
extern crate std;
#![allow(bad_style)]
use tinyvec::*;
use core::ops::Deref;
#[test]
fn test_push_pop() {
fn TinyVec_push_pop() {
let mut tv = TinyVec::new();
assert_eq!(tv.pop(), None);
@@ -31,3 +32,32 @@ fn test_push_pop() {
assert_eq!(tv.pop(), None);
assert_eq!(tv.pop(), None);
}
#[test]
fn TinyVec_truncate() {
// Truncating a five element vector to two elements
let mut tv = TinyVec::new();
tv.push(1_i32);
tv.push(2);
tv.push(3);
tv.push(4);
tv.push(5);
tv.truncate(2);
assert_eq!(tv.deref(), [1, 2]);
// No truncation occurs when len is greater than the vector's current length
let mut tv = TinyVec::new();
tv.push(1_i32);
tv.push(2);
tv.push(3);
tv.truncate(8);
assert_eq!(tv.deref(), [1, 2, 3]);
// Truncating when len == 0 is equivalent to calling the clear method.
let mut tv = TinyVec::new();
tv.push(1_i32);
tv.push(2);
tv.push(3);
tv.truncate(0);
assert_eq!(tv.deref(), []);
}