Repository navigation
Conversation
|
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. |
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. |
This comment has been minimized.
This comment has been minimized.
|
r? libs |
|
r? libs |
|
@rfcbot fcp merge libs |
|
@Mark-Simulacrum has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns. |
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.
b658092 to
3fb78b8
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
I think we should register a formal concern for |
|
Is doing this a blocker to later adding InfiniteIterator? It seems like we should be able to replace this impl with that more general impl if/when we decide to do that? |
|
It could, but we're also setting the precedent that we are fine just adding in cases that would be better suited for that trait in the meantime, and I propose we don't do that. Essentially, proposing that only two options are acceptable:
The third option ends up kind of saying that we do this for |
|
@rfcbot fcp concern infinite-iterator Ok, I'll cut a concern on your behalf. I'm not sure I would jump to a general solution until we have at least a few cases, but 🤷 no strong opinion on it. Interested in other opinions on this. It does seem like it'd be good to cut an ACP for infinite iterator in that case... |
|
I mean, strictly speaking, I was providing a compelling case based upon Scott's comment which included
|
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.
|
Just a refresher, since this issue is a little old: A more general solution runs into problems (InfiniteIterator hits some overlapping instances). Since I don't see a way to actually get a more general solution in, (apart from possibly adding it to a new language edition?), I still think that merging this PR is a good idea. edit: Perfect is the enemy of good, and all that. |
View all comments
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.