d939f708d9
Fix bugs in Peekable and Flatten when using non-fused iterators I fixed a couple of bugs with regard to the `Peekable` and `Flatten`/`FlatMap` iterators when the underlying iterator isn't fused. For testing, I also added a `NonFused` iterator wrapper that panics when `next` or `next_back` is called on an iterator that has returned `None` before, which will hopefully make it easier to spot these mistakes in the future. ### Peekable `Peekable::next_back` was implemented as ```rust self.iter.next_back().or_else(|| self.peeked.take().and_then(|x| x)) ``` which is incorrect because when the `peeked` field is `Some(None)`, then `None` has already been returned from the inner iterator and what it returns from `next_back` can no longer be relied upon. `test_peekable_non_fused` tests this. ### Flatten When a `FlattenCompat` instance only has a `backiter` remaining (i.e. `self.frontiter` is `None` and `self.iter` is empty), then `next` will call `self.iter.next()` every time, so the `iter` field needs to be fused. I fixed it by giving it the type `Fuse<I>` instead of `I`, I think this is the only way to fix it. `test_flatten_non_fused_outer` tests this. Furthermore, previously `FlattenCompat::next` did not set `self.frontiter` to `None` after it returned `None`, which is incorrect when the inner iterator type isn't fused. I just delegated it to `try_fold` because that already handles it correctly. `test_flatten_non_fused_inner` tests this. r? @scottmcm |
||
---|---|---|
.. | ||
array | ||
benches | ||
char | ||
convert | ||
fmt | ||
future | ||
hash | ||
iter | ||
macros | ||
mem | ||
num | ||
ops | ||
prelude | ||
ptr | ||
slice | ||
str | ||
sync | ||
task | ||
tests | ||
unicode | ||
alloc.rs | ||
any.rs | ||
ascii.rs | ||
bool.rs | ||
borrow.rs | ||
Cargo.toml | ||
cell.rs | ||
clone.rs | ||
cmp.rs | ||
default.rs | ||
ffi.rs | ||
hint.rs | ||
internal_macros.rs | ||
intrinsics.rs | ||
lib.rs | ||
marker.rs | ||
option.rs | ||
panic.rs | ||
panicking.rs | ||
pin.rs | ||
primitive.rs | ||
raw.rs | ||
result.rs | ||
time.rs | ||
tuple.rs | ||
unit.rs |