@@ -209,7 +209,7 @@ pub enum TargetDataLayoutErrors<'a> {
209
209
InvalidAddressSpace { addr_space : & ' a str , cause : & ' a str , err : ParseIntError } ,
210
210
InvalidBits { kind : & ' a str , bit : & ' a str , cause : & ' a str , err : ParseIntError } ,
211
211
MissingAlignment { cause : & ' a str } ,
212
- InvalidAlignment { cause : & ' a str , err : String } ,
212
+ InvalidAlignment { cause : & ' a str , err : AlignFromBytesError } ,
213
213
InconsistentTargetArchitecture { dl : & ' a str , target : & ' a str } ,
214
214
InconsistentTargetPointerWidth { pointer_size : u64 , target : u32 } ,
215
215
InvalidBitsSize { err : String } ,
@@ -640,30 +640,65 @@ impl fmt::Debug for Align {
640
640
}
641
641
}
642
642
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
+
643
678
impl Align {
644
679
pub const ONE : Align = Align { pow2 : 0 } ;
645
680
pub const MAX : Align = Align { pow2 : 29 } ;
646
681
647
682
#[ inline]
648
- pub fn from_bits ( bits : u64 ) -> Result < Align , String > {
683
+ pub fn from_bits ( bits : u64 ) -> Result < Align , AlignFromBytesError > {
649
684
Align :: from_bytes ( Size :: from_bits ( bits) . bytes ( ) )
650
685
}
651
686
652
687
#[ inline]
653
- pub fn from_bytes ( align : u64 ) -> Result < Align , String > {
688
+ pub fn from_bytes ( align : u64 ) -> Result < Align , AlignFromBytesError > {
654
689
// Treat an alignment of 0 bytes like 1-byte alignment.
655
690
if align == 0 {
656
691
return Ok ( Align :: ONE ) ;
657
692
}
658
693
659
694
#[ 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)
662
697
}
663
698
664
699
#[ 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)
667
702
}
668
703
669
704
let tz = align. trailing_zeros ( ) ;
0 commit comments