@@ -18,23 +18,51 @@ use std::u32;
18
18
newtype_index ! {
19
19
pub struct CrateNum {
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
+ }
22
37
23
- /// Item definitions in the currently-compiled crate would have the CrateNum
24
- /// LOCAL_CRATE in their DefId.
25
- const LOCAL_CRATE = 0 ,
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. 0 ) ,
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
+ }
26
48
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 ,
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 ( 0 ) ) ;
31
52
32
- /// A CrateNum value that indicates that something is wrong.
33
- const INVALID_CRATE = CrateNum :: MAX_AS_U32 - 1 ,
34
53
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 ,
54
+ impl Idx for CrateNum {
55
+ #[ inline]
56
+ fn new ( value : usize ) -> Self {
57
+ CrateNum :: Index ( Idx :: new ( value) )
58
+ }
59
+
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
@@ -48,7 +76,12 @@ impl CrateNum {
48
76
49
77
impl fmt:: Display for CrateNum {
50
78
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
51
- fmt:: Display :: fmt ( & self . as_u32 ( ) , f)
79
+ match self {
80
+ CrateNum :: Index ( id) => fmt:: Display :: fmt ( & id. 0 , f) ,
81
+ CrateNum :: Invalid => write ! ( f, "invalid crate" ) ,
82
+ CrateNum :: BuiltinMacros => write ! ( f, "bultin macros crate" ) ,
83
+ CrateNum :: ReservedForIncrCompCache => write ! ( f, "crate for decoding incr comp cache" ) ,
84
+ }
52
85
}
53
86
}
54
87
0 commit comments