@@ -379,6 +379,28 @@ pub enum GlobalAlloc<'tcx> {
379379 Memory ( & ' tcx Allocation ) ,
380380}
381381
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+
382404pub struct AllocMap < ' tcx > {
383405 /// Maps `AllocId`s to their corresponding allocations.
384406 alloc_map : FxHashMap < AllocId , GlobalAlloc < ' tcx > > ,
@@ -491,22 +513,6 @@ impl<'tcx> TyCtxt<'tcx> {
491513 self . alloc_map . lock ( ) . alloc_map . get ( & id) . cloned ( )
492514 }
493515
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-
510516 /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
511517 /// call this function twice, even with the same `Allocation` will ICE the compiler.
512518 pub fn set_alloc_id_memory ( & self , id : AllocId , mem : & ' tcx Allocation ) {
0 commit comments