Skip to content

Conversation

@elenaf9
Copy link
Contributor

@elenaf9 elenaf9 commented May 7, 2025

Based on discussion in #8 and draft #9.

Notes

I renamed the functions to iter{_mut}_typed to avoid confusion with normal iter/ iter_mut and emphasize that streams that can't be downcasted will be skipped. Wdyt @thomaseizinger

Also, a major version bump is needed because it breaks the API with the additional Any bound, right?

Copy link
Owner

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I left a few comments with nits.

I don't think this is a breaking change because the docs for Any say:

Most types implement Any. However, any type which contains a non-'static reference does not. See the module-level documentation for more details.

We already require all streams and futures to be 'static so I think we are fine here. Having said that, I don't think it is a big deal either way and we can just also publish another minor release.

}
assert_eq!(streams.iter_typed::<mpsc::Receiver<()>>().count(), N);
for (i, (id, _)) in streams.iter_typed::<mpsc::Receiver<()>>().enumerate() {
let expect_id = format!("ID{}", N - i - 1); // Reverse order.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? Why are we handing them out in reverse order?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why, but that's how the inner SelectAll::iter returns the the elements. Probably an implementation detail of the SelectAll data structure?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. It is a Vec internally. Should we sort them by ID?

Copy link
Contributor Author

@elenaf9 elenaf9 May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can sort them in the SelectAll, can we? Or where/ how should we sort them?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could sort them as part of the Iterator implementation. itertools has a combinator for that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better left to the caller: callers who want a sorted iterator can use the combinator, and those who don't care don't have to pay the performance cost of sorting/comparisons (and extra temporary memory).

Documenting that the order is unspecified should be enough to inform people of the need for sorting.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documenting that the order is unspecified should be enough to inform people of the need for sorting.

Makes sense to me! Wanna send a PR?

@thomaseizinger
Copy link
Owner

Awesome! Thanks for pushing this forward. I'll merge this to main and would appreciate if you can experiment with it (using [patch.crates-io]) in rust-libp2p before we make a release.

@thomaseizinger thomaseizinger merged commit b524675 into thomaseizinger:main May 7, 2025
1 check passed
@elenaf9 elenaf9 deleted the iterators branch May 7, 2025 20:33
@thomaseizinger
Copy link
Owner

thomaseizinger commented May 7, 2025

Also, if someone is keen (@elenaf9 or @teor2345 ?), it would be great if we could do the same thing for the futures collections in here to keep feature-parity among all the collections :)

@thomaseizinger
Copy link
Owner

Awesome! Thanks for pushing this forward. I'll merge this to main and would appreciate if you can experiment with it (using [patch.crates-io]) in rust-libp2p before we make a release.

@elenaf9 Let me know once you need a release here.

@teor2345
Copy link
Contributor

Awesome! Thanks for pushing this forward. I'll merge this to main and would appreciate if you can experiment with it (using [patch.crates-io]) in rust-libp2p before we make a release.

@elenaf9 Let me know once you need a release here.

This is on my list of things to do, but I don't know how many days or weeks it will be until it reaches the top of the list. So @elenaf9 feel free to go ahead (and close my libp2p PR).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants