Skip to content

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

Merged
bors merged 1 commit into
rust-lang:masterfrom
shepmaster:vec-from-array-ref
Mar 28, 2022
Merged

impl From<&[T; N]> and From<&mut [T; N]> for Vec<T>#95098
bors merged 1 commit into
rust-lang:masterfrom
shepmaster:vec-from-array-ref

Conversation

@shepmaster

Copy link
Copy Markdown
Member

I really wanted to write:

fnexample(a:implInto<Vec<u8>>){}fnmain(){example(b"raw");}

@shepmastershepmaster 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
Copy Markdown
Contributor

r? @dtolnay

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

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 19, 2022
@dtolnaydtolnay added T-libs-api [DEPRECATED; DO NOT USE] needs-fcp This change is insta-stable, or significant enough to need a team 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
Copy Markdown
Member

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

impl<T:Clone,constN:usize>From<&[T;N]>forVec<T>{}impl<T:Clone,constN:usize>From<&mut[T;N]>forVec<T>{}

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

impl<T:Clone>From<&[T]>forVec<T>{}// since 1.0.0impl<T:Clone>From<&mut[T]>forVec<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

rfcbot commented Mar 19, 2022

Copy link
Copy Markdown

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.

@rfcbotrfcbot 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
Copy Markdown

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

@rfcbotrfcbot 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
Copy Markdown
Member

@bors r+

@bors

bors commented Mar 28, 2022

Copy link
Copy Markdown
Collaborator

📌 Commit 5dd7027 has been approved by dtolnay

@borsbors 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:masterMar 28, 2022
@rustbotrustbot 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
@rfcbotrfcbot 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
@apirainoapiraino 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#95098rust-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
Copy Markdown
MemberAuthor

See #96074 and #96489

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.needs-fcpThis change is insta-stable, or significant enough to need a team FCP to proceed.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libs-api[DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@shepmaster@rust-highfive@dtolnay@rfcbot@bors@apiraino@rustbot