FEAT: Add ArrayVec.capacity_left
This commit is contained in:
+13
@@ -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.
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user