Function webrender::util::drain_filter

source ·
pub fn drain_filter<T, Filter, Action>(
    vec: &mut Vec<T>,
    filter: Filter,
    action: Action
)where
    Filter: FnMut(&mut T) -> bool,
    Action: FnMut(T),
Expand description

Run the first callback over all elements in the array. If the callback returns true, the element is removed from the array and moved to a second callback.

This is a simple implementation waiting for Vec::drain_filter to be stable. When that happens, code like:

let filter = |op| { match *op { Enum::Foo | Enum::Bar => true, Enum::Baz => false, } }; drain_filter( &mut ops, filter, |op| { match op { Enum::Foo => { foo(); } Enum::Bar => { bar(); } Enum::Baz => { unreachable!(); } } }, );

Can be rewritten as:

let filter = |op| { match *op { Enum::Foo | Enum::Bar => true, Enum::Baz => false, } }; for op in ops.drain_filter(filter) { match op { Enum::Foo => { foo(); } Enum::Bar => { bar(); } Enum::Baz => { unreachable!(); } } }

See https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain_filter