Trait Capacity

Source
pub trait Capacity: Len {
    // Required method
    fn capacity(&self) -> usize;
}
Expand description

A trait for describing the capacity of a collection.

Because frequently allocating memory can be inefficient, collections often store room for more data than they actually hold. This “extra length” is represented by capacity, stating the maximum length that can be occupied without any memory allocations.

Obtaining the capacity of the collection must take a constant amount of time and space.

Required Methods§

Source

fn capacity(&self) -> usize

Returns the capacity of the collection.

If this collection stores zero-sized types, then it effectively has infinite capacity. For this reason, those collections should have a capacity of usize::MAX.

§Examples
use len_trait::Capacity;

fn check_capacity<C: Capacity>(collection: C, zero_sized: bool) {
    if zero_sized {
        assert_eq!(collection.capacity(), usize::max_value());
    } else {
        assert!(collection.capacity() >= collection.len());
    }
}

check_capacity(vec![()], true);
check_capacity(vec![1, 2, 3], false);
check_capacity("Hello, world!".to_string(), false);

Implementations on Foreign Types§

Source§

impl Capacity for String

Source§

fn capacity(&self) -> usize

Source§

impl Capacity for OsString

Source§

fn capacity(&self) -> usize

Source§

impl<K: Eq + Hash, V> Capacity for HashMap<K, V>

Source§

fn capacity(&self) -> usize

Source§

impl<T> Capacity for VecDeque<T>

Source§

fn capacity(&self) -> usize

Source§

impl<T> Capacity for Vec<T>

Source§

fn capacity(&self) -> usize

Source§

impl<T: Eq + Hash> Capacity for HashSet<T>

Source§

fn capacity(&self) -> usize

Source§

impl<T: Ord> Capacity for BinaryHeap<T>

Source§

fn capacity(&self) -> usize

Source§

impl<T: ?Sized + Capacity> Capacity for Box<T>

Source§

fn capacity(&self) -> usize

Source§

impl<T: ?Sized + Capacity> Capacity for Rc<T>

Source§

fn capacity(&self) -> usize

Source§

impl<T: ?Sized + Capacity> Capacity for Arc<T>

Source§

fn capacity(&self) -> usize

Implementors§