@@ -45,13 +45,10 @@ unsafe extern "Rust" {
4545
4646/// The global memory allocator.
4747///
48- /// This type implements the [`Allocator `] trait by forwarding calls
48+ /// This type implements the [`Alloc `] trait by forwarding calls
4949/// to the allocator registered with the `#[global_allocator]` attribute
5050/// if there is one, or the `std` crate’s default.
51- ///
52- /// Note: while this type is unstable, the functionality it provides can be
53- /// accessed through the [free functions in `alloc`](self#functions).
54- #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
51+ #[ stable( feature = "allocator_api_mvp" , since = "CURRENT_RUSTC_VERSION" ) ]
5552#[ derive( Copy , Clone , Default , Debug ) ]
5653// the compiler needs to know when a Box uses the global allocator vs a custom one
5754#[ lang = "global_alloc_ty" ]
@@ -202,14 +199,14 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
202199impl Global {
203200 #[ inline]
204201 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
205- fn alloc_impl_runtime ( layout : Layout , zeroed : bool ) -> Result < NonNull < [ u8 ] > , AllocError > {
202+ fn alloc_impl_runtime ( layout : Layout , zeroed : bool ) -> Result < NonNull < u8 > , AllocError > {
206203 match layout. size ( ) {
207- 0 => Ok ( layout. dangling_ptr ( ) . cast_slice ( 0 ) ) ,
204+ 0 => Ok ( layout. dangling_ptr ( ) ) ,
208205 // SAFETY: `layout` is non-zero in size,
209- size => unsafe {
206+ _ => unsafe {
210207 let raw_ptr = if zeroed { alloc_zeroed ( layout) } else { alloc ( layout) } ;
211208 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocError ) ?;
212- Ok ( ptr. cast_slice ( size ) )
209+ Ok ( ptr)
213210 } ,
214211 }
215212 }
@@ -224,13 +221,13 @@ impl Global {
224221 // "fit" always means a layout that is equal to the original, because our
225222 // `allocate()`, `grow()`, and `shrink()` implementations never returns a larger
226223 // allocation than requested.
227- // * Other conditions must be upheld by the caller, as per `Allocator ::deallocate()`'s
224+ // * Other conditions must be upheld by the caller, as per `Alloc ::deallocate()`'s
228225 // safety documentation.
229226 unsafe { dealloc_nonnull ( ptr, layout) }
230227 }
231228 }
232229
233- // SAFETY: Same as `Allocator ::grow`
230+ // SAFETY: Same as `Alloc ::grow`
234231 #[ inline]
235232 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
236233 fn grow_impl_runtime (
@@ -239,7 +236,7 @@ impl Global {
239236 old_layout : Layout ,
240237 new_layout : Layout ,
241238 zeroed : bool ,
242- ) -> Result < NonNull < [ u8 ] > , AllocError > {
239+ ) -> Result < NonNull < u8 > , AllocError > {
243240 debug_assert ! (
244241 new_layout. size( ) >= old_layout. size( ) ,
245242 "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
@@ -261,7 +258,7 @@ impl Global {
261258 if zeroed {
262259 raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
263260 }
264- Ok ( ptr. cast_slice ( new_size ) )
261+ Ok ( ptr)
265262 } ,
266263
267264 // SAFETY: because `new_layout.size()` must be greater than or equal to `old_size`,
@@ -271,14 +268,14 @@ impl Global {
271268 // for `dealloc` must be upheld by the caller.
272269 old_size => unsafe {
273270 let new_ptr = self . alloc_impl ( new_layout, zeroed) ?;
274- ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_ptr. as_mut_ptr ( ) , old_size) ;
271+ ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_ptr. as_ptr ( ) , old_size) ;
275272 self . deallocate ( ptr, old_layout) ;
276273 Ok ( new_ptr)
277274 } ,
278275 }
279276 }
280277
281- // SAFETY: Same as `Allocator ::grow`
278+ // SAFETY: Same as `Alloc ::grow`
282279 #[ inline]
283280 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
284281 fn shrink_impl_runtime (
@@ -287,7 +284,7 @@ impl Global {
287284 old_layout : Layout ,
288285 new_layout : Layout ,
289286 _zeroed : bool ,
290- ) -> Result < NonNull < [ u8 ] > , AllocError > {
287+ ) -> Result < NonNull < u8 > , AllocError > {
291288 debug_assert ! (
292289 new_layout. size( ) <= old_layout. size( ) ,
293290 "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
@@ -297,7 +294,7 @@ impl Global {
297294 // SAFETY: conditions must be upheld by the caller
298295 0 => unsafe {
299296 self . deallocate ( ptr, old_layout) ;
300- Ok ( new_layout. dangling_ptr ( ) . cast_slice ( 0 ) )
297+ Ok ( new_layout. dangling_ptr ( ) )
301298 } ,
302299
303300 // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
@@ -307,7 +304,7 @@ impl Global {
307304
308305 let raw_ptr = realloc_nonnull ( ptr, old_layout, new_size) ;
309306 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocError ) ?;
310- Ok ( ptr. cast_slice ( new_size ) )
307+ Ok ( ptr)
311308 } ,
312309
313310 // SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
@@ -317,26 +314,26 @@ impl Global {
317314 // for `dealloc` must be upheld by the caller.
318315 new_size => unsafe {
319316 let new_ptr = self . allocate ( new_layout) ?;
320- ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_ptr. as_mut_ptr ( ) , new_size) ;
317+ ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_ptr. as_ptr ( ) , new_size) ;
321318 self . deallocate ( ptr, old_layout) ;
322319 Ok ( new_ptr)
323320 } ,
324321 }
325322 }
326323
327- // SAFETY: Same as `Allocator ::allocate`
324+ // SAFETY: Same as `Alloc ::allocate`
328325 #[ inline]
329326 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
330327 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
331- const fn alloc_impl ( & self , layout : Layout , zeroed : bool ) -> Result < NonNull < [ u8 ] > , AllocError > {
328+ const fn alloc_impl ( & self , layout : Layout , zeroed : bool ) -> Result < NonNull < u8 > , AllocError > {
332329 core:: intrinsics:: const_eval_select (
333330 ( layout, zeroed) ,
334331 Global :: alloc_impl_const,
335332 Global :: alloc_impl_runtime,
336333 )
337334 }
338335
339- // SAFETY: Same as `Allocator ::deallocate`
336+ // SAFETY: Same as `Alloc ::deallocate`
340337 #[ inline]
341338 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
342339 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
@@ -348,7 +345,7 @@ impl Global {
348345 )
349346 }
350347
351- // SAFETY: Same as `Allocator ::grow`
348+ // SAFETY: Same as `Alloc ::grow`
352349 #[ inline]
353350 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
354351 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
@@ -358,15 +355,15 @@ impl Global {
358355 old_layout : Layout ,
359356 new_layout : Layout ,
360357 zeroed : bool ,
361- ) -> Result < NonNull < [ u8 ] > , AllocError > {
358+ ) -> Result < NonNull < u8 > , AllocError > {
362359 core:: intrinsics:: const_eval_select (
363360 ( self , ptr, old_layout, new_layout, zeroed) ,
364361 Global :: grow_shrink_impl_const,
365362 Global :: grow_impl_runtime,
366363 )
367364 }
368365
369- // SAFETY: Same as `Allocator ::shrink`
366+ // SAFETY: Same as `Alloc ::shrink`
370367 #[ inline]
371368 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
372369 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
@@ -375,7 +372,7 @@ impl Global {
375372 ptr : NonNull < u8 > ,
376373 old_layout : Layout ,
377374 new_layout : Layout ,
378- ) -> Result < NonNull < [ u8 ] > , AllocError > {
375+ ) -> Result < NonNull < u8 > , AllocError > {
379376 core:: intrinsics:: const_eval_select (
380377 ( self , ptr, old_layout, new_layout, false ) ,
381378 Global :: grow_shrink_impl_const,
@@ -386,9 +383,9 @@ impl Global {
386383 #[ inline]
387384 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
388385 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
389- const fn alloc_impl_const ( layout : Layout , zeroed : bool ) -> Result < NonNull < [ u8 ] > , AllocError > {
386+ const fn alloc_impl_const ( layout : Layout , zeroed : bool ) -> Result < NonNull < u8 > , AllocError > {
390387 match layout. size ( ) {
391- 0 => Ok ( layout. dangling_ptr ( ) . cast_slice ( 0 ) ) ,
388+ 0 => Ok ( layout. dangling_ptr ( ) ) ,
392389 // SAFETY: `layout` is non-zero in size,
393390 size => unsafe {
394391 let raw_ptr = core:: intrinsics:: const_allocate ( layout. size ( ) , layout. align ( ) ) ;
@@ -397,7 +394,7 @@ impl Global {
397394 // SAFETY: the pointer returned by `const_allocate` is valid to write to.
398395 ptr. write_bytes ( 0 , size) ;
399396 }
400- Ok ( ptr. cast_slice ( size ) )
397+ Ok ( ptr)
401398 } ,
402399 }
403400 }
@@ -423,13 +420,13 @@ impl Global {
423420 old_layout : Layout ,
424421 new_layout : Layout ,
425422 zeroed : bool ,
426- ) -> Result < NonNull < [ u8 ] > , AllocError > {
423+ ) -> Result < NonNull < u8 > , AllocError > {
427424 let new_ptr = self . alloc_impl ( new_layout, zeroed) ?;
428425 // SAFETY: both pointers are valid and this operations is in bounds.
429426 unsafe {
430427 ptr:: copy_nonoverlapping (
431428 ptr. as_ptr ( ) ,
432- new_ptr. as_mut_ptr ( ) ,
429+ new_ptr. as_ptr ( ) ,
433430 cmp:: min ( old_layout. size ( ) , new_layout. size ( ) ) ,
434431 ) ;
435432 }
@@ -440,18 +437,18 @@ impl Global {
440437 }
441438}
442439
443- #[ unstable ( feature = "allocator_api " , issue = "32838 " ) ]
440+ #[ stable ( feature = "allocator_api_mvp " , since = "CURRENT_RUSTC_VERSION " ) ]
444441#[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
445- const unsafe impl Allocator for Global {
442+ const unsafe impl Alloc for Global {
446443 #[ inline]
447444 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
448- fn allocate ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
445+ fn allocate ( & self , layout : Layout ) -> Result < NonNull < u8 > , AllocError > {
449446 self . alloc_impl ( layout, false )
450447 }
451448
452449 #[ inline]
453450 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
454- fn allocate_zeroed ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
451+ fn allocate_zeroed ( & self , layout : Layout ) -> Result < NonNull < u8 > , AllocError > {
455452 self . alloc_impl ( layout, true )
456453 }
457454
@@ -469,7 +466,7 @@ const unsafe impl Allocator for Global {
469466 ptr : NonNull < u8 > ,
470467 old_layout : Layout ,
471468 new_layout : Layout ,
472- ) -> Result < NonNull < [ u8 ] > , AllocError > {
469+ ) -> Result < NonNull < u8 > , AllocError > {
473470 // SAFETY: all conditions must be upheld by the caller
474471 unsafe { self . grow_impl ( ptr, old_layout, new_layout, false ) }
475472 }
@@ -481,7 +478,7 @@ const unsafe impl Allocator for Global {
481478 ptr : NonNull < u8 > ,
482479 old_layout : Layout ,
483480 new_layout : Layout ,
484- ) -> Result < NonNull < [ u8 ] > , AllocError > {
481+ ) -> Result < NonNull < u8 > , AllocError > {
485482 // SAFETY: all conditions must be upheld by the caller
486483 unsafe { self . grow_impl ( ptr, old_layout, new_layout, true ) }
487484 }
@@ -493,12 +490,16 @@ const unsafe impl Allocator for Global {
493490 ptr : NonNull < u8 > ,
494491 old_layout : Layout ,
495492 new_layout : Layout ,
496- ) -> Result < NonNull < [ u8 ] > , AllocError > {
493+ ) -> Result < NonNull < u8 > , AllocError > {
497494 // SAFETY: all conditions must be upheld by the caller
498495 unsafe { self . shrink_impl ( ptr, old_layout, new_layout) }
499496 }
500497}
501498
499+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
500+ #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
501+ const unsafe impl Allocator for Global { }
502+
502503// # Allocation error handler
503504
504505#[ cfg( not( no_global_oom_handling) ) ]
0 commit comments