Rollup merge of #71095 - pickfire:box-from-array, r=dtolnay

impl From<[T; N]> for Box<[T]>

Based on https://github.com/rust-lang/rust/pull/68692
This commit is contained in:
Ralf Jung 2020-05-29 21:58:19 +02:00 committed by GitHub
commit b387a11636
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 6 deletions

View File

@ -865,6 +865,25 @@ impl From<Box<str>> for Box<[u8]> {
} }
} }
#[stable(feature = "box_from_array", since = "1.45.0")]
impl<T, const N: usize> From<[T; N]> for Box<[T]>
where
[T; N]: LengthAtMost32,
{
/// Converts a `[T; N]` into a `Box<[T]>`
///
/// This conversion moves the array to newly heap-allocated memory.
///
/// # Examples
/// ```rust
/// let boxed: Box<[u8]> = Box::from([4, 2]);
/// println!("{:?}", boxed);
/// ```
fn from(array: [T; N]) -> Box<[T]> {
box array
}
}
#[stable(feature = "boxed_slice_try_from", since = "1.43.0")] #[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]>
where where

View File

@ -18,6 +18,10 @@ pub fn yes_array_into_vec<T>() -> Vec<T> {
[].into() [].into()
} }
pub fn yes_array_into_box<T>() -> Box<[T]> {
[].into()
}
use std::collections::VecDeque; use std::collections::VecDeque;
pub fn yes_vecdeque_partial_eq_array<A, B>() -> impl PartialEq<[B; 32]> pub fn yes_vecdeque_partial_eq_array<A, B>() -> impl PartialEq<[B; 32]>

View File

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

View File

@ -18,10 +18,23 @@ LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<&str>> <std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<&str>>
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::borrow::Cow<'a, str>>> <std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::borrow::Cow<'a, str>>>
<std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::string::String>> <std::boxed::Box<(dyn std::error::Error + 'static)> as std::convert::From<std::string::String>>
and 21 others and 22 others
= note: required because of the requirements on the impl of `std::convert::Into<std::boxed::Box<[i32; 33]>>` for `std::boxed::Box<[i32]>` = note: required because of the requirements on the impl of `std::convert::Into<std::boxed::Box<[i32; 33]>>` for `std::boxed::Box<[i32]>`
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::boxed::Box<[i32]>>` for `std::boxed::Box<[i32; 33]>` = 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]: arrays only have std trait implementations for lengths 0..=32
--> $DIR/alloc-types-no-impls-length-33.rs:15:42
|
LL | let boxed_slice = <Box<[i32]>>::from([0; 33]);
| ^^^^^^^
| |
| expected an implementor of trait `std::convert::From<[{integer}; 33]>`
| help: consider borrowing here: `&[0; 33]`
|
= note: the trait bound `[i32; 33]: std::convert::From<[{integer}; 33]>` is not satisfied
= note: required because of the requirements on the impl of `std::convert::From<[i32; 33]>` for `std::boxed::Box<[i32]>`
= note: required by `std::convert::From::from`
error[E0277]: the trait bound `std::boxed::Box<[i32; 33]>: std::convert::TryFrom<std::boxed::Box<[i32]>>` is not satisfied 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:12:23 --> $DIR/alloc-types-no-impls-length-33.rs:12:23
| |
@ -32,7 +45,7 @@ LL | let boxed_array = <Box<[i32; 33]>>::try_from(boxed_slice);
<std::boxed::Box<[T; N]> as std::convert::TryFrom<std::boxed::Box<[T]>>> <std::boxed::Box<[T; N]> 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 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:19:23 --> $DIR/alloc-types-no-impls-length-33.rs:21:23
| |
LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice); 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]>` | ^^^^^^^^^^^^^^^^^^^^^^^^^ 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);
= note: required because of the requirements on the impl of `std::convert::TryFrom<std::rc::Rc<[i32]>>` for `std::rc::Rc<[i32; 33]>` = 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 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:19:23 --> $DIR/alloc-types-no-impls-length-33.rs:21:23
| |
LL | let boxed_array = <Rc<[i32; 33]>>::try_from(boxed_slice); 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]>` | ^^^^^^^^^^^^^^^^^^^^^^^^^ 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);
<std::rc::Rc<[T; N]> as std::convert::TryFrom<std::rc::Rc<[T]>>> <std::rc::Rc<[T; N]> 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 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:26:23 --> $DIR/alloc-types-no-impls-length-33.rs:28:23
| |
LL | let boxed_array = <Arc<[i32; 33]>>::try_from(boxed_slice); 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]>` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::sync::Arc<[i32]>>` is not implemented for `std::sync::Arc<[i32; 33]>`
@ -71,7 +84,7 @@ 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]>` = 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 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:26:23 --> $DIR/alloc-types-no-impls-length-33.rs:28:23
| |
LL | let boxed_array = <Arc<[i32; 33]>>::try_from(boxed_slice); 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]>` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::TryFrom<std::sync::Arc<[i32]>>` is not implemented for `std::sync::Arc<[i32; 33]>`
@ -79,6 +92,6 @@ LL | let boxed_array = <Arc<[i32; 33]>>::try_from(boxed_slice);
= help: the following implementations were found: = help: the following implementations were found:
<std::sync::Arc<[T; N]> as std::convert::TryFrom<std::sync::Arc<[T]>>> <std::sync::Arc<[T; N]> as std::convert::TryFrom<std::sync::Arc<[T]>>>
error: aborting due to 7 previous errors error: aborting due to 8 previous errors
For more information about this error, try `rustc --explain E0277`. For more information about this error, try `rustc --explain E0277`.