@@ -1988,29 +1988,22 @@ impl<'tcx> TyCtxt<'tcx> {
1988
1988
self . impl_trait_ref ( def_id) . map ( |tr| tr. skip_binder ( ) . def_id )
1989
1989
}
1990
1990
1991
+ /// If the given `DefId` is an associated item, returns the `DefId` of the parent trait or impl.
1992
+ pub fn assoc_parent ( self , def_id : DefId ) -> Option < DefId > {
1993
+ self . def_kind ( def_id) . is_assoc ( ) . then ( || self . parent ( def_id) )
1994
+ }
1995
+
1991
1996
/// If the given `DefId` describes an item belonging to a trait,
1992
1997
/// returns the `DefId` of the trait that the trait item belongs to;
1993
1998
/// otherwise, returns `None`.
1994
1999
pub fn trait_of_item ( self , def_id : DefId ) -> Option < DefId > {
1995
- if let DefKind :: AssocConst | DefKind :: AssocFn | DefKind :: AssocTy = self . def_kind ( def_id) {
1996
- let parent = self . parent ( def_id) ;
1997
- if let DefKind :: Trait = self . def_kind ( parent) {
1998
- return Some ( parent) ;
1999
- }
2000
- }
2001
- None
2000
+ self . assoc_parent ( def_id) . filter ( |id| self . def_kind ( id) == DefKind :: Trait )
2002
2001
}
2003
2002
2004
2003
/// If the given `DefId` describes a method belonging to an impl, returns the
2005
2004
/// `DefId` of the impl that the method belongs to; otherwise, returns `None`.
2006
2005
pub fn impl_of_method ( self , def_id : DefId ) -> Option < DefId > {
2007
- if let DefKind :: AssocConst | DefKind :: AssocFn | DefKind :: AssocTy = self . def_kind ( def_id) {
2008
- let parent = self . parent ( def_id) ;
2009
- if let DefKind :: Impl { .. } = self . def_kind ( parent) {
2010
- return Some ( parent) ;
2011
- }
2012
- }
2013
- None
2006
+ self . assoc_parent ( def_id) . filter ( |id| matches ! ( self . def_kind( id) , DefKind :: Impl { .. } ) )
2014
2007
}
2015
2008
2016
2009
pub fn is_exportable ( self , def_id : DefId ) -> bool {
0 commit comments