Skip to content

Chain has an incorrect implementation of DoubleEndedIterator #26316

Closed
@eefriedman

Description

@eefriedman

Testcase:

struct CrazyIterator(bool);
impl CrazyIterator { fn new() -> CrazyIterator { CrazyIterator(false) } }
impl Iterator for CrazyIterator {
    type Item = i32;
    fn next(&mut self) -> Option<i32> {
        if self.0 { Some(1) } else { self.0 = true; None }
    }
}
impl DoubleEndedIterator for CrazyIterator {
    fn next_back(&mut self) -> Option<i32> {
        self.next()
    }
}

fn main() {
    println!("{}", CrazyIterator::new().chain(0..10).rev().any(|i| i > 10i32));
    println!("{}", (0..10).chain(CrazyIterator::new()).rev().any(|i| i > 10i32));
}

gives:

false
playpen: timeout triggered!

I'm pretty sure CrazyIterator isn't a legitimate implementation of DoubleEndedIterator, but the documentation doesn't say that explicitly, and it explicitly states that this is a legitimate implementation of Iterator. (If this is supposed to be allowed, std::iter::Chain has a bug.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-lowLow priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions