-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add methods to access the wrapped iterator of Cloned
#127517
Conversation
rustbot has assigned @Mark-Simulacrum. Use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I wanted to call `.as_slice()` on a `slice::Iter`, but the iterator was wrapped in a `.cloned()` call.
Imo dconstructing adapters does not set a good precedent because ultimately it's not possible to do this for all adapters or would constrain future implementation changes. And since it wouldn't be available for many adapters it wouldn't carry well across refactorings for users. So it'd be better to encourage the use of See rust-lang/libs-team#128 for a general discussion on the topic |
This seems more like a slippery slope argument. I think on
The iterator is part of my data structure, so this would need self-referential types to work. |
It doesn't have to be. Cloned has no state, so you can keep the slice iterator and build a |
Indeed, it doesn't have to be. But this means that But maybe the standard library also wants the iterator adapters to be embeddable into data structures (and not just reimplemented everywhere). |
And there's the slope 😉 . If we add it there'd be questions why it's on this adapter and not some other one. Making it a general feature is incompatible with some adapters. E.g.
That's not correct. With associated type specialization structs can substitute their default fields with optimized representations for specific types, which would be incompatible with returning a reference to the inner iterator. And as the StepBy specialization shows that with some limitations we don't even need to wait for the associated type kind to do some creative reinterpretation of inner structs. And since such transformations can be slightly lossy it would also make So it very much does bind the future. To clarify, I do get that this is quite a convenient feature. The standard library itself even unpacks iterator abstractions in various places. It's just that the usefulness-to-users conflicts with useful-to-the-implementation properties that are enabled by having exclusive, private access to internals. So if possible it's preferable that people use |
I see. So the question is really: Can we imagine an optimized implementation for
That's not possible in my case. I can construct the iterator on the fly, but then I'd lose out on the optimization benefits. So the correct solution might just be to reimplement all the iterator abstractions on my own so that I can also access the inner iterator. |
That is quite hard indeed. Not only does it depend on future optimizations but also future iterator types that might require new optimizations. A simple "existence proof" for a) involves lending iterators. When you have an unfortunate API that takes a lending iterator but the underlying source is owning then we may be able to excise the step that takes an owned item and turns it into a short-lived reference which the consumer then clones and just return the original item. By unpacking the iterator two levels deep this would no longer contain the field that temporarily holds the owned value that's being lent. b) might involve somthing reference-counted where if
Which ones are relevant for you? |
For future optimization potential, we don't want to guarantee that |
I wanted to call
.as_slice()
on aslice::Iter
, but the iterator was wrapped in a.cloned()
call.