@@ -4,11 +4,14 @@ pub use Primitive::*;
44use crate :: spec:: Target ;
55
66use std:: convert:: { TryFrom , TryInto } ;
7+ use std:: fmt;
78use std:: num:: NonZeroUsize ;
89use std:: ops:: { Add , AddAssign , Deref , Mul , Range , RangeInclusive , Sub } ;
10+ use std:: str:: FromStr ;
911
1012use rustc_index:: vec:: { Idx , IndexVec } ;
1113use rustc_macros:: HashStable_Generic ;
14+ use rustc_serialize:: json:: { Json , ToJson } ;
1215use rustc_span:: Span ;
1316
1417pub mod call;
@@ -152,22 +155,19 @@ impl TargetDataLayout {
152155 }
153156
154157 // 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 {
160159 return Err ( format ! (
161160 "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( ) ,
164164 ) ) ;
165165 }
166166
167167 if dl. pointer_size . bits ( ) != target. pointer_width . into ( ) {
168168 return Err ( format ! (
169169 "inconsistent target specification: \" data-layout\" claims \
170- pointers are {}-bit, while \" target-pointer-width\" is `{}`",
170+ pointers are {}-bit, while \" target-pointer-width\" is `{}`",
171171 dl. pointer_size. bits( ) ,
172172 target. pointer_width
173173 ) ) ;
@@ -234,6 +234,39 @@ pub enum Endian {
234234 Big ,
235235}
236236
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+
237270/// Size of a type in bytes.
238271#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug , Encodable , Decodable ) ]
239272#[ derive( HashStable_Generic ) ]
0 commit comments