Implement ExactSizeIterator for Zip<A, Repeat<B>>#146642
Implement ExactSizeIterator for Zip<A, Repeat<B>>#146642414owen wants to merge 1 commit intorust-lang:mainfrom
Zip<A, Repeat<B>>#146642Conversation
|
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
09aac55 to
8eb55d7
Compare
Zip<A, Repeat>Zip<A, Repeat<B>>
|
Cool idea! I think they could/should also be rust/library/alloc/src/vec/mod.rs Lines 3811 to 3840 in eec6bd9 |
8eb55d7 to
49894ee
Compare
|
Thanks @yotamofek :) There's already a: unsafe impl<A, B> TrustedLen for Zip<A, B>
where
A: TrustedLen,
B: TrustedLen,
{
}And since |
|
Ah right, sorry, missed that! |
|
I think what this really wants it an |
|
@scottmcm I had the same thought, but I figured this would be easier to get through, first. |
|
RepeatWith could also be added to the list @scottmcm mentioned. Such a trait would also scale better outside std in case other iterators outside std can both implement said trait and make use of said trait for their own optimisations. |
Where:
A: ExactSizeIterator,
B: Clone,
And the symmetrical instance.
The only other instance for `ExactSizeIterator<Zip<_, _>>` requires each
zipped iterator to be `ExactSizeIterator`, and since `Repeat` does not
implement `ExactSizeIterator`, these instances don't overlap with
anything.
49894ee to
b658092
Compare
|
Although for The instance for |
This enables more ExactSizeIterator instances. Previously, in rust-lang#146642, I sought to add specific instances for `Zip<Repeat<A>, I>` and its symmatrical mirror. Introducing `InfiniteIterator` provides much broader support for `ExactSizeIterator`. For example, ```rust [1, 2, 3].chain(repeat(1)).take(5) ``` Will now happily resolve to an instance of `ExactSizeIterator`. The downside of this approach is that, to avoid the overlapping instance with `Zip`, I had to introduce a negative trait bound, which, IIUC, isn't available outside of the compiler. If anyone knows od a better way to handle the overlapping instances, or a way I can expose something which triggers the negative instance, that would be very helpful.
|
r? libs-api |
This enables more ExactSizeIterator instances. Previously, in rust-lang#146642, I sought to add specific instances for `Zip<Repeat<A>, I>` and its symmatrical mirror. Introducing `InfiniteIterator` provides much broader support for `ExactSizeIterator`. For example, ```rust [1, 2, 3].chain(repeat(1)).take(5) ``` Will now happily resolve to an instance of `ExactSizeIterator`. The downside of this approach is that, to avoid the overlapping instance with `Zip`, I had to introduce a negative trait bound, which, IIUC, isn't available outside of the compiler. If anyone knows of a better way to handle the overlapping instances, or a way I can expose something which triggers the negative instance, that would be very helpful. There's also a missing symmetrical instance for `Chain`. Solutions are welcome.
This enables more ExactSizeIterator instances, specifically, those for `Take`, where the sub-iterator is infinite, and `Zip` where the one sub-iterator is infinite, and the other is exact sized. Previously, in rust-lang#146642, I sought to add specific instances for `Zip<Repeat<A>, I>` and its symmatrical mirror. Introducing `InfiniteIterator` provides much broader support for `ExactSizeIterator`. For example, ```rust [1, 2, 3].chain(repeat(1)).take(5) ``` Will now happily resolve to an instance of `ExactSizeIterator`. The downside of this approach is that, to avoid the overlapping instance with `Zip`, I had to introduce a negative trait bound, which, IIUC, isn't available outside of the compiler. If anyone knows of a better way to handle the overlapping instances, or a way I can expose something which triggers the negative instance, that would be very helpful. There's also a missing symmetrical instance for `Chain`. Solutions are welcome.
This enables more ExactSizeIterator instances, specifically, those for `Take`, where the sub-iterator is infinite, and `Zip` where the one sub-iterator is infinite, and the other is exact sized. Previously, in rust-lang#146642, I sought to add specific instances for `Zip<Repeat<A>, I>` and its symmatrical mirror. Introducing `InfiniteIterator` provides much broader support for `ExactSizeIterator`. For example, ```rust [1, 2, 3].into_iter().chain(repeat(1)).take(5) ``` Will now happily resolve to an instance of `ExactSizeIterator`. The downside of this approach is that, to avoid the overlapping instance with `Zip`, I had to introduce a negative trait bound, which, IIUC, isn't available outside of the compiler. If anyone knows of a better way to handle the overlapping instances, or a way I can expose something which triggers the negative instance, that would be very helpful. There's also a missing symmetrical instance for `Chain`. Solutions are welcome.
This enables more ExactSizeIterator instances, specifically, those for `Take`, where the sub-iterator is infinite, and `Zip` where the one sub-iterator is infinite, and the other is exact sized. Previously, in rust-lang#146642, I sought to add specific instances for `Zip<Repeat<A>, I>` and its symmatrical mirror. Introducing `InfiniteIterator` provides much broader support for `ExactSizeIterator`. For example, ```rust [1, 2, 3].into_iter().chain(repeat(1)).take(5) ``` Will now happily resolve to an instance of `ExactSizeIterator`. The downside of this approach is that, to avoid the overlapping instance with `Zip`, I had to introduce a negative trait bound, which, IIUC, isn't available outside of the compiler. If anyone knows of a better way to handle the overlapping instances, or a way I can expose something which triggers the negative instance, that would be very helpful. There's also a missing symmetrical instance for `Chain`. Solutions are welcome.
|
@scottmcm @VorpalBlade please see #146668, and the comment I added there. |
Where:
A: ExactSizeIterator,
B: Clone,
And the symmetrical instance.
The only other instance for
ExactSizeIterator<Zip<_, _>>requires each zipped iterator to beExactSizeIterator, and sinceRepeatdoes not implementExactSizeIterator, these instances don't overlap with anything.I posted on the internals forum, and was told these instances would be
insta-stable.The
stableattribute seems to requirefeatureandsinceattributes, which I'm not entirely sure I've done correctly. Pointers are welcome.