Skip to content

Iterator::fuse is not guaranteed to fuse a generic iterator. #83969

Closed
@lcnr

Description

@lcnr
struct S(bool);

impl Iterator for S {
    type Item = ();
    fn next(&mut self) -> Option<()> {
        self.0 = !self.0;
        if self.0 {
            Some(())
        } else {
            None
        }
    }
}

impl std::iter::FusedIterator for S {}

fn main() {
    let mut x = S(false).fuse();
    for _i in &mut x {}
    // x is fused, so it must be empty.
    assert_eq!(x.next(), None);
}

playground

This means unsafe code may not rely on iter.fuse() to actually fuse the iterator if iter is generic, which is unexpected to me.
I personally would like us to either add a note to Iterator::fuse mentioning this or to remove the specialization for std::iter::Fused.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions