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<&[T; N]> and From<&mut [T; N]> for Vec<T> where T: Clone? #100880

Closed
EFanZh opened this issue Aug 22, 2022 · 4 comments · Fixed by #111278
Closed

Implement From<&[T; N]> and From<&mut [T; N]> for Vec<T> where T: Clone? #100880

EFanZh opened this issue Aug 22, 2022 · 4 comments · Fixed by #111278
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@EFanZh
Copy link
Contributor

EFanZh commented Aug 22, 2022

Currently we have impl From<&[T]> for Vec<T> where T: Clone and impl From<&mut [T]> for Vec<T> where T: Clone, should From<&[T; N]> and From<&mut [T; N]> also be implemented for Vec<T> where T: Clone?

@vincenzopalazzo
Copy link
Member

@rustbot claim

@vincenzopalazzo
Copy link
Member

vincenzopalazzo commented Oct 22, 2022

Can you help me with runnable code to understand why the From<&mut [T; N]> [for Vec<T> where T: Clone will be useful and what can be a use case for it?

@rustbot label +T-libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 22, 2022
@vincenzopalazzo
Copy link
Member

Ah ok, maybe I understand what do you mean, currently we can do

use std::vec::Vec;

#[derive(Clone, Copy, Debug)]
struct Type;

fn main() {
    let mut array = [Type{}];
    let mut vect = vec![];
    vect.extend_from_slice(&array);
    println!("{:?}", vect);  
}

And your proposal is to support

use std::vec::Vec;

#[derive(Clone, Copy, Debug)]
struct Type;

fn main() {
    let mut array = [Type{}];
    let mut vect = Vec::from(&array);
    println!("{:?}", vect);
    
}

@EFanZh
Copy link
Contributor Author

EFanZh commented Oct 24, 2022

@vincenzopalazzo I don’t have a use case for From<&mut [T; N]>, adding it is just for matching existing From<&mut [T]>. But for From<&[T; N]>, I want to be able to write:

fn foo(value: Option<&[String; 2]>) -> Option<Vec<String>> {
    value.map(Vec::from)
}

instead of

fn foo(value: Option<&[String; 2]>) -> Option<Vec<String>> {
    value.map(<[_; 2]>::as_slice).map(Vec::from)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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