Skip to content

Commit c51fcb5

Browse files
authored
Rollup merge of #68692 - jyn514:vec-from-array, r=LukasKalbertodt
impl From<[T; N]> for Vec<T> Closes #67963
2 parents 8045865 + 3477e67 commit c51fcb5

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

src/liballoc/vec.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ignore-tidy-filelength
12
//! A contiguous growable array type with heap-allocated contents, written
23
//! `Vec<T>`.
34
//!
@@ -2398,6 +2399,21 @@ impl<T: Clone> From<&mut [T]> for Vec<T> {
23982399
}
23992400
}
24002401

2402+
#[stable(feature = "vec_from_array", since = "1.44.0")]
2403+
impl<T, const N: usize> From<[T; N]> for Vec<T>
2404+
where
2405+
[T; N]: LengthAtMost32,
2406+
{
2407+
#[cfg(not(test))]
2408+
fn from(s: [T; N]) -> Vec<T> {
2409+
<[T]>::into_vec(box s)
2410+
}
2411+
#[cfg(test)]
2412+
fn from(s: [T; N]) -> Vec<T> {
2413+
crate::slice::into_vec(box s)
2414+
}
2415+
}
2416+
24012417
#[stable(feature = "vec_from_cow_slice", since = "1.14.0")]
24022418
impl<'a, T> From<Cow<'a, [T]>> for Vec<T>
24032419
where

src/test/ui/const-generics/array-impls/alloc-traits-impls-length-32.rs

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ where
1414
Vec::<A>::new()
1515
}
1616

17+
pub fn yes_array_into_vec<T>() -> Vec<T> {
18+
[].into()
19+
}
20+
1721
use std::collections::VecDeque;
1822

1923
pub fn yes_vecdeque_partial_eq_array<A, B>() -> impl PartialEq<[B; 32]>

src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

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

5+
pub fn no_vec() {
6+
let v: Vec<_> = [0; 33].into();
7+
//~^ ERROR arrays only have std trait implementations for lengths 0..=32
8+
}
9+
510
pub fn no_box() {
611
let boxed_slice = Box::new([0; 33]) as Box<[i32]>;
712
let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);

src/test/ui/const-generics/array-impls/alloc-types-no-impls-length-33.stderr

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
error[E0277]: arrays only have std trait implementations for lengths 0..=32
2+
--> $DIR/alloc-types-no-impls-length-33.rs:6:29
3+
|
4+
LL | let v: Vec<_> = [0; 33].into();
5+
| ^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[{integer}; 33]`
6+
|
7+
= note: required because of the requirements on the impl of `std::convert::From<[{integer}; 33]>` for `std::vec::Vec<{integer}>`
8+
= note: required because of the requirements on the impl of `std::convert::Into<std::vec::Vec<{integer}>>` for `[{integer}; 33]`
9+
110
error[E0277]: the trait bound `std::boxed::Box<[i32; 33]>: std::convert::From<std::boxed::Box<[i32]>>` is not satisfied
2-
--> $DIR/alloc-types-no-impls-length-33.rs:7:23
11+
--> $DIR/alloc-types-no-impls-length-33.rs:12:23
312
|
413
LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
514
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::boxed::Box<[i32]>>` is not implemented for `std::boxed::Box<[i32; 33]>`
@@ -14,7 +23,7 @@ LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
1423
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::boxed::Box<[i32]>>` for `std::boxed::Box<[i32; 33]>`
1524

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

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

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

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

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

73-
error: aborting due to 6 previous errors
82+
error: aborting due to 7 previous errors
7483

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

0 commit comments

Comments
 (0)