VecDrainWhereExt

Trait VecDrainWhereExt 

Source
pub trait VecDrainWhereExt<Item> {
    // Required method
    fn e_drain_where<F>(&mut self, predicate: F) -> VecDrainWhere<'_, Item, F> 
       where F: FnMut(&mut Item) -> bool;
}
Expand description

Ext. trait adding e_drain_where to Vec

Required Methods§

Source

fn e_drain_where<F>(&mut self, predicate: F) -> VecDrainWhere<'_, Item, F>
where F: FnMut(&mut Item) -> bool,

Drains all elements from the vector where the predicate is true.

Note that dropping the iterator early will stop the process of draining. So for example if you add an combinator to the drain iterator which short circuits (e.g. any/all) this will stop draining once short circuiting is hit. So use it with care.

you can use fold e.g. any(pred) => `fold(false, |s| )

§Leak Behavior

For safety reasons the length of the original vector is set to 0 while the drain iterator lives.

§Panic/Drop Behavior

When the iterator is dropped due to an panic in the predicate the element it panicked on is leaked but all elements which have already been decided to not be drained and such which have not yet been decided about will still be in the vector safely. I.e. if the panic also causes the vector to drop they are normally dropped if not the vector still can be normally used.

§Tip: non iterator short circuiting all/any

Instead of iter.any(pred) use iter.fold(false, |s,i| s|pred(i)).

Instead of iter.all(pred) use iter.fold(true, |s,i| s&pred(i)).

And if it is fine to not call pred once it’s found/has show to not hold but it’s still required to run the iterator to end in the normal case replace the | with || and the & with &&.

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<Item> VecDrainWhereExt<Item> for Vec<Item>

Source§

fn e_drain_where<F>(&mut self, predicate: F) -> VecDrainWhere<'_, Item, F>
where F: FnMut(&mut Item) -> bool,

Implementors§