@@ -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,15 +155,12 @@ 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. target_endian {
158+ if dl. endian != target. 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. target_endian
161+ architecture is {}-endian, while \" target-endian\" is `{}`",
162+ dl. endian. name( ) ,
163+ target. target_endian. name( )
164164 ) ) ;
165165 }
166166
@@ -234,6 +234,47 @@ pub enum Endian {
234234 Big ,
235235}
236236
237+ impl Endian {
238+ pub fn name ( & self ) -> & str {
239+ match self {
240+ Self :: Little => "little" ,
241+ Self :: Big => "big" ,
242+ }
243+ }
244+
245+ pub fn is_big ( & self ) -> bool {
246+ Self :: Big == * self
247+ }
248+
249+ pub fn is_little ( & self ) -> bool {
250+ Self :: Little == * self
251+ }
252+ }
253+
254+ impl fmt:: Debug for Endian {
255+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
256+ f. write_str ( self . name ( ) )
257+ }
258+ }
259+
260+ impl FromStr for Endian {
261+ type Err = String ;
262+
263+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
264+ match s {
265+ "little" => Ok ( Self :: Little ) ,
266+ "big" => Ok ( Self :: Big ) ,
267+ _ => Err ( format ! ( r#"unknown endian: "{}""# , s) ) ,
268+ }
269+ }
270+ }
271+
272+ impl ToJson for Endian {
273+ fn to_json ( & self ) -> Json {
274+ Json :: String ( self . name ( ) . to_owned ( ) )
275+ }
276+ }
277+
237278/// Size of a type in bytes.
238279#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug , Encodable , Decodable ) ]
239280#[ derive( HashStable_Generic ) ]
0 commit comments