Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain iterator adaptor shold drop exhausted subiterator #66031

Open
matklad opened this issue Nov 1, 2019 · 1 comment
Open

Chain iterator adaptor shold drop exhausted subiterator #66031

matklad opened this issue Nov 1, 2019 · 1 comment
Labels
A-iterators Area: Iterators C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@matklad
Copy link
Member

matklad commented Nov 1, 2019

Consider the following program:

use std::sync::mpsc::channel;

fn main() {
    let (sender, reciever) = channel();

    let source = (1..10).map(move |i| {sender.send(i).unwrap(); i});
    let sink = reciever;

    let iter = source.chain(sink);
    for x in iter {
        println!("{}", x);
    }

    println!("done");
}

Currently, it deadlocks. It can be argued, however, that this program should finish, given that "equivalent" program without chain finishes:

use std::sync::mpsc::channel;

fn main() {
    let (sender, reciever) = channel();

    let source = (1..10).map(move |i| {sender.send(i).unwrap(); i});
    let sink = reciever;

    for x in source {
        println!("{}", x)
    }
    for x in sink {
        println!("{}", x);
    }

    println!("done");
}

I think this can be achieved by changing the Chain definition from

struct Chain<A, B> {
  left: A,
  right: B,
  state: State,
}

enum State { Left, Right, Both }

to

struct Chain<A, B> {
  state: State<A, B>,
}

enum State<A, B> { Left(A), Right(B), Both(A, B) }

this will require some unsafe code to flip from Both to Left, using only &mut Both, but should be doable.

Context: I've discovered similarly-shaped code when trying to simplify this code. Basically, this is an event loop, which works on the stream of events, and some events can feed new events back into the loop. The goal is to make sure the loop is cleanly exhausted.

Not sure if this actually worth it, given super-obscure use-case and potential complications during implementation...

cc @bluss

@matklad matklad added the A-iterators Area: Iterators label Nov 1, 2019
@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Mar 22, 2020
@ssomers
Copy link
Contributor

ssomers commented Dec 22, 2021

Isn't this "accidentally" implemented through #70896? Playground seems happy to complete the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-iterators Area: Iterators C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants