-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Draft: Add inner and into_inner methods to iterator adapters #103294
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
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon. Please see the contribution instructions for more information. |
|
This seems reasonable to me; the example of having a type that implements Iterator and also has other methods makes the most sense. I can imagine ways to confuse an iterator adapter by accessing the underlying iterator when not expected, but (Also, I don't think |
|
Hi josh! thanks for replying so quickly. I have created a tracking issue but there are still points that need discussion API wise. For example, in the zulip discussion, the following question surrounding
We can discuss it wherever is best between here, the tracking issue (#103302) or zulip. |
|
At least for It would also preclude some future optimizations that would similarly assume that internals are not observable once an iterator has been wrapped into an adapter. |
To achieve that you can do things like let token_iter = ...;
let borrowed_iter = token_iter.by_ref().filter(...);
// do something with borrowed_iter here
drop(borrowed_iter);
// token_iter becomes usable again
|
|
Given that this is a "new thing that every iterator adapter should think about supporting", I think it would be appropriate to have an ACP about it: https://std-dev-guide.rust-lang.org/feature-lifecycle/api-change-proposals.html. That's a better place to have a motivation discussion than a PR. I do think it's quite reasonable for the simple 1:1 adapters -- like for For a bunch of them, though, this being an infallible API is more of a commitment than you might be thinking. For example, Even for So for me, 👍 to the obvious ones, but I'm less certain about all of them. |
Those might also be the least interesting to expose though? E.g. the cited motivation (xmlparser::Tokenizer) doesn't return reference items, so one couldn't even use I have two questions: a) is |
|
Thanks a lot for your numerous comments
TIL, I would indeed need to dive into TrustedRandomAccess. Do you have a pointer to any doc/blog post about it by any chance?
Definitely. I guess this motivates further discussion? I'm not completely aware of what the decision process is for these things.
I could but the API would have been better served by not doing it in that case (because I want to hide the Moreover, I would still need an
I'll definitely write something then.
You are 100% right that this is a commitment I hadn't forseen. I guess it is to be discussed in the ACP?
No, cf. above. I would like
I don't understand what "unspool" means. Thanks again to the both of you for your precious feedback! |
|
I have opened the APC following your advice: |
What I was suggesting is to introduce a new adapter We already have an internal unsafe trait that does almost that, but it's for a slightly different purpose. The explicit struct could be optional. Instead it could be implemented for |
|
Closing per rust-lang/libs-team#128 (comment) |

Hi!
I'm aware that I'm not exactly following the normal order for adding things to the library but I wanted to try the contributing workflow and it was pretty quick to add a minimal example of what I'd like to do.
The idea is that many iterator adapters own the underlying iterator. In my use-case, the iterator wrapped in a
Peekablewas xmlparser::Tokenizer which has other methods I may want to access to. I also wanted my struct (a parser for a XML-based language) to own the underlying tokenizer, and was hence missinginner().To be more general, I think (and propose to implement) that all relevant iterator adapters get
inner()andinto_inner()methods.PS: I've tried to see if it was discussed before but didn't find any reference. Let me know if I missed something.
ACP: rust-lang/libs-team#128