rapx/analysis/api_dependency/graph/
dep_node.rs

1use std::hash::Hash;
2
3use super::ty_wrapper::TyWrapper;
4use rustc_middle::ty::{self, Ty, TyCtxt};
5
6use rustc_hir::def_id::DefId;
7
8#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
9pub enum DepNode<'tcx> {
10    Api(DefId, ty::GenericArgsRef<'tcx>),
11    Ty(TyWrapper<'tcx>),
12}
13
14impl<'tcx> DepNode<'tcx> {
15    pub fn api(id: DefId, args: ty::GenericArgsRef<'tcx>) -> DepNode<'tcx> {
16        DepNode::Api(id, args)
17    }
18    pub fn ty(ty: Ty<'tcx>) -> DepNode<'tcx> {
19        DepNode::Ty(TyWrapper::from(ty))
20    }
21    pub fn is_ty(&self) -> bool {
22        matches!(self, DepNode::Ty(_))
23    }
24    pub fn is_api(&self) -> bool {
25        matches!(self, DepNode::Api(..))
26    }
27
28    pub fn as_ty(&self) -> Option<TyWrapper<'tcx>> {
29        match self {
30            DepNode::Ty(ty) => Some(*ty),
31            _ => None,
32        }
33    }
34
35    pub fn expect_ty(&self) -> TyWrapper<'tcx> {
36        match self {
37            DepNode::Ty(ty) => *ty,
38            _ => panic!("{self:?} is not a ty"),
39        }
40    }
41
42    pub fn expect_api(&self) -> (DefId, ty::GenericArgsRef<'tcx>) {
43        match self {
44            DepNode::Api(did, args) => (*did, args),
45            _ => {
46                panic!("{self:?} is not an api")
47            }
48        }
49    }
50
51    pub fn desc_str(&self, tcx: TyCtxt<'tcx>) -> String {
52        match self {
53            DepNode::Api(def_id, args) => tcx.def_path_str_with_args(*def_id, *args),
54            DepNode::Ty(ty) => ty.desc_str(tcx),
55        }
56    }
57}