@@ -209,7 +209,7 @@ pub enum TargetDataLayoutErrors<'a> {
209209 InvalidAddressSpace { addr_space : & ' a str , cause : & ' a str , err : ParseIntError } ,
210210 InvalidBits { kind : & ' a str , bit : & ' a str , cause : & ' a str , err : ParseIntError } ,
211211 MissingAlignment { cause : & ' a str } ,
212- InvalidAlignment { cause : & ' a str , err : String } ,
212+ InvalidAlignment { cause : & ' a str , err : AlignFromBytesError } ,
213213 InconsistentTargetArchitecture { dl : & ' a str , target : & ' a str } ,
214214 InconsistentTargetPointerWidth { pointer_size : u64 , target : u32 } ,
215215 InvalidBitsSize { err : String } ,
@@ -640,30 +640,65 @@ impl fmt::Debug for Align {
640640 }
641641}
642642
643+ #[ derive( Clone , Copy ) ]
644+ pub enum AlignFromBytesError {
645+ NotPowerOfTwo ( u64 ) ,
646+ TooLarge ( u64 ) ,
647+ }
648+
649+ impl AlignFromBytesError {
650+ pub fn diag_ident ( self ) -> & ' static str {
651+ match self {
652+ Self :: NotPowerOfTwo ( _) => "not_power_of_two" ,
653+ Self :: TooLarge ( _) => "too_large" ,
654+ }
655+ }
656+
657+ pub fn align ( self ) -> u64 {
658+ let ( Self :: NotPowerOfTwo ( align) | Self :: TooLarge ( align) ) = self ;
659+ align
660+ }
661+ }
662+
663+ impl fmt:: Debug for AlignFromBytesError {
664+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
665+ fmt:: Display :: fmt ( self , f)
666+ }
667+ }
668+
669+ impl fmt:: Display for AlignFromBytesError {
670+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
671+ match self {
672+ AlignFromBytesError :: NotPowerOfTwo ( align) => write ! ( f, "`{align}` is not a power of 2" ) ,
673+ AlignFromBytesError :: TooLarge ( align) => write ! ( f, "`{align}` is too large" ) ,
674+ }
675+ }
676+ }
677+
643678impl Align {
644679 pub const ONE : Align = Align { pow2 : 0 } ;
645680 pub const MAX : Align = Align { pow2 : 29 } ;
646681
647682 #[ inline]
648- pub fn from_bits ( bits : u64 ) -> Result < Align , String > {
683+ pub fn from_bits ( bits : u64 ) -> Result < Align , AlignFromBytesError > {
649684 Align :: from_bytes ( Size :: from_bits ( bits) . bytes ( ) )
650685 }
651686
652687 #[ inline]
653- pub fn from_bytes ( align : u64 ) -> Result < Align , String > {
688+ pub fn from_bytes ( align : u64 ) -> Result < Align , AlignFromBytesError > {
654689 // Treat an alignment of 0 bytes like 1-byte alignment.
655690 if align == 0 {
656691 return Ok ( Align :: ONE ) ;
657692 }
658693
659694 #[ cold]
660- fn not_power_of_2 ( align : u64 ) -> String {
661- format ! ( "`{}` is not a power of 2" , align)
695+ fn not_power_of_2 ( align : u64 ) -> AlignFromBytesError {
696+ AlignFromBytesError :: NotPowerOfTwo ( align)
662697 }
663698
664699 #[ cold]
665- fn too_large ( align : u64 ) -> String {
666- format ! ( "`{}` is too large" , align)
700+ fn too_large ( align : u64 ) -> AlignFromBytesError {
701+ AlignFromBytesError :: TooLarge ( align)
667702 }
668703
669704 let tz = align. trailing_zeros ( ) ;
0 commit comments