@@ -96,34 +96,20 @@ impl fmt::Display for CrateNum {
96
96
impl serialize:: UseSpecializedEncodable for CrateNum { }
97
97
impl serialize:: UseSpecializedDecodable for CrateNum { }
98
98
99
- /// A DefIndex is an index into the hir-map for a crate, identifying a
100
- /// particular definition. It should really be considered an interned
101
- /// shorthand for a particular DefPath.
102
- #[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Copy ) ]
103
- pub struct DefIndex ( u32 ) ;
104
-
105
- /// The crate root is always assigned index 0 by the AST Map code,
106
- /// thanks to `NodeCollector::new`.
107
- pub const CRATE_DEF_INDEX : DefIndex = DefIndex ( 0 ) ;
99
+ newtype_index ! {
100
+ /// A DefIndex is an index into the hir-map for a crate, identifying a
101
+ /// particular definition. It should really be considered an interned
102
+ /// shorthand for a particular DefPath.
103
+ pub struct DefIndex {
104
+ DEBUG_FORMAT = "DefIndex({})" ,
108
105
109
- impl fmt :: Debug for DefIndex {
110
- fn fmt ( & self , f : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
111
- write ! ( f , "DefIndex({})" , self . as_array_index ( ) )
106
+ /// The crate root is always assigned index 0 by the AST Map code,
107
+ /// thanks to `NodeCollector::new`.
108
+ const CRATE_DEF_INDEX = 0 ,
112
109
}
113
110
}
114
111
115
112
impl DefIndex {
116
- /// Converts this DefIndex into a zero-based array index.
117
- #[ inline]
118
- pub fn as_array_index ( & self ) -> usize {
119
- self . 0 as usize
120
- }
121
-
122
- #[ inline]
123
- pub fn from_array_index ( i : usize ) -> DefIndex {
124
- DefIndex ( i as u32 )
125
- }
126
-
127
113
// Proc macros from a proc-macro crate have a kind of virtual DefIndex. This
128
114
// function maps the index of the macro within the crate (which is also the
129
115
// index of the macro in the CrateMetadata::proc_macros array) to the
@@ -132,7 +118,7 @@ impl DefIndex {
132
118
// DefIndex for proc macros start from FIRST_FREE_DEF_INDEX,
133
119
// because the first FIRST_FREE_DEF_INDEX indexes are reserved
134
120
// for internal use.
135
- let def_index = DefIndex :: from_array_index (
121
+ let def_index = DefIndex :: from (
136
122
proc_macro_index. checked_add ( FIRST_FREE_DEF_INDEX )
137
123
. expect ( "integer overflow adding `proc_macro_index`" ) ) ;
138
124
assert ! ( def_index != CRATE_DEF_INDEX ) ;
@@ -141,19 +127,11 @@ impl DefIndex {
141
127
142
128
// This function is the reverse of from_proc_macro_index() above.
143
129
pub fn to_proc_macro_index ( self : DefIndex ) -> usize {
144
- self . as_array_index ( ) . checked_sub ( FIRST_FREE_DEF_INDEX )
130
+ self . index ( ) . checked_sub ( FIRST_FREE_DEF_INDEX )
145
131
. unwrap_or_else ( || {
146
132
bug ! ( "using local index {:?} as proc-macro index" , self )
147
133
} )
148
134
}
149
-
150
- pub fn from_raw_u32 ( x : u32 ) -> DefIndex {
151
- DefIndex ( x)
152
- }
153
-
154
- pub fn as_raw_u32 ( & self ) -> u32 {
155
- self . 0
156
- }
157
135
}
158
136
159
137
impl serialize:: UseSpecializedEncodable for DefIndex { }
@@ -169,7 +147,7 @@ pub struct DefId {
169
147
170
148
impl fmt:: Debug for DefId {
171
149
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
172
- write ! ( f, "DefId({}:{}" , self . krate, self . index. as_array_index ( ) ) ?;
150
+ write ! ( f, "DefId({}:{}" , self . krate, self . index. index ( ) ) ?;
173
151
174
152
ty:: tls:: with_opt ( |opt_tcx| {
175
153
if let Some ( tcx) = opt_tcx {
0 commit comments