@@ -191,9 +191,7 @@ use core::error::{self, Error};
191191use core:: fmt;
192192use core:: future:: Future ;
193193use core:: hash:: { Hash , Hasher } ;
194- #[ cfg( not( bootstrap) ) ]
195- use core:: marker:: PointerLike ;
196- use core:: marker:: { Tuple , Unsize } ;
194+ use core:: marker:: { PointerLike , Tuple , Unsize } ;
197195use core:: mem:: { self , SizedTypeProperties } ;
198196use core:: ops:: {
199197 AsyncFn , AsyncFnMut , AsyncFnOnce , CoerceUnsized , Coroutine , CoroutineState , Deref , DerefMut ,
@@ -227,14 +225,35 @@ pub use thin::ThinBox;
227225#[ fundamental]
228226#[ stable( feature = "rust1" , since = "1.0.0" ) ]
229227#[ rustc_insignificant_dtor]
230- #[ cfg_attr ( not ( bootstrap ) , doc( search_unbox) ) ]
228+ #[ doc( search_unbox) ]
231229// The declaration of the `Box` struct must be kept in sync with the
232230// compiler or ICEs will happen.
233231pub struct Box <
234232 T : ?Sized ,
235233 #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
236234> ( Unique < T > , A ) ;
237235
236+ /// Constructs a `Box<T>` by calling the `exchange_malloc` lang item and moving the argument into
237+ /// the newly allocated memory. This is an intrinsic to avoid unnecessary copies.
238+ ///
239+ /// This is the surface syntax for `box <expr>` expressions.
240+ #[ cfg( not( bootstrap) ) ]
241+ #[ rustc_intrinsic]
242+ #[ rustc_intrinsic_must_be_overridden]
243+ #[ unstable( feature = "liballoc_internals" , issue = "none" ) ]
244+ pub fn box_new < T > ( _x : T ) -> Box < T > {
245+ unreachable ! ( )
246+ }
247+
248+ /// Transition function for the next bootstrap bump.
249+ #[ cfg( bootstrap) ]
250+ #[ unstable( feature = "liballoc_internals" , issue = "none" ) ]
251+ #[ inline( always) ]
252+ pub fn box_new < T > ( x : T ) -> Box < T > {
253+ #[ rustc_box]
254+ Box :: new ( x)
255+ }
256+
238257impl < T > Box < T > {
239258 /// Allocates memory on the heap and then places `x` into it.
240259 ///
@@ -252,8 +271,7 @@ impl<T> Box<T> {
252271 #[ rustc_diagnostic_item = "box_new" ]
253272 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
254273 pub fn new ( x : T ) -> Self {
255- #[ rustc_box]
256- Box :: new ( x)
274+ return box_new ( x) ;
257275 }
258276
259277 /// Constructs a new box with uninitialized contents.
@@ -763,6 +781,26 @@ impl<T> Box<[T]> {
763781 } ;
764782 unsafe { Ok ( RawVec :: from_raw_parts_in ( ptr. as_ptr ( ) , len, Global ) . into_box ( len) ) }
765783 }
784+
785+ /// Converts the boxed slice into a boxed array.
786+ ///
787+ /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
788+ ///
789+ /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
790+ #[ unstable( feature = "slice_as_array" , issue = "133508" ) ]
791+ #[ inline]
792+ #[ must_use]
793+ pub fn into_array < const N : usize > ( self ) -> Option < Box < [ T ; N ] > > {
794+ if self . len ( ) == N {
795+ let ptr = Self :: into_raw ( self ) as * mut [ T ; N ] ;
796+
797+ // SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
798+ let me = unsafe { Box :: from_raw ( ptr) } ;
799+ Some ( me)
800+ } else {
801+ None
802+ }
803+ }
766804}
767805
768806impl < T , A : Allocator > Box < [ T ] , A > {
@@ -1027,6 +1065,8 @@ impl<T: ?Sized> Box<T> {
10271065 /// memory problems. For example, a double-free may occur if the
10281066 /// function is called twice on the same raw pointer.
10291067 ///
1068+ /// The raw pointer must point to a block of memory allocated by the global allocator.
1069+ ///
10301070 /// The safety conditions are described in the [memory layout] section.
10311071 ///
10321072 /// # Examples
@@ -1130,6 +1170,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11301170 /// memory problems. For example, a double-free may occur if the
11311171 /// function is called twice on the same raw pointer.
11321172 ///
1173+ /// The raw pointer must point to a block of memory allocated by `alloc`
11331174 ///
11341175 /// # Examples
11351176 ///
@@ -1502,7 +1543,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15021543 /// [`as_ptr`]: Self::as_ptr
15031544 #[ unstable( feature = "box_as_ptr" , issue = "129090" ) ]
15041545 #[ rustc_never_returns_null_ptr]
1505- #[ cfg_attr ( not ( bootstrap ) , rustc_as_ptr) ]
1546+ #[ rustc_as_ptr]
15061547 #[ inline]
15071548 pub fn as_mut_ptr ( b : & mut Self ) -> * mut T {
15081549 // This is a primitive deref, not going through `DerefMut`, and therefore not materializing
@@ -1551,7 +1592,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15511592 /// [`as_ptr`]: Self::as_ptr
15521593 #[ unstable( feature = "box_as_ptr" , issue = "129090" ) ]
15531594 #[ rustc_never_returns_null_ptr]
1554- #[ cfg_attr ( not ( bootstrap ) , rustc_as_ptr) ]
1595+ #[ rustc_as_ptr]
15551596 #[ inline]
15561597 pub fn as_ptr ( b : & Self ) -> * const T {
15571598 // This is a primitive deref, not going through `DerefMut`, and therefore not materializing
@@ -1987,7 +2028,7 @@ impl<Args: Tuple, F: Fn<Args> + ?Sized, A: Allocator> Fn<Args> for Box<F, A> {
19872028 }
19882029}
19892030
1990- #[ unstable ( feature = "async_fn_traits " , issue = "none " ) ]
2031+ #[ stable ( feature = "async_closure " , since = "1.85.0 " ) ]
19912032impl < Args : Tuple , F : AsyncFnOnce < Args > + ?Sized , A : Allocator > AsyncFnOnce < Args > for Box < F , A > {
19922033 type Output = F :: Output ;
19932034 type CallOnceFuture = F :: CallOnceFuture ;
@@ -1997,7 +2038,7 @@ impl<Args: Tuple, F: AsyncFnOnce<Args> + ?Sized, A: Allocator> AsyncFnOnce<Args>
19972038 }
19982039}
19992040
2000- #[ unstable ( feature = "async_fn_traits " , issue = "none " ) ]
2041+ #[ stable ( feature = "async_closure " , since = "1.85.0 " ) ]
20012042impl < Args : Tuple , F : AsyncFnMut < Args > + ?Sized , A : Allocator > AsyncFnMut < Args > for Box < F , A > {
20022043 type CallRefFuture < ' a >
20032044 = F :: CallRefFuture < ' a >
@@ -2009,7 +2050,7 @@ impl<Args: Tuple, F: AsyncFnMut<Args> + ?Sized, A: Allocator> AsyncFnMut<Args> f
20092050 }
20102051}
20112052
2012- #[ unstable ( feature = "async_fn_traits " , issue = "none " ) ]
2053+ #[ stable ( feature = "async_closure " , since = "1.85.0 " ) ]
20132054impl < Args : Tuple , F : AsyncFn < Args > + ?Sized , A : Allocator > AsyncFn < Args > for Box < F , A > {
20142055 extern "rust-call" fn async_call ( & self , args : Args ) -> Self :: CallRefFuture < ' _ > {
20152056 F :: async_call ( self , args)
@@ -2134,6 +2175,5 @@ impl<E: Error> Error for Box<E> {
21342175 }
21352176}
21362177
2137- #[ cfg( not( bootstrap) ) ]
21382178#[ unstable( feature = "pointer_like_trait" , issue = "none" ) ]
21392179impl < T > PointerLike for Box < T > { }
0 commit comments