@@ -202,14 +202,14 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
202202impl Global {
203203 #[ inline]
204204 #[ 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 > {
205+ fn alloc_impl_runtime ( layout : Layout , zeroed : bool ) -> Result < NonNull < u8 > , AllocError > {
206206 match layout. size ( ) {
207- 0 => Ok ( layout. dangling_ptr ( ) . cast_slice ( 0 ) ) ,
207+ 0 => Ok ( layout. dangling_ptr ( ) ) ,
208208 // SAFETY: `layout` is non-zero in size,
209- size => unsafe {
209+ _ => unsafe {
210210 let raw_ptr = if zeroed { alloc_zeroed ( layout) } else { alloc ( layout) } ;
211211 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocError ) ?;
212- Ok ( ptr. cast_slice ( size ) )
212+ Ok ( ptr)
213213 } ,
214214 }
215215 }
@@ -224,13 +224,13 @@ impl Global {
224224 // "fit" always means a layout that is equal to the original, because our
225225 // `allocate()`, `grow()`, and `shrink()` implementations never returns a larger
226226 // allocation than requested.
227- // * Other conditions must be upheld by the caller, as per `Allocator ::deallocate()`'s
227+ // * Other conditions must be upheld by the caller, as per `Alloc ::deallocate()`'s
228228 // safety documentation.
229229 unsafe { dealloc_nonnull ( ptr, layout) }
230230 }
231231 }
232232
233- // SAFETY: Same as `Allocator ::grow`
233+ // SAFETY: Same as `Alloc ::grow`
234234 #[ inline]
235235 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
236236 fn grow_impl_runtime (
@@ -239,7 +239,7 @@ impl Global {
239239 old_layout : Layout ,
240240 new_layout : Layout ,
241241 zeroed : bool ,
242- ) -> Result < NonNull < [ u8 ] > , AllocError > {
242+ ) -> Result < NonNull < u8 > , AllocError > {
243243 debug_assert ! (
244244 new_layout. size( ) >= old_layout. size( ) ,
245245 "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
@@ -261,7 +261,7 @@ impl Global {
261261 if zeroed {
262262 raw_ptr. add ( old_size) . write_bytes ( 0 , new_size - old_size) ;
263263 }
264- Ok ( ptr. cast_slice ( new_size ) )
264+ Ok ( ptr)
265265 } ,
266266
267267 // SAFETY: because `new_layout.size()` must be greater than or equal to `old_size`,
@@ -271,14 +271,14 @@ impl Global {
271271 // for `dealloc` must be upheld by the caller.
272272 old_size => unsafe {
273273 let new_ptr = self . alloc_impl ( new_layout, zeroed) ?;
274- ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_ptr. as_mut_ptr ( ) , old_size) ;
275- self . deallocate ( ptr, old_layout) ;
274+ ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_ptr. as_ptr ( ) , old_size) ;
275+ self . alloc_ref ( ) . deallocate ( ptr, old_layout) ;
276276 Ok ( new_ptr)
277277 } ,
278278 }
279279 }
280280
281- // SAFETY: Same as `Allocator ::grow`
281+ // SAFETY: Same as `Alloc ::grow`
282282 #[ inline]
283283 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
284284 fn shrink_impl_runtime (
@@ -287,7 +287,7 @@ impl Global {
287287 old_layout : Layout ,
288288 new_layout : Layout ,
289289 _zeroed : bool ,
290- ) -> Result < NonNull < [ u8 ] > , AllocError > {
290+ ) -> Result < NonNull < u8 > , AllocError > {
291291 debug_assert ! (
292292 new_layout. size( ) <= old_layout. size( ) ,
293293 "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
@@ -296,8 +296,8 @@ impl Global {
296296 match new_layout. size ( ) {
297297 // SAFETY: conditions must be upheld by the caller
298298 0 => unsafe {
299- self . deallocate ( ptr, old_layout) ;
300- Ok ( new_layout. dangling_ptr ( ) . cast_slice ( 0 ) )
299+ self . alloc_ref ( ) . deallocate ( ptr, old_layout) ;
300+ Ok ( new_layout. dangling_ptr ( ) )
301301 } ,
302302
303303 // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
@@ -307,7 +307,7 @@ impl Global {
307307
308308 let raw_ptr = realloc_nonnull ( ptr, old_layout, new_size) ;
309309 let ptr = NonNull :: new ( raw_ptr) . ok_or ( AllocError ) ?;
310- Ok ( ptr. cast_slice ( new_size ) )
310+ Ok ( ptr)
311311 } ,
312312
313313 // SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
@@ -316,27 +316,27 @@ impl Global {
316316 // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
317317 // for `dealloc` must be upheld by the caller.
318318 new_size => unsafe {
319- let new_ptr = self . allocate ( new_layout) ?;
320- ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_ptr. as_mut_ptr ( ) , new_size) ;
321- self . deallocate ( ptr, old_layout) ;
319+ let new_ptr = self . alloc_ref ( ) . allocate ( new_layout) ?;
320+ ptr:: copy_nonoverlapping ( ptr. as_ptr ( ) , new_ptr. as_ptr ( ) , new_size) ;
321+ self . alloc_ref ( ) . deallocate ( ptr, old_layout) ;
322322 Ok ( new_ptr)
323323 } ,
324324 }
325325 }
326326
327- // SAFETY: Same as `Allocator ::allocate`
327+ // SAFETY: Same as `Alloc ::allocate`
328328 #[ inline]
329329 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
330330 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
331- const fn alloc_impl ( & self , layout : Layout , zeroed : bool ) -> Result < NonNull < [ u8 ] > , AllocError > {
331+ const fn alloc_impl ( & self , layout : Layout , zeroed : bool ) -> Result < NonNull < u8 > , AllocError > {
332332 core:: intrinsics:: const_eval_select (
333333 ( layout, zeroed) ,
334334 Global :: alloc_impl_const,
335335 Global :: alloc_impl_runtime,
336336 )
337337 }
338338
339- // SAFETY: Same as `Allocator ::deallocate`
339+ // SAFETY: Same as `Alloc ::deallocate`
340340 #[ inline]
341341 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
342342 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
@@ -348,7 +348,7 @@ impl Global {
348348 )
349349 }
350350
351- // SAFETY: Same as `Allocator ::grow`
351+ // SAFETY: Same as `Alloc ::grow`
352352 #[ inline]
353353 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
354354 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
@@ -358,15 +358,15 @@ impl Global {
358358 old_layout : Layout ,
359359 new_layout : Layout ,
360360 zeroed : bool ,
361- ) -> Result < NonNull < [ u8 ] > , AllocError > {
361+ ) -> Result < NonNull < u8 > , AllocError > {
362362 core:: intrinsics:: const_eval_select (
363363 ( self , ptr, old_layout, new_layout, zeroed) ,
364364 Global :: grow_shrink_impl_const,
365365 Global :: grow_impl_runtime,
366366 )
367367 }
368368
369- // SAFETY: Same as `Allocator ::shrink`
369+ // SAFETY: Same as `Alloc ::shrink`
370370 #[ inline]
371371 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
372372 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
@@ -375,7 +375,7 @@ impl Global {
375375 ptr : NonNull < u8 > ,
376376 old_layout : Layout ,
377377 new_layout : Layout ,
378- ) -> Result < NonNull < [ u8 ] > , AllocError > {
378+ ) -> Result < NonNull < u8 > , AllocError > {
379379 core:: intrinsics:: const_eval_select (
380380 ( self , ptr, old_layout, new_layout, false ) ,
381381 Global :: grow_shrink_impl_const,
@@ -386,9 +386,9 @@ impl Global {
386386 #[ inline]
387387 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
388388 #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
389- const fn alloc_impl_const ( layout : Layout , zeroed : bool ) -> Result < NonNull < [ u8 ] > , AllocError > {
389+ const fn alloc_impl_const ( layout : Layout , zeroed : bool ) -> Result < NonNull < u8 > , AllocError > {
390390 match layout. size ( ) {
391- 0 => Ok ( layout. dangling_ptr ( ) . cast_slice ( 0 ) ) ,
391+ 0 => Ok ( layout. dangling_ptr ( ) ) ,
392392 // SAFETY: `layout` is non-zero in size,
393393 size => unsafe {
394394 let raw_ptr = core:: intrinsics:: const_allocate ( layout. size ( ) , layout. align ( ) ) ;
@@ -397,7 +397,7 @@ impl Global {
397397 // SAFETY: the pointer returned by `const_allocate` is valid to write to.
398398 ptr. write_bytes ( 0 , size) ;
399399 }
400- Ok ( ptr. cast_slice ( size ) )
400+ Ok ( ptr)
401401 } ,
402402 }
403403 }
@@ -423,13 +423,13 @@ impl Global {
423423 old_layout : Layout ,
424424 new_layout : Layout ,
425425 zeroed : bool ,
426- ) -> Result < NonNull < [ u8 ] > , AllocError > {
426+ ) -> Result < NonNull < u8 > , AllocError > {
427427 let new_ptr = self . alloc_impl ( new_layout, zeroed) ?;
428428 // SAFETY: both pointers are valid and this operations is in bounds.
429429 unsafe {
430430 ptr:: copy_nonoverlapping (
431431 ptr. as_ptr ( ) ,
432- new_ptr. as_mut_ptr ( ) ,
432+ new_ptr. as_ptr ( ) ,
433433 cmp:: min ( old_layout. size ( ) , new_layout. size ( ) ) ,
434434 ) ;
435435 }
@@ -440,26 +440,33 @@ impl Global {
440440 }
441441}
442442
443+ // Only needed while `Alloc::allocate` and `Allocator::allocate`
444+ // coexist for `hashbrown` compatibility.
445+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
446+ #[ derive( Debug ) ]
447+ #[ doc( hidden) ]
448+ pub struct __GlobalButOnlyAlloc ( Global ) ;
449+
443450#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
444451#[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
445- const unsafe impl Allocator for Global {
452+ const unsafe impl Alloc for __GlobalButOnlyAlloc {
446453 #[ inline]
447454 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
448- fn allocate ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
449- self . alloc_impl ( layout, false )
455+ fn allocate ( & self , layout : Layout ) -> Result < NonNull < u8 > , AllocError > {
456+ self . 0 . alloc_impl ( layout, false )
450457 }
451458
452459 #[ inline]
453460 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
454- fn allocate_zeroed ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
455- self . alloc_impl ( layout, true )
461+ fn allocate_zeroed ( & self , layout : Layout ) -> Result < NonNull < u8 > , AllocError > {
462+ self . 0 . alloc_impl ( layout, true )
456463 }
457464
458465 #[ inline]
459466 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
460467 unsafe fn deallocate ( & self , ptr : NonNull < u8 > , layout : Layout ) {
461468 // SAFETY: all conditions must be upheld by the caller
462- unsafe { self . deallocate_impl ( ptr, layout) }
469+ unsafe { self . 0 . deallocate_impl ( ptr, layout) }
463470 }
464471
465472 #[ inline]
@@ -469,9 +476,9 @@ const unsafe impl Allocator for Global {
469476 ptr : NonNull < u8 > ,
470477 old_layout : Layout ,
471478 new_layout : Layout ,
472- ) -> Result < NonNull < [ u8 ] > , AllocError > {
479+ ) -> Result < NonNull < u8 > , AllocError > {
473480 // SAFETY: all conditions must be upheld by the caller
474- unsafe { self . grow_impl ( ptr, old_layout, new_layout, false ) }
481+ unsafe { self . 0 . grow_impl ( ptr, old_layout, new_layout, false ) }
475482 }
476483
477484 #[ inline]
@@ -481,9 +488,9 @@ const unsafe impl Allocator for Global {
481488 ptr : NonNull < u8 > ,
482489 old_layout : Layout ,
483490 new_layout : Layout ,
484- ) -> Result < NonNull < [ u8 ] > , AllocError > {
491+ ) -> Result < NonNull < u8 > , AllocError > {
485492 // SAFETY: all conditions must be upheld by the caller
486- unsafe { self . grow_impl ( ptr, old_layout, new_layout, true ) }
493+ unsafe { self . 0 . grow_impl ( ptr, old_layout, new_layout, true ) }
487494 }
488495
489496 #[ inline]
@@ -493,9 +500,19 @@ const unsafe impl Allocator for Global {
493500 ptr : NonNull < u8 > ,
494501 old_layout : Layout ,
495502 new_layout : Layout ,
496- ) -> Result < NonNull < [ u8 ] > , AllocError > {
503+ ) -> Result < NonNull < u8 > , AllocError > {
497504 // SAFETY: all conditions must be upheld by the caller
498- unsafe { self . shrink_impl ( ptr, old_layout, new_layout) }
505+ unsafe { self . 0 . shrink_impl ( ptr, old_layout, new_layout) }
506+ }
507+ }
508+
509+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
510+ #[ rustc_const_unstable( feature = "const_heap" , issue = "79597" ) ]
511+ const unsafe impl Allocator for Global {
512+ type Alloc = __GlobalButOnlyAlloc ;
513+ #[ inline( always) ]
514+ fn alloc_ref ( & self ) -> & Self :: Alloc {
515+ & __GlobalButOnlyAlloc ( Global )
499516 }
500517}
501518
0 commit comments