Skip to content

Commit a8ed9aa

Browse files
committed
impl From<[T; N]> for Box<[T]>
Based on #68692
1 parent 3a7dfda commit a8ed9aa

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

src/liballoc/boxed.rs

+19
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,25 @@ impl From<Box<str>> for Box<[u8]> {
865865
}
866866
}
867867

868+
#[stable(feature = "box_from_array", since = "1.45.0")]
869+
impl<T, const N: usize> From<[T; N]> for Box<[T]>
870+
where
871+
[T; N]: LengthAtMost32,
872+
{
873+
/// Converts a `[T; N]` into a `Box<[T]>`
874+
///
875+
/// This conversion moves the array to newly heap-allocated memory.
876+
///
877+
/// # Examples
878+
/// ```rust
879+
/// let boxed: Box<[u8]> = Box::from([4, 2]);
880+
/// println!("{:?}", boxed);
881+
/// ```
882+
fn from(array: [T; N]) -> Box<[T]> {
883+
box array
884+
}
885+
}
886+
868887
#[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
869888
impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]>
870889
where

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

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub fn yes_array_into_vec<T>() -> Vec<T> {
1818
[].into()
1919
}
2020

21+
pub fn yes_array_into_box<T>() -> Box<[T]> {
22+
[].into()
23+
}
24+
2125
use std::collections::VecDeque;
2226

2327
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

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub fn no_box() {
1212
let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
1313
//~^ ERROR the trait bound `std::boxed::Box<[i32; 33]>: std::convert::From<std::boxed::Box<[i32]>>` is not satisfied
1414
//~^^ ERROR the trait bound `std::boxed::Box<[i32; 33]>: std::convert::TryFrom<std::boxed::Box<[i32]>>` is not satisfied
15+
let boxed_slice = <Box<[i32]>>::from([0; 33]);
16+
//~^ 15:42: 15:49: arrays only have std trait implementations for lengths 0..=32 [E0277]
1517
}
1618

1719
pub fn no_rc() {

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

+19-6
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,23 @@ LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
1818
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<&str>>
1919
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::borrow::Cow<'a, str>>>
2020
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::string::String>>
21-
and 21 others
21+
and 22 others
2222
= note: required because of the requirements on the impl of `std::convert::Into<std::boxed::Box<[i32; 33]>>` for `std::boxed::Box<[i32]>`
2323
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::boxed::Box<[i32]>>` for `std::boxed::Box<[i32; 33]>`
2424

25+
error[E0277]: arrays only have std trait implementations for lengths 0..=32
26+
--> $DIR/alloc-types-no-impls-length-33.rs:15:42
27+
|
28+
LL | let boxed_slice = <Box<[i32]>>::from([0; 33]);
29+
| ^^^^^^^
30+
| |
31+
| expected an implementor of trait `std::convert::From<[{integer}; 33]>`
32+
| help: consider borrowing here: `&[0; 33]`
33+
|
34+
= note: the trait bound `[i32; 33]: std::convert::From<[{integer}; 33]>` is not satisfied
35+
= note: required because of the requirements on the impl of `std::convert::From<[i32; 33]>` for `std::boxed::Box<[i32]>`
36+
= note: required by `std::convert::From::from`
37+
2538
error[E0277]: the trait bound `std::boxed::Box<[i32; 33]>: std::convert::TryFrom<std::boxed::Box<[i32]>>` is not satisfied
2639
--> $DIR/alloc-types-no-impls-length-33.rs:12:23
2740
|
@@ -32,7 +45,7 @@ LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
3245
<std::boxed::Box<[T; N]> as std::convert::TryFrom<std::boxed::Box<[T]>>>
3346

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

4962
error[E0277]: the trait bound `std::rc::Rc<[i32; 33]>: std::convert::TryFrom<std::rc::Rc<[i32]>>` is not satisfied
50-
--> $DIR/alloc-types-no-impls-length-33.rs:19:23
63+
--> $DIR/alloc-types-no-impls-length-33.rs:21:23
5164
|
5265
LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice);
5366
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom<std::rc::Rc<[i32]>>` is not implemented for `std::rc::Rc<[i32; 33]>`
@@ -56,7 +69,7 @@ LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice);
5669
<std::rc::Rc<[T; N]> as std::convert::TryFrom<std::rc::Rc<[T]>>>
5770

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

7386
error[E0277]: the trait bound `std::sync::Arc<[i32; 33]>: std::convert::TryFrom<std::sync::Arc<[i32]>>` is not satisfied
74-
--> $DIR/alloc-types-no-impls-length-33.rs:26:23
87+
--> $DIR/alloc-types-no-impls-length-33.rs:28:23
7588
|
7689
LL | let boxed_array = <Arc<[i32; 33]>>::try_from(boxed_slice);
7790
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom<std::sync::Arc<[i32]>>` is not implemented for `std::sync::Arc<[i32; 33]>`
7891
|
7992
= help: the following implementations were found:
8093
<std::sync::Arc<[T; N]> as std::convert::TryFrom<std::sync::Arc<[T]>>>
8194

82-
error: aborting due to 7 previous errors
95+
error: aborting due to 8 previous errors
8396

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

0 commit comments

Comments
 (0)