You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Box<[T]> should have an IntoIter implementation. #59878
Right now the only way to move out of a Box<[T]> is using into_vec, which is a bit inconvenient. Is there any reason Box<[T]> shouldn't implement IntoIter?
Reacted by dbdr, Calvin, Ken Reed, Caleb Sander Mateos, EFanZh, Eric Langlois, Sunyeop Lee, David Hoppenbrouwers, Jakub Beránek, Chayim Refael Friedman and 41 more
Unfortunately this has the same issue as #25725: boxed_slice.into_iter() works today through auto-deref (then auto-ref?) to <&[T] as IntoIterator>::into_iter, so adding this impl would change the meaning of (some) existing code.
Whatever we end up deciding should probably apply to both.
So we could add the implementation today, using Vec::from(self).into_iter() to make a vec::IntoIter? Seems like there wouldn't even be a need to make a new iterator type...
The user could work around this by going through Vec, and the nice thing is that this has no real cost -- it's just a bit awkward.
But if std did this, we would run into the same problems as arrays, where calling into_iter()today on a Box<[T]> gets you a slice::Iter. So it's a breaking change to add IntoIterator -- technically minor and allowable, but needs caution.
@SimonSapin Do you think it would be a good idea to add a future-incompatibility lint for this, like we did for the array.into_iter() problem (#66145)? In particular, because you said "Whatever we end up deciding should probably apply to both." This was suggested here.
error[E0119]: conflicting implementations of trait `core::iter::IntoIterator` for type `boxed::Box<[_], _>`:
--> library/alloc/src/boxed.rs:1578:1
|
1578 | impl<T, A> IntoIterator for Box<[T], A> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<I> IntoIterator for I
where I: Iterator;
= note: upstream crates may add a new impl of trait `core::iter::Iterator` for type `[_]` in future versions
error[E0119]: conflicting implementations of trait `core::iter::IntoIterator` for type `boxed::Box<[_; _], _>`:
--> library/alloc/src/boxed.rs:1589:1
|
1589 | impl<T, A, const N: usize> IntoIterator for Box<[T; N], A> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<I> IntoIterator for I
where I: Iterator;
= note: upstream crates may add a new impl of trait `core::iter::Iterator` for type `[_; _]` in future versions
This has to do with impl<I: Iterator + ?Sized, A> Iterator for Box<I, A> to complete the conflict, although we know that we would never add direct Iterator for [T] or [T; N]. I don't know if we could hack around that with a lang item or something.
We discussed this in the @rust-lang/libs meeting today.
Our consensus was that we'd be happy to see this impl, as long as doing so was possible and wouldn't break backwards compatibility. It's possible that the machinery proposed for supporting [].into_iter() might help here; if that's the case, and this would potentially benefit from the edition transition, we'd love to see an implementation making use of that.
The coherence issue @cuviper reported seems orthogonal to that, though.
@yaahc This might be another good use case for your negative trait implementation feature. E.g. impl !Iterator for [_; _]; to promise we will never add the trait implementation that rustc is worried about.
Reacted by scottmcm, Jane Losare-Lusby and Mathspy
Are there any updates on this? It does seem like a waste of the edition update if this isn't going into Rust 2021. There're a number of places in our codebase where we're doing Vec::from(boxed_slice).into_iter() currently, which would benefit from this.
I wouldn't mind spending some time to get this working, if we're just waiting for someone to get around to writing the implementation. (But I've never contributed to Rust before, so I might need some guidance.)
Are there any updates on this? It does seem like a waste of the edition update if this isn't going into Rust 2021. There're a number of places in our codebase where we're doing Vec::from(boxed_slice).into_iter() currently, which would benefit from this.
I wouldn't mind spending some time to get this working, if we're just waiting for someone to get around to writing the implementation. (But I've never contributed to Rust before, so I might need some guidance.)
I believe the edition cutoff has already passed quite a few months ago, so I doubt it's possible for this to make it into the 2021 edition but there may be some ways we can make it work backwards compatibly as @joshtriplett mentioned. AFAIK this is just waiting on someone to get around to writing the implementation so if this is something you're interested in please take a crack at it @btzy!
Activity
SimonSapin commentedon Apr 11, 2019
Unfortunately this has the same issue as #25725:
boxed_slice.into_iter()works today through auto-deref (then auto-ref?) to<&[T] as IntoIterator>::into_iter, so adding this impl would change the meaning of (some) existing code.Whatever we end up deciding should probably apply to both.
CC @rust-lang/libs
SimonSapin commentedon Apr 11, 2019
Unlike with arrays though, this case has a work-around in converting though
Vec<T>.scottmcm commentedon Apr 16, 2019
So we could add the implementation today, using
Vec::from(self).into_iter()to make avec::IntoIter? Seems like there wouldn't even be a need to make a new iterator type...cuviper commentedon Apr 16, 2019
The user could work around this by going through
Vec, and the nice thing is that this has no real cost -- it's just a bit awkward.But if
stddid this, we would run into the same problems as arrays, where callinginto_iter()today on aBox<[T]>gets you aslice::Iter. So it's a breaking change to addIntoIterator-- technically minor and allowable, but needs caution.SimonSapin commentedon Apr 16, 2019
@scottmcm Yes, the implementation is easy and does not require a new type. The concern is only in the change being breaking.
LukasKalbertodt commentedon Dec 29, 2019
@SimonSapin Do you think it would be a good idea to add a future-incompatibility lint for this, like we did for the
array.into_iter()problem (#66145)? In particular, because you said "Whatever we end up deciding should probably apply to both." This was suggested here.cuviper commentedon Mar 28, 2021
FWIW, I just tried adding
Box<[T]>andBox<[T; N]>to see what would break, and they fail immediately:This has to do with
impl<I: Iterator + ?Sized, A> Iterator for Box<I, A>to complete the conflict, although we know that we would never add directIterator for [T]or[T; N]. I don't know if we could hack around that with alangitem or something.joshtriplett commentedon May 19, 2021
We discussed this in the @rust-lang/libs meeting today.
Our consensus was that we'd be happy to see this impl, as long as doing so was possible and wouldn't break backwards compatibility. It's possible that the machinery proposed for supporting
[].into_iter()might help here; if that's the case, and this would potentially benefit from the edition transition, we'd love to see an implementation making use of that.The coherence issue @cuviper reported seems orthogonal to that, though.
m-ou-se commentedon Jun 10, 2021
@yaahc This might be another good use case for your negative trait implementation feature. E.g.
impl !Iterator for [_; _];to promise we will never add the trait implementation that rustc is worried about.btzy commentedon Jul 5, 2021
Are there any updates on this? It does seem like a waste of the edition update if this isn't going into Rust 2021. There're a number of places in our codebase where we're doing
Vec::from(boxed_slice).into_iter()currently, which would benefit from this.I wouldn't mind spending some time to get this working, if we're just waiting for someone to get around to writing the implementation. (But I've never contributed to Rust before, so I might need some guidance.)
yaahc commentedon Aug 6, 2021
I believe the edition cutoff has already passed quite a few months ago, so I doubt it's possible for this to make it into the 2021 edition but there may be some ways we can make it work backwards compatibly as @joshtriplett mentioned. AFAIK this is just waiting on someone to get around to writing the implementation so if this is something you're interested in please take a crack at it @btzy!
Auto merge of rust-lang#124097 - compiler-errors:box-into-iter, r=Waf…
traviscross commentedon Jun 18, 2024
This has now happened in:
IntoIteratorforBox<[T]>+ edition 2024-specific lints #124097...and is expected to stabilize with Rust 1.80.
So we can close this issue.