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

[Feature Request] implement From<array literal> for Vec #67963

Closed
jyn514 opened this issue Jan 7, 2020 · 1 comment · Fixed by #68692
Closed

[Feature Request] implement From<array literal> for Vec #67963

jyn514 opened this issue Jan 7, 2020 · 1 comment · Fixed by #68692
Labels
A-collections Area: `std::collection` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Jan 7, 2020

I would like the following code to compile:

fn f<S: Into<Vec<u8>> + Clone>(subscripts: &[S]) -> Vec<Vec<u8>> {
    subscripts.iter().cloned().map(|s| s.into()).collect()
}

fn main() {
    f(&[&b"hi"[..]]);  // this works
    f(&[b"hi"]); // error: `From<&[u8; 2]>` is not satisfied
}

The first call in main is hard to get right, since leaving out any part of the syntax gives confusing errors (without [..], gives From<&&[u8; 2]> is not satisfied, without & gives doesn't have a size known at compile-time).

The second call seems to be the same idea, but doesn't compile since From isn't implemented for array literals.

This can be implemented without trouble on nightly Rust except for the orphan rule: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=21632f9784745afa98663d5e6e471cbb

If someone is willing to mentor I'm happy to turn that playground into a PR :)

@jyn514 jyn514 changed the title [Feature Request] impl<N: const usize, T> From<[T; N]> for Vec<T> where [T; N]: LengthAtMost32, [Feature Request] impl<T, N: const usize> From<[T; N]> for Vec<T> where [T; N]: LengthAtMost32, Jan 7, 2020
@jyn514 jyn514 changed the title [Feature Request] impl<T, N: const usize> From<[T; N]> for Vec<T> where [T; N]: LengthAtMost32, [Feature Request] implement From<array literal> for Vec Jan 7, 2020
@jonas-schievink jonas-schievink added A-collections Area: `std::collection` C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 7, 2020
@bors bors closed this as completed in c51fcb5 Mar 29, 2020
@pickfire
Copy link
Contributor

pickfire commented Apr 8, 2020

Should we also get one for impl<T, const N: usize> From<[T; N]> for Box<[T]>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` C-feature-request Category: A feature request, i.e: not implemented / a PR. 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.

3 participants