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]> for Vec<T> #68692

Merged
merged 9 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ignore-tidy-filelength
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what's the procedure on this and what this tidy check tries to achieve, but it feels like this file could easily be split. There are already multiple sections. It would be easy, for example, to move the iterator stuff out of there (the last section, starting here). It's kind of unfortunate that you have to do it now, since this is unrelated to your PR, but I'd prefer that over the ignore tidy annotation.

But just to get a second opinion: @BurntSushi @withoutboats What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of the tidy check is to avoid gigantic files with poor structure. I think doing it before this PR or after is fine, but someone should sign up for the work.

//! A contiguous growable array type with heap-allocated contents, written
//! `Vec<T>`.
//!
Expand Down Expand Up @@ -2397,6 +2398,21 @@ impl<T: Clone> From<&mut [T]> for Vec<T> {
}
}

#[stable(feature = "vec_from_array", since = "1.44.0")]
impl<T, const N: usize> From<[T; N]> for Vec<T>
where
[T; N]: LengthAtMost32,
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any docs for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need documentation? It looks pretty self-explanatory to me.

#[cfg(not(test))]
fn from(s: [T; N]) -> Vec<T> {
<[T]>::into_vec(box s)
}
#[cfg(test)]
Mark-Simulacrum marked this conversation as resolved.
Show resolved Hide resolved
fn from(s: [T; N]) -> Vec<T> {
crate::slice::into_vec(box s)
LukasKalbertodt marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[stable(feature = "vec_from_cow_slice", since = "1.14.0")]
impl<'a, T> From<Cow<'a, [T]>> for Vec<T>
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ where
Vec::<A>::new()
}

pub fn yes_array_into_vec<T>() -> Vec<T> {
[].into()
}

use std::collections::VecDeque;

pub fn yes_vecdeque_partial_eq_array<A, B>() -> impl PartialEq<[B; 32]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

use std::{convert::TryFrom, rc::Rc, sync::Arc};

pub fn no_vec() {
let v: Vec<_> = [0; 33].into();
//~^ ERROR arrays only have std trait implementations for lengths 0..=32
}

pub fn no_box() {
let boxed_slice = Box::new([0; 33]) as Box<[i32]>;
let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
error[E0277]: arrays only have std trait implementations for lengths 0..=32
--> $DIR/alloc-types-no-impls-length-33.rs:6:29
|
LL | let v: Vec<_> = [0; 33].into();
| ^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[{integer}; 33]`
|
= note: required because of the requirements on the impl of `std::convert::From<[{integer}; 33]>` for `std::vec::Vec<{integer}>`
= note: required because of the requirements on the impl of `std::convert::Into<std::vec::Vec<{integer}>>` for `[{integer}; 33]`

error[E0277]: the trait bound `std::boxed::Box<[i32; 33]>: std::convert::From<std::boxed::Box<[i32]>>` is not satisfied
--> $DIR/alloc-types-no-impls-length-33.rs:7:23
--> $DIR/alloc-types-no-impls-length-33.rs:12:23
|
LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::boxed::Box<[i32]>>` is not implemented for `std::boxed::Box<[i32; 33]>`
Expand All @@ -14,7 +23,7 @@ LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::boxed::Box<[i32]>>` for `std::boxed::Box<[i32; 33]>`

error[E0277]: the trait bound `std::boxed::Box<[i32; 33]>: std::convert::TryFrom<std::boxed::Box<[i32]>>` is not satisfied
--> $DIR/alloc-types-no-impls-length-33.rs:7:23
--> $DIR/alloc-types-no-impls-length-33.rs:12:23
|
LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom<std::boxed::Box<[i32]>>` is not implemented for `std::boxed::Box<[i32; 33]>`
Expand All @@ -23,7 +32,7 @@ LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
<std::boxed::Box<[T; _]> as std::convert::TryFrom<std::boxed::Box<[T]>>>

error[E0277]: the trait bound `std::rc::Rc<[i32; 33]>: std::convert::From<std::rc::Rc<[i32]>>` is not satisfied
--> $DIR/alloc-types-no-impls-length-33.rs:14:23
--> $DIR/alloc-types-no-impls-length-33.rs:19:23
|
LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::rc::Rc<[i32]>>` is not implemented for `std::rc::Rc<[i32; 33]>`
Expand All @@ -38,7 +47,7 @@ LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice);
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::rc::Rc<[i32]>>` for `std::rc::Rc<[i32; 33]>`

error[E0277]: the trait bound `std::rc::Rc<[i32; 33]>: std::convert::TryFrom<std::rc::Rc<[i32]>>` is not satisfied
--> $DIR/alloc-types-no-impls-length-33.rs:14:23
--> $DIR/alloc-types-no-impls-length-33.rs:19:23
|
LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom<std::rc::Rc<[i32]>>` is not implemented for `std::rc::Rc<[i32; 33]>`
Expand All @@ -47,7 +56,7 @@ LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice);
<std::rc::Rc<[T; _]> as std::convert::TryFrom<std::rc::Rc<[T]>>>

error[E0277]: the trait bound `std::sync::Arc<[i32; 33]>: std::convert::From<std::sync::Arc<[i32]>>` is not satisfied
--> $DIR/alloc-types-no-impls-length-33.rs:21:23
--> $DIR/alloc-types-no-impls-length-33.rs:26:23
|
LL | let boxed_array = <Arc<[i32; 33]>>::try_from(boxed_slice);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::sync::Arc<[i32]>>` is not implemented for `std::sync::Arc<[i32; 33]>`
Expand All @@ -62,14 +71,14 @@ LL | let boxed_array = <Arc<[i32; 33]>>::try_from(boxed_slice);
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::sync::Arc<[i32]>>` for `std::sync::Arc<[i32; 33]>`

error[E0277]: the trait bound `std::sync::Arc<[i32; 33]>: std::convert::TryFrom<std::sync::Arc<[i32]>>` is not satisfied
--> $DIR/alloc-types-no-impls-length-33.rs:21:23
--> $DIR/alloc-types-no-impls-length-33.rs:26:23
|
LL | let boxed_array = <Arc<[i32; 33]>>::try_from(boxed_slice);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom<std::sync::Arc<[i32]>>` is not implemented for `std::sync::Arc<[i32; 33]>`
|
= help: the following implementations were found:
<std::sync::Arc<[T; _]> as std::convert::TryFrom<std::sync::Arc<[T]>>>

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0277`.