@@ -151,7 +151,6 @@ use core::async_iter::AsyncIterator;
151
151
use core:: borrow;
152
152
use core:: cmp:: Ordering ;
153
153
use core:: convert:: { From , TryFrom } ;
154
- #[ cfg( not( bootstrap) ) ]
155
154
use core:: error:: Error ;
156
155
use core:: fmt;
157
156
use core:: future:: Future ;
@@ -176,7 +175,6 @@ use crate::borrow::Cow;
176
175
use crate :: raw_vec:: RawVec ;
177
176
#[ cfg( not( no_global_oom_handling) ) ]
178
177
use crate :: str:: from_boxed_utf8_unchecked;
179
- #[ cfg( not( bootstrap) ) ]
180
178
#[ cfg( not( no_global_oom_handling) ) ]
181
179
use crate :: string:: String ;
182
180
#[ cfg( not( no_global_oom_handling) ) ]
@@ -1622,6 +1620,22 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
1622
1620
}
1623
1621
}
1624
1622
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
+
1625
1639
#[ stable( feature = "boxed_slice_try_from" , since = "1.43.0" ) ]
1626
1640
impl < T , const N : usize > TryFrom < Box < [ T ] > > for Box < [ T ; N ] > {
1627
1641
type Error = Box < [ T ] > ;
@@ -1637,13 +1651,46 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
1637
1651
/// `boxed_slice.len()` does not equal `N`.
1638
1652
fn try_from ( boxed_slice : Box < [ T ] > ) -> Result < Self , Self :: Error > {
1639
1653
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) } )
1641
1655
} else {
1642
1656
Err ( boxed_slice)
1643
1657
}
1644
1658
}
1645
1659
}
1646
1660
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
+
1647
1694
impl < A : Allocator > Box < dyn Any , A > {
1648
1695
/// Attempt to downcast the box to a concrete type.
1649
1696
///
@@ -2090,7 +2137,6 @@ impl<S: ?Sized + AsyncIterator + Unpin> AsyncIterator for Box<S> {
2090
2137
}
2091
2138
}
2092
2139
2093
- #[ cfg( not( bootstrap) ) ]
2094
2140
impl dyn Error {
2095
2141
#[ inline]
2096
2142
#[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
@@ -2108,7 +2154,6 @@ impl dyn Error {
2108
2154
}
2109
2155
}
2110
2156
2111
- #[ cfg( not( bootstrap) ) ]
2112
2157
impl dyn Error + Send {
2113
2158
#[ inline]
2114
2159
#[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
@@ -2123,7 +2168,6 @@ impl dyn Error + Send {
2123
2168
}
2124
2169
}
2125
2170
2126
- #[ cfg( not( bootstrap) ) ]
2127
2171
impl dyn Error + Send + Sync {
2128
2172
#[ inline]
2129
2173
#[ stable( feature = "error_downcast" , since = "1.3.0" ) ]
@@ -2138,7 +2182,6 @@ impl dyn Error + Send + Sync {
2138
2182
}
2139
2183
}
2140
2184
2141
- #[ cfg( not( bootstrap) ) ]
2142
2185
#[ cfg( not( no_global_oom_handling) ) ]
2143
2186
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2144
2187
impl < ' 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> {
2172
2215
}
2173
2216
}
2174
2217
2175
- #[ cfg( not( bootstrap) ) ]
2176
2218
#[ cfg( not( no_global_oom_handling) ) ]
2177
2219
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2178
2220
impl < ' 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 +
2212
2254
}
2213
2255
}
2214
2256
2215
- #[ cfg( not( bootstrap) ) ]
2216
2257
#[ cfg( not( no_global_oom_handling) ) ]
2217
2258
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2218
2259
impl From < String > for Box < dyn Error + Send + Sync > {
@@ -2257,7 +2298,6 @@ impl From<String> for Box<dyn Error + Send + Sync> {
2257
2298
}
2258
2299
}
2259
2300
2260
- #[ cfg( not( bootstrap) ) ]
2261
2301
#[ cfg( not( no_global_oom_handling) ) ]
2262
2302
#[ stable( feature = "string_box_error" , since = "1.6.0" ) ]
2263
2303
impl From < String > for Box < dyn Error > {
@@ -2280,7 +2320,6 @@ impl From<String> for Box<dyn Error> {
2280
2320
}
2281
2321
}
2282
2322
2283
- #[ cfg( not( bootstrap) ) ]
2284
2323
#[ cfg( not( no_global_oom_handling) ) ]
2285
2324
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2286
2325
impl < ' 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> {
2305
2344
}
2306
2345
}
2307
2346
2308
- #[ cfg( not( bootstrap) ) ]
2309
2347
#[ cfg( not( no_global_oom_handling) ) ]
2310
2348
#[ stable( feature = "string_box_error" , since = "1.6.0" ) ]
2311
2349
impl From < & str > for Box < dyn Error > {
@@ -2328,7 +2366,6 @@ impl From<&str> for Box<dyn Error> {
2328
2366
}
2329
2367
}
2330
2368
2331
- #[ cfg( not( bootstrap) ) ]
2332
2369
#[ cfg( not( no_global_oom_handling) ) ]
2333
2370
#[ stable( feature = "cow_box_error" , since = "1.22.0" ) ]
2334
2371
impl < ' 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> {
2351
2388
}
2352
2389
}
2353
2390
2354
- #[ cfg( not( bootstrap) ) ]
2355
2391
#[ cfg( not( no_global_oom_handling) ) ]
2356
2392
#[ stable( feature = "cow_box_error" , since = "1.22.0" ) ]
2357
2393
impl < ' a > From < Cow < ' a , str > > for Box < dyn Error > {
@@ -2373,7 +2409,6 @@ impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
2373
2409
}
2374
2410
}
2375
2411
2376
- #[ cfg( not( bootstrap) ) ]
2377
2412
#[ stable( feature = "box_error" , since = "1.8.0" ) ]
2378
2413
impl < T : core:: error:: Error > core:: error:: Error for Box < T > {
2379
2414
#[ allow( deprecated, deprecated_in_future) ]
0 commit comments