@@ -16,25 +16,53 @@ use std::fmt;
16
16
use std:: u32;
17
17
18
18
newtype_index ! {
19
- pub struct CrateNum {
19
+ pub struct CrateId {
20
20
ENCODABLE = custom
21
- DEBUG_FORMAT = "crate{}" ,
21
+ }
22
+ }
23
+
24
+ #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
25
+ pub enum CrateNum {
26
+ /// Virtual crate for builtin macros
27
+ // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
28
+ // `CrateNum`s.
29
+ BuiltinMacros ,
30
+ /// A CrateNum value that indicates that something is wrong.
31
+ Invalid ,
32
+ /// A special CrateNum that we use for the tcx.rcache when decoding from
33
+ /// the incr. comp. cache.
34
+ ReservedForIncrCompCache ,
35
+ Index ( CrateId ) ,
36
+ }
37
+
38
+ impl :: std:: fmt:: Debug for CrateNum {
39
+ fn fmt ( & self , fmt : & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
40
+ match self {
41
+ CrateNum :: Index ( id) => write ! ( fmt, "crate{}" , id. private) ,
42
+ CrateNum :: Invalid => write ! ( fmt, "invalid crate" ) ,
43
+ CrateNum :: BuiltinMacros => write ! ( fmt, "bultin macros crate" ) ,
44
+ CrateNum :: ReservedForIncrCompCache => write ! ( fmt, "crate for decoding incr comp cache" ) ,
45
+ }
46
+ }
47
+ }
22
48
23
- /// Item definitions in the currently-compiled crate would have the CrateNum
24
- /// LOCAL_CRATE in their DefId.
25
- const LOCAL_CRATE = 0 ,
49
+ /// Item definitions in the currently-compiled crate would have the CrateNum
50
+ /// LOCAL_CRATE in their DefId.
51
+ pub const LOCAL_CRATE : CrateNum = CrateNum :: Index ( CrateId :: from_u32_const ( 0 ) ) ;
26
52
27
- /// Virtual crate for builtin macros
28
- // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
29
- // `CrateNum`s.
30
- const BUILTIN_MACROS_CRATE = CrateNum :: MAX_AS_U32 ,
31
53
32
- /// A CrateNum value that indicates that something is wrong.
33
- const INVALID_CRATE = CrateNum :: MAX_AS_U32 - 1 ,
54
+ impl Idx for CrateNum {
55
+ #[ inline]
56
+ fn new ( value : usize ) -> Self {
57
+ CrateNum :: Index ( Idx :: new ( value) )
58
+ }
34
59
35
- /// A special CrateNum that we use for the tcx.rcache when decoding from
36
- /// the incr. comp. cache.
37
- const RESERVED_FOR_INCR_COMP_CACHE = CrateNum :: MAX_AS_U32 - 2 ,
60
+ #[ inline]
61
+ fn index ( self ) -> usize {
62
+ match self {
63
+ CrateNum :: Index ( idx) => Idx :: index ( idx) ,
64
+ _ => bug ! ( "Tried to get crate index of {:?}" , self ) ,
65
+ }
38
66
}
39
67
}
40
68
@@ -43,12 +71,39 @@ impl CrateNum {
43
71
CrateNum :: from_usize ( x)
44
72
}
45
73
74
+ pub fn from_usize ( x : usize ) -> CrateNum {
75
+ CrateNum :: Index ( CrateId :: from_usize ( x) )
76
+ }
77
+
78
+ pub fn from_u32 ( x : u32 ) -> CrateNum {
79
+ CrateNum :: Index ( CrateId :: from_u32 ( x) )
80
+ }
81
+
82
+ pub fn as_usize ( self ) -> usize {
83
+ match self {
84
+ CrateNum :: Index ( id) => id. as_usize ( ) ,
85
+ _ => bug ! ( "tried to get index of nonstandard crate {:?}" , self ) ,
86
+ }
87
+ }
88
+
89
+ pub fn as_u32 ( self ) -> u32 {
90
+ match self {
91
+ CrateNum :: Index ( id) => id. as_u32 ( ) ,
92
+ _ => bug ! ( "tried to get index of nonstandard crate {:?}" , self ) ,
93
+ }
94
+ }
95
+
46
96
pub fn as_def_id ( & self ) -> DefId { DefId { krate : * self , index : CRATE_DEF_INDEX } }
47
97
}
48
98
49
99
impl fmt:: Display for CrateNum {
50
100
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
51
- fmt:: Display :: fmt ( & self . as_u32 ( ) , f)
101
+ match self {
102
+ CrateNum :: Index ( id) => fmt:: Display :: fmt ( & id. private , f) ,
103
+ CrateNum :: Invalid => write ! ( f, "invalid crate" ) ,
104
+ CrateNum :: BuiltinMacros => write ! ( f, "bultin macros crate" ) ,
105
+ CrateNum :: ReservedForIncrCompCache => write ! ( f, "crate for decoding incr comp cache" ) ,
106
+ }
52
107
}
53
108
}
54
109
0 commit comments