@@ -168,7 +168,7 @@ use core::task::{Context, Poll};
168168
169169#[ cfg( not( no_global_oom_handling) ) ]
170170use crate :: alloc:: { handle_alloc_error, WriteCloneIntoRaw } ;
171- use crate :: alloc:: { AllocError , Allocator , Global , Layout } ;
171+ use crate :: alloc:: { AllocError , Allocator , Global , Layout , PinSafeAllocator } ;
172172#[ cfg( not( no_global_oom_handling) ) ]
173173use crate :: borrow:: Cow ;
174174use crate :: raw_vec:: RawVec ;
@@ -1123,8 +1123,12 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11231123 // recognized as "releasing" the unique pointer to permit aliased raw accesses,
11241124 // so all raw pointer methods have to go through `Box::leak`. Turning *that* to a raw pointer
11251125 // behaves correctly.
1126- let alloc = unsafe { ptr:: read ( & b. 1 ) } ;
1127- ( Unique :: from ( Box :: leak ( b) ) , alloc)
1126+ let manually_drop = mem:: ManuallyDrop :: new ( b) ;
1127+ // SAFETY: unique ownership of the memory block moves into `ptr`
1128+ let ptr = unsafe { & mut * manually_drop. 0 . as_ptr ( ) } ;
1129+ // SAFETY: moving the allocator will not invalidate `ptr`
1130+ let alloc = unsafe { ptr:: read ( & manually_drop. 1 ) } ;
1131+ ( Unique :: from ( ptr) , alloc)
11281132 }
11291133
11301134 /// Returns a reference to the underlying allocator.
@@ -1179,9 +1183,13 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
11791183 #[ inline]
11801184 pub const fn leak < ' a > ( b : Self ) -> & ' a mut T
11811185 where
1182- A : ' a ,
1186+ A : ~ const PinSafeAllocator ,
11831187 {
1184- unsafe { & mut * mem:: ManuallyDrop :: new ( b) . 0 . as_ptr ( ) }
1188+ let ( ptr, alloc) = Box :: into_unique ( b) ;
1189+ mem:: forget ( alloc) ;
1190+ // SAFETY: ptr will remain valid for any lifetime since `alloc` is never
1191+ // dropped
1192+ unsafe { & mut * ptr. as_ptr ( ) }
11851193 }
11861194
11871195 /// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then
@@ -1218,7 +1226,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12181226 #[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
12191227 pub const fn into_pin ( boxed : Self ) -> Pin < Self >
12201228 where
1221- A : ' static ,
1229+ A : ~ const PinSafeAllocator ,
12221230 {
12231231 // It's not possible to move or replace the insides of a `Pin<Box<T>>`
12241232 // when `T: !Unpin`, so it's safe to pin it directly without any
@@ -1454,9 +1462,9 @@ impl<T> From<T> for Box<T> {
14541462
14551463#[ stable( feature = "pin" , since = "1.33.0" ) ]
14561464#[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
1457- impl < T : ?Sized , A : Allocator > const From < Box < T , A > > for Pin < Box < T , A > >
1465+ impl < T : ?Sized , A > const From < Box < T , A > > for Pin < Box < T , A > >
14581466where
1459- A : ' static ,
1467+ A : ~ const PinSafeAllocator ,
14601468{
14611469 /// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then
14621470 /// `*boxed` will be pinned in memory and unable to be moved.
@@ -2033,13 +2041,10 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
20332041 */
20342042#[ stable( feature = "pin" , since = "1.33.0" ) ]
20352043#[ rustc_const_unstable( feature = "const_box" , issue = "92521" ) ]
2036- impl < T : ?Sized , A : Allocator > const Unpin for Box < T , A > where A : ' static { }
2044+ impl < T : ?Sized , A : Allocator > const Unpin for Box < T , A > { }
20372045
20382046#[ unstable( feature = "generator_trait" , issue = "43122" ) ]
2039- impl < G : ?Sized + Generator < R > + Unpin , R , A : Allocator > Generator < R > for Box < G , A >
2040- where
2041- A : ' static ,
2042- {
2047+ impl < G : ?Sized + Generator < R > + Unpin , R , A : Allocator > Generator < R > for Box < G , A > {
20432048 type Yield = G :: Yield ;
20442049 type Return = G :: Return ;
20452050
@@ -2049,10 +2054,7 @@ where
20492054}
20502055
20512056#[ unstable( feature = "generator_trait" , issue = "43122" ) ]
2052- impl < G : ?Sized + Generator < R > , R , A : Allocator > Generator < R > for Pin < Box < G , A > >
2053- where
2054- A : ' static ,
2055- {
2057+ impl < G : ?Sized + Generator < R > , R , A : Allocator > Generator < R > for Pin < Box < G , A > > {
20562058 type Yield = G :: Yield ;
20572059 type Return = G :: Return ;
20582060
@@ -2062,10 +2064,7 @@ where
20622064}
20632065
20642066#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
2065- impl < F : ?Sized + Future + Unpin , A : Allocator > Future for Box < F , A >
2066- where
2067- A : ' static ,
2068- {
2067+ impl < F : ?Sized + Future + Unpin , A : Allocator > Future for Box < F , A > {
20692068 type Output = F :: Output ;
20702069
20712070 fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
0 commit comments