Closed mehcode closed 1 year ago
This is from the rust-web3
crate
fn pad_to_bytes32(s: &str) -> Option<[u8; 32]> {
let s = s.as_bytes();
let s_len = s.len();
if s_len > 32 {
return None;
}
let mut result: [u8; 32] = Default::default();
result[..s_len].clone_from_slice(s);
Some(result)
}
Problem
It's very common/recommended to use the
bytes32
type when working with strings that are smaller than 32 bytes in Solidity for performance reasons. However,add_bytes32
only takes&[u8; 32]
which is frustrating to convert a string to in Rust.Solution
Define a new private trait
IntoBytes32
and change the signature ofadd_bytes32
toAnd implement
IntoBytes32
for:&[u8; 32]
String
&str
Alternatives
No response