Closed Philippe-Cholet closed 6 months ago
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 94.56%. Comparing base (
6814180
) to head (0936c8d
). Report is 31 commits behind head on master.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
I'm exploring the possibility of buggy methods in the case of unfused iterators.
EDIT: Well, after a short investigation, I found that only the Err
case of exactly_one
is subject to weird behavior for an unfused iterator, PR to come.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b461adac3020a6f3e38087a56dd5cbd8 It contains an idea to test that our adaptors behave well (see #55).
I think we should fuse them: collect_vec
+sort
+truncate
should be the same as k_smallest
(see https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=8e6e791d95adc335900ea6b8a2bfd95d).
Explicitely fuse the iterator is also clearer than checking .len() == k
.
Done. And I did the same for the recent k_smallest_general
.
I'm shocked! 😲 I found a bug in my favorite method!
In the case of an unfused iterator yielding at most k-1 elements, then None, then other elements, the heap is not k-long and
for_each
is called on the other elements leadingdebug_assert_eq
to rightfully panic.Our recent variants of
k_smallest
do not have this bug (the.len() ==
check is present)!Alternatively, iterators could be fused:
let mut iter = iter.fuse();
.I probably should add a test about this.
Story: After recently reviewing #654 where I deeply looked at
k_smallest
and its variants and my really recent #899 where I similarly thought of checking the length of the collected elements before callingfor_each
, I eventually/luckily saw this bug.