[][src]Trait fill::Fill

pub trait Fill<T> {
    fn fill<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = T>
; fn fill_and_keep_tail<I>(&mut self, iter: I) -> I::IntoIter
    where
        I: IntoIterator<Item = T>
, { ... }
fn fill_count<I>(&mut self, iter: I) -> usize
    where
        I: IntoIterator<Item = T>
, { ... }
fn checked_fill<I>(&mut self, iter: I) -> Option<I::IntoIter>
    where
        I: IntoIterator<Item = T>
, { ... } }

Fill a collection from an iterator until its capacity has been reached.

Examples

An option's capacity has been reached when it contains an item in a Some. It consumes no items from the iterator when it already contains one and consumes exactly one if it is currently None.

let mut option = None;

let spilled = option.fill_and_keep_tail(42..);
assert_eq!(option, Some(42));
assert_eq!(spilled.start, 43);

Required methods

fn fill<I>(&mut self, iter: I) where
    I: IntoIterator<Item = T>, 

Fill empty space with contents from an iterator.

The iterator should be dropped and not exhausted when no more items can be pushed into the container.

Loading content...

Provided methods

fn fill_and_keep_tail<I>(&mut self, iter: I) -> I::IntoIter where
    I: IntoIterator<Item = T>, 

Fill the container from an iterator, returning it afterwards.

This is only a shorthand for iterating by reference when the caller wants to inspect the state after filling or when a fall-back is supposed to consume remaining elements.

fn fill_count<I>(&mut self, iter: I) -> usize where
    I: IntoIterator<Item = T>, 

Fill the container and count the number of pulled items.

The count is equal to the number of elements that have been pulled from the iterator. On well-behaved containers this shall equal the number of items inserted as it should do so for all items.

Examples

For containers with a statically known capacity this can be an alternative to checking the current state after a fill operation.

let mut option = None;
assert_eq!(option.fill_count(0..), 1);
assert_eq!(option.fill_count(1..), 0);

fn checked_fill<I>(&mut self, iter: I) -> Option<I::IntoIter> where
    I: IntoIterator<Item = T>, 

Fill the container and return if more elements could have been available.

This returns None if the iterator was exhausted before the container could be filled completely. Otherwise it will return Some(_) containing the remaining iterator.

Note that a well behaved fillable container should not poll more items when it is full so that this method should return Some but when it violates this assumption then the method might return None instead as the implementation merely detects if the fused iterator was exhausted.

Examples

let mut option = None;
assert_eq!(option.checked_fill(None).is_some(), false);
assert_eq!(option.checked_fill(0..).is_some(), true);

Note that a full container will not poll the iterator, so even an 'obviously empty' iterator will cause the function to return true.

let mut full = Some(0);
assert_eq!(full.checked_fill(None).is_some(), true);
Loading content...

Implementations on Foreign Types

impl<T> Fill<T> for Vec<T>[src]

Fills the vector as far as possible without reallocating.

impl<T> Fill<T> for VecDeque<T>[src]

Fills the queue as far as possible without reallocating.

impl<T> Fill<T> for Option<T>[src]

If the option does not yet contain an item, insert the next item of the iterator.

Note that this will call iter.next() at most once. The option will still be empty if the iterator is not fused and returns None as the first result of that call.

impl<C, E, T> Fill<Result<T, E>> for Result<C, E> where
    C: Fill<T>, 
[src]

Tries to fill the container in the Ok.

A result is full if it contains an error, or if the container in Ok is full.

impl<'_, T> Fill<T> for IterMut<'_, T>[src]

Advances self while inserting elements from the iterator.

impl<'a, '_, T: 'a> Fill<&'a mut T> for IterMut<'_, T>[src]

Advances self while swapping elements with the iterator.

Loading content...

Implementors

impl Fill<()> for ()[src]

Consumes all items until the first None.

Loading content...