@@ -2653,6 +2653,37 @@ impl ItemInNs {
26532653 }
26542654}
26552655
2656+ /// Invariant: `inner.as_extern_assoc_item(db).is_some()`
2657+ /// We do not actively enforce this invariant.
2658+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
2659+ pub enum ExternAssocItem {
2660+ Function ( Function ) ,
2661+ Static ( Static ) ,
2662+ TypeAlias ( TypeAlias ) ,
2663+ }
2664+
2665+ pub trait AsExternAssocItem {
2666+ fn as_extern_assoc_item ( self , db : & dyn HirDatabase ) -> Option < ExternAssocItem > ;
2667+ }
2668+
2669+ impl AsExternAssocItem for Function {
2670+ fn as_extern_assoc_item ( self , db : & dyn HirDatabase ) -> Option < ExternAssocItem > {
2671+ as_extern_assoc_item ( db, ExternAssocItem :: Function , self . id )
2672+ }
2673+ }
2674+
2675+ impl AsExternAssocItem for Static {
2676+ fn as_extern_assoc_item ( self , db : & dyn HirDatabase ) -> Option < ExternAssocItem > {
2677+ as_extern_assoc_item ( db, ExternAssocItem :: Static , self . id )
2678+ }
2679+ }
2680+
2681+ impl AsExternAssocItem for TypeAlias {
2682+ fn as_extern_assoc_item ( self , db : & dyn HirDatabase ) -> Option < ExternAssocItem > {
2683+ as_extern_assoc_item ( db, ExternAssocItem :: TypeAlias , self . id )
2684+ }
2685+ }
2686+
26562687/// Invariant: `inner.as_assoc_item(db).is_some()`
26572688/// We do not actively enforce this invariant.
26582689#[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
@@ -2727,6 +2758,63 @@ where
27272758 }
27282759}
27292760
2761+ fn as_extern_assoc_item < ' db , ID , DEF , LOC > (
2762+ db : & ( dyn HirDatabase + ' db ) ,
2763+ ctor : impl FnOnce ( DEF ) -> ExternAssocItem ,
2764+ id : ID ,
2765+ ) -> Option < ExternAssocItem >
2766+ where
2767+ ID : Lookup < Database < ' db > = dyn DefDatabase + ' db , Data = AssocItemLoc < LOC > > ,
2768+ DEF : From < ID > ,
2769+ LOC : ItemTreeNode ,
2770+ {
2771+ match id. lookup ( db. upcast ( ) ) . container {
2772+ ItemContainerId :: ExternBlockId ( _) => Some ( ctor ( DEF :: from ( id) ) ) ,
2773+ ItemContainerId :: TraitId ( _) | ItemContainerId :: ImplId ( _) | ItemContainerId :: ModuleId ( _) => {
2774+ None
2775+ }
2776+ }
2777+ }
2778+
2779+ impl ExternAssocItem {
2780+ pub fn name ( self , db : & dyn HirDatabase ) -> Name {
2781+ match self {
2782+ Self :: Function ( it) => it. name ( db) ,
2783+ Self :: Static ( it) => it. name ( db) ,
2784+ Self :: TypeAlias ( it) => it. name ( db) ,
2785+ }
2786+ }
2787+
2788+ pub fn module ( self , db : & dyn HirDatabase ) -> Module {
2789+ match self {
2790+ Self :: Function ( f) => f. module ( db) ,
2791+ Self :: Static ( c) => c. module ( db) ,
2792+ Self :: TypeAlias ( t) => t. module ( db) ,
2793+ }
2794+ }
2795+
2796+ pub fn as_function ( self ) -> Option < Function > {
2797+ match self {
2798+ Self :: Function ( v) => Some ( v) ,
2799+ _ => None ,
2800+ }
2801+ }
2802+
2803+ pub fn as_static ( self ) -> Option < Static > {
2804+ match self {
2805+ Self :: Static ( v) => Some ( v) ,
2806+ _ => None ,
2807+ }
2808+ }
2809+
2810+ pub fn as_type_alias ( self ) -> Option < TypeAlias > {
2811+ match self {
2812+ Self :: TypeAlias ( v) => Some ( v) ,
2813+ _ => None ,
2814+ }
2815+ }
2816+ }
2817+
27302818impl AssocItem {
27312819 pub fn name ( self , db : & dyn HirDatabase ) -> Option < Name > {
27322820 match self {
0 commit comments