Skip to content

core::iter::Fuse Does Not Fuse Iterators #100518

Open
@asquared31415

Description

@asquared31415

This is acknowledged in Iterator::fuse with

fuse() may therefore behave incorrectly if the FusedIterator trait is improperly implemented.

But I believe this should not be the case. Fuse still incurs a memory overhead for all iterators, whether or not it specializes, and it's useless because it currently doesn't actually do anything in the general case.

Example code:

struct UwU<'a>(&'a (), bool);

impl<'a> Iterator for UwU<'a> {
    type Item = bool;
    fn next(&mut self) -> Option<Self::Item> {
        self.1 = !self.1;
        match self.1 {
            true => Some(true),
            false => None,
        }
    }
}

impl<'a> std::iter::FusedIterator for UwU<'a> { }

fn main() {
    let a = ();
    let mut owo = UwU(&a, true).fuse();
    assert_eq!(owo.next(), None);

    // this should ideally hold because we called `fuse`, and 
    // the first call was `None` but it returns `Some`
    assert_eq!(owo.next(), None);
}

It fails with

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Some(true)`,
 right: `None`', /app/example.rs:23:5

Since rustc 1.26.0, when this type was added, through current nightly (2022-08-13).

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