Skip to content
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

Tracking issue for slice::IterMut::as_slice and vec::Drain::as_slice #58957

Closed
cuviper opened this issue Mar 6, 2019 · 10 comments · Fixed by #82771
Closed

Tracking issue for slice::IterMut::as_slice and vec::Drain::as_slice #58957

cuviper opened this issue Mar 6, 2019 · 10 comments · Fixed by #82771
Labels
A-collections Area: `std::collection` A-slice Area: `[T]` B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. Libs-Small Libs issues that are considered "small" or self-contained Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@cuviper
Copy link
Member

cuviper commented Mar 6, 2019

This issue tracks stabilization for 2 methods added in #58924:

  • core::slice::IterMut::as_slice
  • alloc::vec::Drain::as_slice
@cuviper cuviper added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. labels Mar 6, 2019
@clarfonthey
Copy link
Contributor

If this feature is accepted, they should implement AsRef as well.

@cuviper
Copy link
Member Author

cuviper commented Oct 28, 2019

Good point, I didn't notice that slice::Iter already implements AsRef<[T]>. Another candidate that could is vec::IntoIter, and maybe there are more to consider for completeness.

@clarfonthey
Copy link
Contributor

I actually didn't notice either until I went digging in for a totally separate issue. Will have a PR for that separately, although I won't include AsRef<[T]> for IterMut<'_, T> because it'd be instantly stable, and I'm not sure whether we want to guarantee that.

@talchas
Copy link

talchas commented Nov 10, 2019

Is there a reason to not add as_slice_mut for these? Particularly IterMut, where it makes the iteration over mutable pairs nicer.

@clarfonthey
Copy link
Contributor

@talchas Only into_slice_mut is sound, and that's offered at the moment. Otherwise, you can have multiple mutable borrows to the same spot in memory.

@talchas
Copy link

talchas commented Nov 10, 2019

&mut self -> &mut [T] should be just as sound as &self -> &[T] here. It could not be &'a mut [T], but that's true for & as well.

@cuviper
Copy link
Member Author

cuviper commented Nov 11, 2019

Yes, as long as it borrows from the IterMut itself, not its parameterized lifetime, as_slice_mut should be fine. I guess for mutable pairs, you mean something like this?

let mut iter = slice.iter_mut();
while let Some(x) = iter.next() {
    for y in iter.as_slice_mut() {
        // x and y are distinct `&mut T`
    }
}

@talchas
Copy link

talchas commented Nov 12, 2019

Yeah, exactly that.

@CAD97
Copy link
Contributor

CAD97 commented May 25, 2020

What is the status of this? Is anything required for stabilization other than someone to propose stabilization, make a stabilization PR, and FCP?

In rowan, rust-analyzer's syntax tree backend, we're currently trying to remove unneeded allocations from syntax node cache hits. A simple patch that just does so via an arrayvec already sees a 5% improvement on rust-analyzer's main integration benchmark (not rowan's, and not purely parsing!), but being able to use the fact that vec::Drain already has a non-destructively-iterable array backing would make even that collect/copy unnecessary.

RalfJung added a commit to RalfJung/rust that referenced this issue Jun 8, 2020
impl AsRef<[T]> for vec::IntoIter<T>

Adds `impl<T> AsRef<[T]> for vec::IntoIter<T>`. This mirrors the same trait impl for [`slice::Iter`](https://doc.rust-lang.org/nightly/std/slice/struct.Iter.html). Both types already offer `fn as_slice(&self) -> &[T]`, this just adds the trait impl for `vec::IntoIter`.

If/when `fn as_slice(&self) -> &[T]` stabilizes for `vec::Drain` and `slice::IterMut`, they should get `AsRef<[T]>` impls as well. As thus, tangentially related to rust-lang#58957.

My ultimate goal here: being able to use `for<T, I: Iterator<Item=T> + AsRef<[T]>> I` to refer to `vec::IntoIter`, `vec::Drain`, and eventually `array::IntoIter`, as an approximation of the set of by-value iterators that can be "previewed" as by-ref iterators. (Actually expressing that as a trait requires GAT.)
RalfJung added a commit to RalfJung/rust that referenced this issue Jun 15, 2020
Stabilize vec::Drain::as_slice

and add `AsRef<[T]> for Drain<'_, T>`.

Tracking issue: rust-lang#58957. Does not stabilize `slice::IterMut::as_slice` yet. cc @cuviper
This PR proposes stabilizing just the `vec::Drain::as_slice` part of that tracking issue.

My ultimate goal here: being able to use `for<T, I: Iterator<Item=T> + AsRef<[T]>> I` to refer to `vec::IntoIter`, `vec::Drain`, and eventually `array::IntoIter`, as an approximation of the set of by-value iterators that can be "previewed" as by-ref iterators. (Actually expressing that as a trait requires GAT.)
RalfJung added a commit to RalfJung/rust that referenced this issue Jun 15, 2020
Stabilize vec::Drain::as_slice

and add `AsRef<[T]> for Drain<'_, T>`.

Tracking issue: rust-lang#58957. Does not stabilize `slice::IterMut::as_slice` yet. cc @cuviper
This PR proposes stabilizing just the `vec::Drain::as_slice` part of that tracking issue.

My ultimate goal here: being able to use `for<T, I: Iterator<Item=T> + AsRef<[T]>> I` to refer to `vec::IntoIter`, `vec::Drain`, and eventually `array::IntoIter`, as an approximation of the set of by-value iterators that can be "previewed" as by-ref iterators. (Actually expressing that as a trait requires GAT.)
@KodrAus KodrAus added A-slice Area: `[T]` A-collections Area: `std::collection` I-nominated Libs-Small Libs issues that are considered "small" or self-contained Libs-Tracked Libs issues that are tracked on the team's project board. labels Jul 29, 2020
@m-ou-se
Copy link
Member

m-ou-se commented Nov 20, 2020

One of thse (vec::Drain::as_slice) is already stabilized. As for the other one: we discussed this tracking issue in the libs meeting, and would be happy to see a stabilization PR.

emilio added a commit to emilio/rust that referenced this issue Mar 4, 2021
Much like rust-lang#72584.

As per rust-lang#58957 there's no blocker for this, and I wanted to use this
today :-)
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 21, 2021
slice: Stabilize IterMut::as_slice.

Much like rust-lang#72584.

As per rust-lang#58957 there's no blocker for this, and I wanted to use this
today :-)

Closes rust-lang#58957
@bors bors closed this as completed in 34285de Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` A-slice Area: `[T]` B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. Libs-Small Libs issues that are considered "small" or self-contained Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants