@@ -379,6 +379,28 @@ pub enum GlobalAlloc<'tcx> {
379
379
Memory ( & ' tcx Allocation ) ,
380
380
}
381
381
382
+ impl GlobalAlloc < ' tcx > {
383
+ /// Panics if the `GlobalAlloc` does not refer to an `GlobalAlloc::Memory`
384
+ #[ track_caller]
385
+ #[ inline]
386
+ pub fn unwrap_memory ( & self ) -> & ' tcx Allocation {
387
+ match * self {
388
+ GlobalAlloc :: Memory ( mem) => mem,
389
+ _ => bug ! ( "expected memory, got {:?}" , self ) ,
390
+ }
391
+ }
392
+
393
+ /// Panics if the `GlobalAlloc` is not `GlobalAlloc::Function`
394
+ #[ track_caller]
395
+ #[ inline]
396
+ pub fn unwrap_fn ( & self ) -> Instance < ' tcx > {
397
+ match * self {
398
+ GlobalAlloc :: Function ( instance) => instance,
399
+ _ => bug ! ( "expected function, got {:?}" , self ) ,
400
+ }
401
+ }
402
+ }
403
+
382
404
pub struct AllocMap < ' tcx > {
383
405
/// Maps `AllocId`s to their corresponding allocations.
384
406
alloc_map : FxHashMap < AllocId , GlobalAlloc < ' tcx > > ,
@@ -491,22 +513,6 @@ impl<'tcx> TyCtxt<'tcx> {
491
513
self . alloc_map . lock ( ) . alloc_map . get ( & id) . cloned ( )
492
514
}
493
515
494
- /// Panics if the `AllocId` does not refer to an `Allocation`
495
- pub fn unwrap_memory ( & self , id : AllocId ) -> & ' tcx Allocation {
496
- match self . get_global_alloc ( id) {
497
- Some ( GlobalAlloc :: Memory ( mem) ) => mem,
498
- _ => bug ! ( "expected allocation ID {} to point to memory" , id) ,
499
- }
500
- }
501
-
502
- /// Panics if the `AllocId` does not refer to a function
503
- pub fn unwrap_fn ( & self , id : AllocId ) -> Instance < ' tcx > {
504
- match self . get_global_alloc ( id) {
505
- Some ( GlobalAlloc :: Function ( instance) ) => instance,
506
- _ => bug ! ( "expected allocation ID {} to point to a function" , id) ,
507
- }
508
- }
509
-
510
516
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
511
517
/// call this function twice, even with the same `Allocation` will ICE the compiler.
512
518
pub fn set_alloc_id_memory ( & self , id : AllocId , mem : & ' tcx Allocation ) {
0 commit comments