#vec #drain-filter #drain-where

vec-drain-where

alternative Vec::drain_filter impl

2 stable releases

Uses old Rust 2015

1.0.1 Oct 11, 2019
1.0.0 Aug 27, 2018

#365 in Algorithms

Download history 22/week @ 2021-02-25 19/week @ 2021-03-04 15/week @ 2021-03-11 18/week @ 2021-03-18 25/week @ 2021-03-25 15/week @ 2021-04-01 23/week @ 2021-04-08 31/week @ 2021-04-15 21/week @ 2021-04-22 25/week @ 2021-04-29 20/week @ 2021-05-06 21/week @ 2021-05-13 26/week @ 2021-05-20 21/week @ 2021-05-27 16/week @ 2021-06-03 23/week @ 2021-06-10

101 downloads per month
Used in 8 crates (via total-order-multi-map)

MIT/Apache

14KB
222 lines

vec-drain-where

A alternate Vec::drain_filter implementation, slightly diverging from the std's implementation.

This crate provides an extension trait adding a e_drain_where method (the e_ prefix is used to prevent name collisions with std as the currently drain_filter might be stabilized as drain_where).

e_drain_where as one large difference to drain filter. It doesn't run to completion when dropped and can as such be "early stopped" from the outside by stopping the iteration an dropping the iterator.

The reason why Vec::drain_filter does run the drain to completion on drop is that it can be quite confusing. E.g. the code:

vec.drain_filter(|x|x.should_be_removed()).any(|x|x.had_fatal_error())

Would not necessary do what it's expected to do, i.e. it would drain thinks until it finds any drained value with had_fatal_error() == true and then would stop draining (with e_drain_where).

But running to completion on drop is also tricky/dangerous e.g. it can lead easily to thinks like panic's on drop and as such double panics, while drop on panic behavior for Vec::drain_filter might still change before stabilization, this crate completely avoids the problem at cost of making it easy to accidentally stop the draining to early.

Documentation

Documentation can be viewed on docs.rs. (at least once it's published ;=) )

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps