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

Implement From<{&,&mut} [T; N]> for Vec<T> where T: Clone #220

Closed
EFanZh opened this issue May 7, 2023 · 2 comments
Closed

Implement From<{&,&mut} [T; N]> for Vec<T> where T: Clone #220

EFanZh opened this issue May 7, 2023 · 2 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@EFanZh
Copy link

EFanZh commented May 7, 2023

Proposal

Problem statement

Currently, we have From<{&,&mut} [T]> for Vec<T> where T: Clone, I think we can also implement From<{&,&mut} [T; N]> for Vec<T> where T: Clone.

Motivation, use-cases

Suppose I want to convert an &[[u32; 4]] value to a Vec<Vec<u32>> type. Currently, I can write:

  • items.iter().map(|item| item.to_vec()).collect::<Vec<_>>();
  • items.iter().copied().map(Vec::from).collect::<Vec<_>>();

I want to be able to write:

  • items.iter().map(Vec::from).collect::<Vec<_>>();

Solution sketches

Implement From<{&,&mut} [T; N]> for Vec<T> where T: Clone.

Links and related work

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

@EFanZh EFanZh added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels May 7, 2023
@the8472
Copy link
Member

the8472 commented May 30, 2023

The Type::method syntax is somewhat poorly supported. Many things don't work because auto-deref and coercions don't happen when you use that syntax.
Adding trait impls to make up for the lack of auto derefs seems like a good solution for that deficiency.

Imo it's worth checking if this can be improved on the language side, e.g. by inserting an anonymous shim function. I don't know if that would be acceptable since it would result in a different Fn type in the iterator adapter.

@Amanieu
Copy link
Member

Amanieu commented Jul 25, 2023

We discussed this in the libs-api meeting today. We're happy to add this, but it might be worth doing a crater run to check for type inference failures.

@Amanieu Amanieu closed this as completed Jul 25, 2023
@Amanieu Amanieu added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Jul 25, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 28, 2023
…-vec, r=dtolnay

Implement `From<{&,&mut} [T; N]>` for `Vec<T>` where `T: Clone`

Currently, if `T` implements `Clone`, we can create a `Vec<T>` from an `&[T]` or an `&mut [T]`, can we also support creating a `Vec<T>` from an `&[T; N]` or an `&mut [T; N]`? Also, do I need to add `#[inline]` to the implementation?

ACP: rust-lang/libs-team#220. [Accepted]

Closes rust-lang#100880.
RalfJung pushed a commit to RalfJung/miri that referenced this issue Sep 30, 2023
…tolnay

Implement `From<{&,&mut} [T; N]>` for `Vec<T>` where `T: Clone`

Currently, if `T` implements `Clone`, we can create a `Vec<T>` from an `&[T]` or an `&mut [T]`, can we also support creating a `Vec<T>` from an `&[T; N]` or an `&mut [T; N]`? Also, do I need to add `#[inline]` to the implementation?

ACP: rust-lang/libs-team#220. [Accepted]

Closes #100880.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Apr 7, 2024
…tolnay

Implement `From<{&,&mut} [T; N]>` for `Vec<T>` where `T: Clone`

Currently, if `T` implements `Clone`, we can create a `Vec<T>` from an `&[T]` or an `&mut [T]`, can we also support creating a `Vec<T>` from an `&[T; N]` or an `&mut [T; N]`? Also, do I need to add `#[inline]` to the implementation?

ACP: rust-lang/libs-team#220. [Accepted]

Closes #100880.
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
…tolnay

Implement `From<{&,&mut} [T; N]>` for `Vec<T>` where `T: Clone`

Currently, if `T` implements `Clone`, we can create a `Vec<T>` from an `&[T]` or an `&mut [T]`, can we also support creating a `Vec<T>` from an `&[T; N]` or an `&mut [T; N]`? Also, do I need to add `#[inline]` to the implementation?

ACP: rust-lang/libs-team#220. [Accepted]

Closes #100880.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants