Trait Collection

Source
pub trait Collection {
    type Item;

    // Required methods
    fn len(&self) -> usize;
    fn capacity(&self) -> usize;
    fn extend_object(&mut self, items: &mut dyn Iterator<Item = Self::Item>)
       where Self: AddRemove;
    fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = Self::Item> + 'a>
       where Self: AddRemove;
    fn reserve(&mut self, additional: usize)
       where Self: AddRemove;
    fn shrink_to_fit(&mut self)
       where Self: AddRemove;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn append(&mut self, other: &mut Self)
       where Self: Sized + AddRemove { ... }
    fn clear(&mut self)
       where Self: AddRemove { ... }
    fn with_capacity(capacity: usize) -> Self
       where Self: Sized + Default { ... }
    fn into_vec(self) -> Vec<Self::Item>
       where Self: Sized { ... }
}
Expand description

A collection.

A collection maintains a finite number of items.

Required Associated Types§

Source

type Item

The type of the collection’s items.

Required Methods§

Source

fn len(&self) -> usize

Returns the number of items in the collection.

Source

fn capacity(&self) -> usize

Returns the number of items the collection can hold without reallocating.

Node-based collections should report a capacity of self.len().

Source

fn extend_object(&mut self, items: &mut dyn Iterator<Item = Self::Item>)
where Self: AddRemove,

Inserts the items yielded by the given iterator into the collection.

This method is provided for use with trait objects, and generic code should prefer Extend::extend, which this method must be equivalent to.

The exact behavior of this method is unspecified, but may be refined by subtraits.

Note that this trait cannot extend Extend due to object-safety limitations.

Source

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = Self::Item> + 'a>
where Self: AddRemove,

Removes all items from the collection and returns an iterator that yields them.

All items are removed even if the iterator is not exhausted. However, the behavior of this method is unspecified if the iterator is leaked (e.g. via mem::forget).

The iteration order is unspecified, but subtraits may place a requirement on it.

self’s capacity should remain the same, when possible.

Source

fn reserve(&mut self, additional: usize)
where Self: AddRemove,

Reserves capacity for the given number of additional items to be inserted into the collection.

This method may do nothing (e.g. for node-based collections).

Source

fn shrink_to_fit(&mut self)
where Self: AddRemove,

Shrinks the collection’s capacity as much as possible.

This method may do nothing (e.g. for node-based collections).

Provided Methods§

Source

fn is_empty(&self) -> bool

Checks if the collection contains no items.

Source

fn append(&mut self, other: &mut Self)
where Self: Sized + AddRemove,

Drains the given collection and inserts its items into the collection.

The exact behavior of this method is unspecified, but it must be equivalent to self.extend_object(&mut other.drain()). other’s capacity should remain the same, when possible.

Source

fn clear(&mut self)
where Self: AddRemove,

Removes all items from the collection.

Source

fn with_capacity(capacity: usize) -> Self
where Self: Sized + Default,

Returns a new collection with the given capacity.

Collections (e.g. node-based ones) may ignore the capacity hint.

Source

fn into_vec(self) -> Vec<Self::Item>
where Self: Sized,

Converts the collection into a vector.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

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

Source§

type Item = (K, V)

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn append(&mut self, other: &mut Self)

Source§

fn extend_object(&mut self, items: &mut dyn Iterator<Item = (K, V)>)

Source§

fn clear(&mut self)

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = (K, V)> + 'a>

Source§

fn reserve(&mut self, additional: usize)

Source§

fn shrink_to_fit(&mut self)

Source§

fn with_capacity(capacity: usize) -> Self

Source§

fn into_vec(self) -> Vec<(K, V)>

Source§

impl<K: Ord, V> Collection for BTreeMap<K, V>

Source§

type Item = (K, V)

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn append(&mut self, other: &mut Self)

Source§

fn extend_object(&mut self, items: &mut dyn Iterator<Item = (K, V)>)

Source§

fn clear(&mut self)

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = (K, V)> + 'a>

Source§

fn reserve(&mut self, _additional: usize)

Source§

fn shrink_to_fit(&mut self)

Source§

fn with_capacity(_capacity: usize) -> Self

Source§

fn into_vec(self) -> Vec<(K, V)>

Source§

impl<T> Collection for [T]

Source§

type Item = T

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn extend_object(&mut self, _items: &mut dyn Iterator<Item = T>)
where Self: AddRemove,

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = T> + 'a>
where Self: AddRemove,

Source§

fn reserve(&mut self, _additional: usize)
where Self: AddRemove,

Source§

fn shrink_to_fit(&mut self)
where Self: AddRemove,

Source§

impl<T> Collection for LinkedList<T>

Source§

type Item = T

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn append(&mut self, other: &mut Self)

Source§

fn extend_object(&mut self, items: &mut dyn Iterator<Item = T>)

Source§

fn clear(&mut self)

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = T> + 'a>

Source§

fn reserve(&mut self, _additional: usize)

Source§

fn shrink_to_fit(&mut self)

Source§

fn with_capacity(_capacity: usize) -> Self

Source§

fn into_vec(self) -> Vec<T>

Source§

impl<T> Collection for VecDeque<T>

Source§

type Item = T

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn clear(&mut self)

Source§

fn append(&mut self, other: &mut Self)

Source§

fn extend_object(&mut self, items: &mut dyn Iterator<Item = T>)

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = T> + 'a>

Source§

fn reserve(&mut self, additional: usize)

Source§

fn shrink_to_fit(&mut self)

Source§

fn with_capacity(capacity: usize) -> Self

Source§

fn into_vec(self) -> Vec<T>

Source§

impl<T> Collection for Vec<T>

Source§

type Item = T

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn append(&mut self, other: &mut Self)

Source§

fn extend_object(&mut self, items: &mut dyn Iterator<Item = T>)

Source§

fn clear(&mut self)

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = T> + 'a>

Source§

fn reserve(&mut self, additional: usize)

Source§

fn shrink_to_fit(&mut self)

Source§

fn with_capacity(capacity: usize) -> Self

Source§

fn into_vec(self) -> Vec<T>

Source§

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

Source§

type Item = T

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn append(&mut self, other: &mut Self)

Source§

fn extend_object(&mut self, items: &mut dyn Iterator<Item = T>)

Source§

fn clear(&mut self)

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = T> + 'a>

Source§

fn reserve(&mut self, additional: usize)

Source§

fn shrink_to_fit(&mut self)

Source§

fn with_capacity(capacity: usize) -> Self

Source§

fn into_vec(self) -> Vec<T>

Source§

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

Source§

type Item = T

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn append(&mut self, other: &mut Self)

Source§

fn extend_object(&mut self, items: &mut dyn Iterator<Item = T>)

Source§

fn clear(&mut self)

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = T> + 'a>

Source§

fn reserve(&mut self, additional: usize)

Source§

fn shrink_to_fit(&mut self)

Source§

fn with_capacity(capacity: usize) -> Self

Source§

fn into_vec(self) -> Vec<T>

Source§

impl<T: Ord> Collection for BTreeSet<T>

Source§

type Item = T

Source§

fn len(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn append(&mut self, other: &mut Self)

Source§

fn extend_object(&mut self, items: &mut dyn Iterator<Item = T>)

Source§

fn clear(&mut self)

Source§

fn drain<'a>(&'a mut self) -> Box<dyn Iterator<Item = T> + 'a>

Source§

fn reserve(&mut self, _additional: usize)

Source§

fn shrink_to_fit(&mut self)

Source§

fn with_capacity(_capacity: usize) -> Self

Source§

fn into_vec(self) -> Vec<T>

Implementors§