rustc_middle/query/into_query_key.rs
1use rustc_hir::OwnerId;
2use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, ModDefId};
3
4/// Argument-conversion trait used by some queries and other `TyCtxt` methods.
5///
6/// A function that accepts an `impl IntoQueryKey<DefId>` argument can be thought
7/// of as taking a [`DefId`], except that callers can also pass a [`LocalDefId`]
8/// or values of other narrower ID types, as long as they have a trivial conversion
9/// to `DefId`.
10///
11/// Using a dedicated trait instead of [`Into`] makes the purpose of the conversion
12/// more explicit, and makes occurrences easier to search for.
13pub trait IntoQueryKey<K> {
14 /// Argument conversion from `Self` to `K`.
15 /// This should always be a very cheap conversion, e.g. [`LocalDefId::to_def_id`].
16 fn into_query_key(self) -> K;
17}
18
19/// Any type can be converted to itself.
20///
21/// This is useful in generic or macro-generated code where we don't know whether
22/// conversion is actually needed, so that we can do a conversion unconditionally.
23impl<K> IntoQueryKey<K> for K {
24 #[inline(always)]
25 fn into_query_key(self) -> K {
26 self
27 }
28}
29
30impl IntoQueryKey<LocalDefId> for OwnerId {
31 #[inline(always)]
32 fn into_query_key(self) -> LocalDefId {
33 self.def_id
34 }
35}
36
37impl IntoQueryKey<DefId> for LocalDefId {
38 #[inline(always)]
39 fn into_query_key(self) -> DefId {
40 self.to_def_id()
41 }
42}
43
44impl IntoQueryKey<DefId> for OwnerId {
45 #[inline(always)]
46 fn into_query_key(self) -> DefId {
47 self.to_def_id()
48 }
49}
50
51impl IntoQueryKey<DefId> for ModDefId {
52 #[inline(always)]
53 fn into_query_key(self) -> DefId {
54 self.to_def_id()
55 }
56}
57
58impl IntoQueryKey<DefId> for LocalModDefId {
59 #[inline(always)]
60 fn into_query_key(self) -> DefId {
61 self.to_def_id()
62 }
63}
64
65impl IntoQueryKey<LocalDefId> for LocalModDefId {
66 #[inline(always)]
67 fn into_query_key(self) -> LocalDefId {
68 self.into()
69 }
70}