1+ use revm_primitives:: { b256, Bytes , B256 } ;
12use std:: borrow:: Cow ;
23
34/// Right-pads the given slice at `offset` with zeroes until `LEN`.
45///
56/// Returns the first `LEN` bytes if it does not need padding.
6- #[ inline( always ) ]
7+ #[ inline]
78pub fn right_pad_with_offset < const LEN : usize > ( data : & [ u8 ] , offset : usize ) -> Cow < ' _ , [ u8 ; LEN ] > {
89 right_pad ( data. get ( offset..) . unwrap_or_default ( ) )
910}
1011
1112/// Right-pads the given slice at `offset` with zeroes until `len`.
1213///
1314/// Returns the first `len` bytes if it does not need padding.
14- #[ inline( always ) ]
15+ #[ inline]
1516pub fn right_pad_with_offset_vec ( data : & [ u8 ] , offset : usize , len : usize ) -> Cow < ' _ , [ u8 ] > {
1617 right_pad_vec ( data. get ( offset..) . unwrap_or_default ( ) , len)
1718}
1819
1920/// Right-pads the given slice with zeroes until `LEN`.
2021///
2122/// Returns the first `LEN` bytes if it does not need padding.
22- #[ inline( always ) ]
23+ #[ inline]
2324pub fn right_pad < const LEN : usize > ( data : & [ u8 ] ) -> Cow < ' _ , [ u8 ; LEN ] > {
2425 if let Some ( data) = data. get ( ..LEN ) {
2526 Cow :: Borrowed ( data. try_into ( ) . unwrap ( ) )
@@ -33,7 +34,7 @@ pub fn right_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
3334/// Right-pads the given slice with zeroes until `len`.
3435///
3536/// Returns the first `len` bytes if it does not need padding.
36- #[ inline( always ) ]
37+ #[ inline]
3738pub fn right_pad_vec ( data : & [ u8 ] , len : usize ) -> Cow < ' _ , [ u8 ] > {
3839 if let Some ( data) = data. get ( ..len) {
3940 Cow :: Borrowed ( data)
@@ -47,7 +48,7 @@ pub fn right_pad_vec(data: &[u8], len: usize) -> Cow<'_, [u8]> {
4748/// Left-pads the given slice with zeroes until `LEN`.
4849///
4950/// Returns the first `LEN` bytes if it does not need padding.
50- #[ inline( always ) ]
51+ #[ inline]
5152pub fn left_pad < const LEN : usize > ( data : & [ u8 ] ) -> Cow < ' _ , [ u8 ; LEN ] > {
5253 if let Some ( data) = data. get ( ..LEN ) {
5354 Cow :: Borrowed ( data. try_into ( ) . unwrap ( ) )
@@ -61,7 +62,7 @@ pub fn left_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
6162/// Left-pads the given slice with zeroes until `len`.
6263///
6364/// Returns the first `len` bytes if it does not need padding.
64- #[ inline( always ) ]
65+ #[ inline]
6566pub fn left_pad_vec ( data : & [ u8 ] , len : usize ) -> Cow < ' _ , [ u8 ] > {
6667 if let Some ( data) = data. get ( ..len) {
6768 Cow :: Borrowed ( data)
@@ -72,6 +73,28 @@ pub fn left_pad_vec(data: &[u8], len: usize) -> Cow<'_, [u8]> {
7273 }
7374}
7475
76+ /// Converts a boolean to a left-padded 32-byte `Bytes` value.
77+ ///
78+ /// This is optimized to not allocate at runtime by using 2 static arrays.
79+ #[ inline]
80+ pub const fn bool_to_bytes32 ( value : bool ) -> Bytes {
81+ Bytes :: from_static ( & bool_to_b256 ( value) . 0 )
82+ }
83+
84+ /// Converts a boolean to a left-padded `B256` value.
85+ ///
86+ /// This is optimized to not allocate at runtime by using 2 static arrays.
87+ #[ inline]
88+ pub const fn bool_to_b256 ( value : bool ) -> & ' static B256 {
89+ const TRUE : & B256 = & b256 ! ( "0000000000000000000000000000000000000000000000000000000000000001" ) ;
90+ const FALSE : & B256 = & b256 ! ( "0000000000000000000000000000000000000000000000000000000000000000" ) ;
91+ if value {
92+ TRUE
93+ } else {
94+ FALSE
95+ }
96+ }
97+
7598#[ cfg( test) ]
7699mod tests {
77100 use super :: * ;
@@ -140,4 +163,14 @@ mod tests {
140163 assert ! ( matches!( padded, Cow :: Borrowed ( _) ) ) ;
141164 assert_eq ! ( padded[ ..] , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) ;
142165 }
166+
167+ #[ test]
168+ fn bool2bytes ( ) {
169+ let f = bool_to_bytes32 ( false ) ;
170+ assert_eq ! ( f[ ..] , [ 0 ; 32 ] ) ;
171+ let t = bool_to_bytes32 ( true ) ;
172+ assert_eq ! ( t. len( ) , 32 ) ;
173+ assert_eq ! ( t[ ..31 ] , [ 0 ; 31 ] ) ;
174+ assert_eq ! ( t[ 31 ] , 1 ) ;
175+ }
143176}
0 commit comments