@@ -151,7 +151,6 @@ use core::async_iter::AsyncIterator;
151151use core:: borrow;
152152use core:: cmp:: Ordering ;
153153use core:: convert:: { From , TryFrom } ;
154- #[ cfg( not( bootstrap) ) ]
155154use core:: error:: Error ;
156155use core:: fmt;
157156use core:: future:: Future ;
@@ -176,7 +175,6 @@ use crate::borrow::Cow;
176175use crate :: raw_vec:: RawVec ;
177176#[ cfg( not( no_global_oom_handling) ) ]
178177use crate :: str:: from_boxed_utf8_unchecked;
179- #[ cfg( not( bootstrap) ) ]
180178#[ cfg( not( no_global_oom_handling) ) ]
181179use crate :: string:: String ;
182180#[ cfg( not( no_global_oom_handling) ) ]
@@ -1622,6 +1620,22 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
16221620 }
16231621}
16241622
1623+ /// Casts a boxed slice to a boxed array.
1624+ ///
1625+ /// # Safety
1626+ ///
1627+ /// `boxed_slice.len()` must be exactly `N`.
1628+ unsafe fn boxed_slice_as_array_unchecked < T , A : Allocator , const N : usize > (
1629+ boxed_slice : Box < [ T ] , A > ,
1630+ ) -> Box < [ T ; N ] , A > {
1631+ debug_assert_eq ! ( boxed_slice. len( ) , N ) ;
1632+
1633+ let ( ptr, alloc) = Box :: into_raw_with_allocator ( boxed_slice) ;
1634+ // SAFETY: Pointer and allocator came from an existing box,
1635+ // and our safety condition requires that the length is exactly `N`
1636+ unsafe { Box :: from_raw_in ( ptr as * mut [ T ; N ] , alloc) }
1637+ }
1638+
16251639#[ stable( feature = "boxed_slice_try_from" , since = "1.43.0" ) ]
16261640impl < T , const N : usize > TryFrom < Box < [ T ] > > for Box < [ T ; N ] > {
16271641 type Error = Box < [ T ] > ;
@@ -1637,13 +1651,46 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
16371651 /// `boxed_slice.len()` does not equal `N`.
16381652 fn try_from ( boxed_slice : Box < [ T ] > ) -> Result < Self , Self :: Error > {
16391653 if boxed_slice. len ( ) == N {
1640- Ok ( unsafe { Box :: from_raw ( Box :: into_raw ( boxed_slice) as * mut [ T ; N ] ) } )
1654+ Ok ( unsafe { boxed_slice_as_array_unchecked ( boxed_slice) } )
16411655 } else {
16421656 Err ( boxed_slice)
16431657 }
16441658 }
16451659}
16461660
1661+ #[ cfg( not( no_global_oom_handling) ) ]
1662+ #[ stable( feature = "boxed_array_try_from_vec" , since = "CURRENT_RUSTC_VERSION" ) ]
1663+ impl < T , const N : usize > TryFrom < Vec < T > > for Box < [ T ; N ] > {
1664+ type Error = Vec < T > ;
1665+
1666+ /// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
1667+ ///
1668+ /// Like [`Vec::into_boxed_slice`], this is in-place if `vec.capacity() == N`,
1669+ /// but will require a reallocation otherwise.
1670+ ///
1671+ /// # Errors
1672+ ///
1673+ /// Returns the original `Vec<T>` in the `Err` variant if
1674+ /// `boxed_slice.len()` does not equal `N`.
1675+ ///
1676+ /// # Examples
1677+ ///
1678+ /// This can be used with [`vec!`] to create an array on the heap:
1679+ ///
1680+ /// ```
1681+ /// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap();
1682+ /// assert_eq!(state.len(), 100);
1683+ /// ```
1684+ fn try_from ( vec : Vec < T > ) -> Result < Self , Self :: Error > {
1685+ if vec. len ( ) == N {
1686+ let boxed_slice = vec. into_boxed_slice ( ) ;
1687+ Ok ( unsafe { boxed_slice_as_array_unchecked ( boxed_slice) } )
1688+ } else {
1689+ Err ( vec)
1690+ }
1691+ }
1692+ }
1693+
16471694impl < A : Allocator > Box < dyn Any , A > {
16481695 /// Attempt to downcast the box to a concrete type.
16491696 ///
@@ -2090,7 +2137,6 @@ impl<S: ?Sized + AsyncIterator + Unpin> AsyncIterator for Box<S> {
20902137 }
20912138}
20922139
2093- #[ cfg( not( bootstrap) ) ]
20942140impl dyn Error {
20952141 #[ inline]
20962142 #[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
@@ -2108,7 +2154,6 @@ impl dyn Error {
21082154 }
21092155}
21102156
2111- #[ cfg( not( bootstrap) ) ]
21122157impl dyn Error + Send {
21132158 #[ inline]
21142159 #[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
@@ -2123,7 +2168,6 @@ impl dyn Error + Send {
21232168 }
21242169}
21252170
2126- #[ cfg( not( bootstrap) ) ]
21272171impl dyn Error + Send + Sync {
21282172 #[ inline]
21292173 #[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
@@ -2138,7 +2182,6 @@ impl dyn Error + Send + Sync {
21382182 }
21392183}
21402184
2141- #[ cfg( not( bootstrap) ) ]
21422185#[ cfg( not( no_global_oom_handling) ) ]
21432186#[ stable( feature = "rust1" , since = "1.0.0" ) ]
21442187impl < ' a , E : Error + ' a > From < E > for Box < dyn Error + ' a > {
@@ -2172,7 +2215,6 @@ impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
21722215 }
21732216}
21742217
2175- #[ cfg( not( bootstrap) ) ]
21762218#[ cfg( not( no_global_oom_handling) ) ]
21772219#[ stable( feature = "rust1" , since = "1.0.0" ) ]
21782220impl < ' a , E : Error + Send + Sync + ' a > From < E > for Box < dyn Error + Send + Sync + ' a > {
@@ -2212,7 +2254,6 @@ impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync +
22122254 }
22132255}
22142256
2215- #[ cfg( not( bootstrap) ) ]
22162257#[ cfg( not( no_global_oom_handling) ) ]
22172258#[ stable( feature = "rust1" , since = "1.0.0" ) ]
22182259impl From < String > for Box < dyn Error + Send + Sync > {
@@ -2257,7 +2298,6 @@ impl From<String> for Box<dyn Error + Send + Sync> {
22572298 }
22582299}
22592300
2260- #[ cfg( not( bootstrap) ) ]
22612301#[ cfg( not( no_global_oom_handling) ) ]
22622302#[ stable( feature = "string_box_error" , since = "1.6.0" ) ]
22632303impl From < String > for Box < dyn Error > {
@@ -2280,7 +2320,6 @@ impl From<String> for Box<dyn Error> {
22802320 }
22812321}
22822322
2283- #[ cfg( not( bootstrap) ) ]
22842323#[ cfg( not( no_global_oom_handling) ) ]
22852324#[ stable( feature = "rust1" , since = "1.0.0" ) ]
22862325impl < ' a > From < & str > for Box < dyn Error + Send + Sync + ' a > {
@@ -2305,7 +2344,6 @@ impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
23052344 }
23062345}
23072346
2308- #[ cfg( not( bootstrap) ) ]
23092347#[ cfg( not( no_global_oom_handling) ) ]
23102348#[ stable( feature = "string_box_error" , since = "1.6.0" ) ]
23112349impl From < & str > for Box < dyn Error > {
@@ -2328,7 +2366,6 @@ impl From<&str> for Box<dyn Error> {
23282366 }
23292367}
23302368
2331- #[ cfg( not( bootstrap) ) ]
23322369#[ cfg( not( no_global_oom_handling) ) ]
23332370#[ stable( feature = "cow_box_error" , since = "1.22.0" ) ]
23342371impl < ' a , ' b > From < Cow < ' b , str > > for Box < dyn Error + Send + Sync + ' a > {
@@ -2351,7 +2388,6 @@ impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
23512388 }
23522389}
23532390
2354- #[ cfg( not( bootstrap) ) ]
23552391#[ cfg( not( no_global_oom_handling) ) ]
23562392#[ stable( feature = "cow_box_error" , since = "1.22.0" ) ]
23572393impl < ' a > From < Cow < ' a , str > > for Box < dyn Error > {
@@ -2373,7 +2409,6 @@ impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
23732409 }
23742410}
23752411
2376- #[ cfg( not( bootstrap) ) ]
23772412#[ stable( feature = "box_error" , since = "1.8.0" ) ]
23782413impl < T : core:: error:: Error > core:: error:: Error for Box < T > {
23792414 #[ allow( deprecated, deprecated_in_future) ]
0 commit comments