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

impl From<&[T; N]> and From<&mut [T; N]> for Vec<T> #95098

Merged
merged 1 commit into from
Mar 28, 2022

Conversation

shepmaster
Copy link
Member

I really wanted to write:

fn example(a: impl Into<Vec<u8>>) {}

fn main() {
    example(b"raw");
}

@shepmaster shepmaster added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Mar 19, 2022
@rust-highfive
Copy link
Collaborator

r? @dtolnay

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 19, 2022
@dtolnay dtolnay added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 19, 2022
@dtolnay
Copy link
Member

dtolnay commented Mar 19, 2022

@rust-lang/libs-api:
@rfcbot fcp merge

impl<T: Clone, const N: usize> From<&[T; N]> for Vec<T> {}
impl<T: Clone, const N: usize> From<&mut [T; N]> for Vec<T> {}

These are array versions of the already-stable conversions from slice to vec.

impl<T: Clone> From<&[T]> for Vec<T> {}  // since 1.0.0
impl<T: Clone> From<&mut [T]> for Vec<T> {}  // since 1.19.0

Array support is useful for methods that use Into<Vec<u8>>, such as https://doc.rust-lang.org/std/ffi/struct.CString.html#method.new.

error[E0277]: the trait bound `Vec<u8>: From<&[u8; 3]>` is not satisfied
   --> src/main.rs:2:36
    |
2   |     let _ = std::ffi::CString::new(b"+\xff+");
    |             ---------------------- ^^^^^^^^^ the trait `From<&[u8; 3]>` is not implemented for `Vec<u8>`
    |             |
    |             required by a bound introduced by this call
    |
    = help: the following implementations were found:
              <Vec<u8> as From<&str>>
              <Vec<u8> as From<CString>>
              <Vec<u8> as From<String>>
              <Vec<T, A> as From<Box<[T], A>>>
            and 6 others
    = note: required because of the requirements on the impl of `Into<Vec<u8>>` for `&[u8; 3]`
note: required by a bound in `CString::new`

@rfcbot
Copy link

rfcbot commented Mar 19, 2022

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

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!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Mar 19, 2022
@rfcbot
Copy link

rfcbot commented Mar 21, 2022

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Mar 21, 2022
@dtolnay
Copy link
Member

dtolnay commented Mar 28, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Mar 28, 2022

📌 Commit 5dd7027 has been approved by dtolnay

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 28, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Mar 28, 2022
…olnay

impl From<&[T; N]> and From<&mut [T; N]> for Vec<T>

I really wanted to write:

```rust
fn example(a: impl Into<Vec<u8>>) {}

fn main() {
    example(b"raw");
}
```
This was referenced Mar 28, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 28, 2022
Rollup of 4 pull requests

Successful merges:

 - rust-lang#88375 (Clarify that ManuallyDrop<T> has same layout as T)
 - rust-lang#93755 (Allow comparing `Vec`s with different allocators using `==`)
 - rust-lang#95016 (Docs: make Vec::from_raw_parts documentation less strict)
 - rust-lang#95098 (impl From<&[T; N]> and From<&mut [T; N]> for Vec<T>)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8bfc03f into rust-lang:master Mar 28, 2022
@rustbot rustbot added this to the 1.61.0 milestone Mar 28, 2022
@shepmaster shepmaster deleted the vec-from-array-ref branch March 28, 2022 11:08
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Mar 31, 2022
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Apr 21, 2022
estebank added a commit to estebank/rust that referenced this pull request Apr 26, 2022
…e without reverting rust-lang#95098

 rust-lang#95098 introduces new `From` impls for `Vec`, which can break existing
 code that relies on inference when calling `.as_ref()`. This change
 explicitly carves out a bias in the inference machinery to keep
 existing code compiling, while still maintaining the new `From` impls.

 Reported in rust-lang#96074.
@shepmaster
Copy link
Member Author

See #96074 and #96489

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants