FEAT: Add ArrayVec.capacity_left

This commit is contained in:
Thomas de Zeeuw
2018-07-17 14:46:53 +02:00
parent d84cb377a9
commit d11c853346
2 changed files with 27 additions and 0 deletions
+13
View File
@@ -172,6 +172,19 @@ impl<A: Array> ArrayVec<A> {
/// ``` /// ```
pub fn is_full(&self) -> bool { self.len() == self.capacity() } pub fn is_full(&self) -> bool { self.len() == self.capacity() }
/// Returns the capacity left in the `ArrayVec`.
///
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut array = ArrayVec::from([1, 2, 3]);
/// array.pop();
/// assert_eq!(array.capacity_left(), 1);
/// ```
pub fn capacity_left(&self) -> usize {
self.capacity() - self.len()
}
/// Push `element` to the end of the vector. /// Push `element` to the end of the vector.
/// ///
/// ***Panics*** if the vector is already full. /// ***Panics*** if the vector is already full.
+14
View File
@@ -27,6 +27,20 @@ fn test_simple() {
assert_eq!(sum_len, 8); assert_eq!(sum_len, 8);
} }
#[test]
fn test_capacity_left() {
let mut vec: ArrayVec<[usize; 4]> = ArrayVec::new();
assert_eq!(vec.capacity_left(), 4);
vec.push(1);
assert_eq!(vec.capacity_left(), 3);
vec.push(2);
assert_eq!(vec.capacity_left(), 2);
vec.push(3);
assert_eq!(vec.capacity_left(), 1);
vec.push(4);
assert_eq!(vec.capacity_left(), 0);
}
#[test] #[test]
fn test_u16_index() { fn test_u16_index() {
const N: usize = 4096; const N: usize = 4096;