@@ -67,6 +67,44 @@ pub type PhysicalAddress = u64;
6767/// of target platform.
6868pub type VirtualAddress = u64 ;
6969
70+ /// ABI-compatible UEFI boolean.
71+ ///
72+ /// This is similar to a `bool`, but allows values other than 0 or 1 to be
73+ /// stored without it being undefined behavior.
74+ ///
75+ /// Any non-zero value is treated as logically `true`.
76+ #[ derive( Copy , Clone , Debug , Default , PartialEq , Ord , PartialOrd , Eq , Hash ) ]
77+ #[ repr( transparent) ]
78+ pub struct Boolean ( pub u8 ) ;
79+
80+ impl Boolean {
81+ /// [`Boolean`] representing `true`.
82+ pub const TRUE : Self = Self ( 1 ) ;
83+
84+ /// [`Boolean`] representing `false`.
85+ pub const FALSE : Self = Self ( 0 ) ;
86+ }
87+
88+ impl From < bool > for Boolean {
89+ fn from ( value : bool ) -> Self {
90+ match value {
91+ true => Self ( 1 ) ,
92+ false => Self ( 0 ) ,
93+ }
94+ }
95+ }
96+
97+ impl From < Boolean > for bool {
98+ #[ allow( clippy:: match_like_matches_macro) ]
99+ fn from ( value : Boolean ) -> Self {
100+ // We handle it as in C: Any bit-pattern != 0 equals true
101+ match value. 0 {
102+ 0 => false ,
103+ _ => true ,
104+ }
105+ }
106+ }
107+
70108/// An IPv4 internet protocol address.
71109#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
72110#[ repr( transparent) ]
@@ -135,3 +173,26 @@ impl Default for IpAddress {
135173#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
136174#[ repr( transparent) ]
137175pub struct MacAddress ( pub [ u8 ; 32 ] ) ;
176+
177+ #[ cfg( test) ]
178+ mod tests {
179+ use super :: * ;
180+
181+ #[ test]
182+ /// Test the properties promised in [0]. This also applies for the other
183+ /// architectures.
184+ ///
185+ /// [0] https://github.com/tianocore/edk2/blob/b0f43dd3fdec2363e3548ec31eb455dc1c4ac761/MdePkg/Include/X64/ProcessorBind.h#L192
186+ fn test_boolean_abi ( ) {
187+ assert_eq ! ( size_of:: <Boolean >( ) , 1 ) ;
188+ assert_eq ! ( Boolean :: from( true ) . 0 , 1 ) ;
189+ assert_eq ! ( Boolean :: from( false ) . 0 , 0 ) ;
190+ assert_eq ! ( Boolean :: TRUE . 0 , 1 ) ;
191+ assert_eq ! ( Boolean :: FALSE . 0 , 0 ) ;
192+ assert_eq ! ( bool :: from( Boolean ( 0b0 ) ) , false ) ;
193+ assert_eq ! ( bool :: from( Boolean ( 0b1 ) ) , true ) ;
194+ // We do it as in C: Every bit pattern not 0 is equal to true.
195+ assert_eq ! ( bool :: from( Boolean ( 0b11111110 ) ) , true ) ;
196+ assert_eq ! ( bool :: from( Boolean ( 0b11111111 ) ) , true ) ;
197+ }
198+ }
0 commit comments