@@ -4,11 +4,14 @@ pub use Primitive::*;
4
4
use crate :: spec:: Target ;
5
5
6
6
use std:: convert:: { TryFrom , TryInto } ;
7
+ use std:: fmt;
7
8
use std:: num:: NonZeroUsize ;
8
9
use std:: ops:: { Add , AddAssign , Deref , Mul , Range , RangeInclusive , Sub } ;
10
+ use std:: str:: FromStr ;
9
11
10
12
use rustc_index:: vec:: { Idx , IndexVec } ;
11
13
use rustc_macros:: HashStable_Generic ;
14
+ use rustc_serialize:: json:: { Json , ToJson } ;
12
15
use rustc_span:: Span ;
13
16
14
17
pub mod call;
@@ -152,22 +155,19 @@ impl TargetDataLayout {
152
155
}
153
156
154
157
// Perform consistency checks against the Target information.
155
- let endian_str = match dl. endian {
156
- Endian :: Little => "little" ,
157
- Endian :: Big => "big" ,
158
- } ;
159
- if endian_str != target. endian {
158
+ if dl. endian != target. endian {
160
159
return Err ( format ! (
161
160
"inconsistent target specification: \" data-layout\" claims \
162
- architecture is {}-endian, while \" target-endian\" is `{}`",
163
- endian_str, target. endian
161
+ architecture is {}-endian, while \" target-endian\" is `{}`",
162
+ dl. endian. as_str( ) ,
163
+ target. endian. as_str( ) ,
164
164
) ) ;
165
165
}
166
166
167
167
if dl. pointer_size . bits ( ) != target. pointer_width . into ( ) {
168
168
return Err ( format ! (
169
169
"inconsistent target specification: \" data-layout\" claims \
170
- pointers are {}-bit, while \" target-pointer-width\" is `{}`",
170
+ pointers are {}-bit, while \" target-pointer-width\" is `{}`",
171
171
dl. pointer_size. bits( ) ,
172
172
target. pointer_width
173
173
) ) ;
@@ -234,6 +234,39 @@ pub enum Endian {
234
234
Big ,
235
235
}
236
236
237
+ impl Endian {
238
+ pub fn as_str ( & self ) -> & ' static str {
239
+ match self {
240
+ Self :: Little => "little" ,
241
+ Self :: Big => "big" ,
242
+ }
243
+ }
244
+ }
245
+
246
+ impl fmt:: Debug for Endian {
247
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
248
+ f. write_str ( self . as_str ( ) )
249
+ }
250
+ }
251
+
252
+ impl FromStr for Endian {
253
+ type Err = String ;
254
+
255
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
256
+ match s {
257
+ "little" => Ok ( Self :: Little ) ,
258
+ "big" => Ok ( Self :: Big ) ,
259
+ _ => Err ( format ! ( r#"unknown endian: "{}""# , s) ) ,
260
+ }
261
+ }
262
+ }
263
+
264
+ impl ToJson for Endian {
265
+ fn to_json ( & self ) -> Json {
266
+ self . as_str ( ) . to_json ( )
267
+ }
268
+ }
269
+
237
270
/// Size of a type in bytes.
238
271
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug , Encodable , Decodable ) ]
239
272
#[ derive( HashStable_Generic ) ]
0 commit comments