rapx/analysis/core/api_dependency/graph/
dep_node.rs

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