feat(abi): add missing str abi trait impls (#1554)

This commit is contained in:
Matthias Seitz 2022-08-03 18:19:33 +02:00 committed by GitHub
parent 8026904c3d
commit 71933f0d33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -77,6 +77,12 @@ impl_abi_codec!(
i128
);
impl<'a> AbiEncode for &'a str {
fn encode(self) -> Vec<u8> {
self.to_string().encode()
}
}
impl<T: TokenizableItem + Clone, const N: usize> AbiEncode for [T; N] {
fn encode(self) -> Vec<u8> {
let token = self.into_token();
@ -266,4 +272,11 @@ mod tests {
let tuple: Vec<(Address, u8, Vec<[u8; 4]>)> = vec![(Address::random(), 0, nested)];
assert_codec(tuple);
}
#[test]
fn str_encoding() {
let value = "str value";
let encoded = value.encode();
assert_eq!(value, String::decode(encoded).unwrap());
}
}

View File

@ -163,6 +163,7 @@ impl_abi_type!(
Address => Address,
bool => Bool,
String => String,
str => String,
H256 => FixedBytes(32),
H512 => FixedBytes(64),
U64 => Uint(64),
@ -180,6 +181,14 @@ impl_abi_type!(
I256 => Int(256)
);
impl<'a> AbiType for &'a str {
fn param_type() -> ParamType {
ParamType::String
}
}
impl<'a> AbiArrayType for &'a str {}
macro_rules! impl_abi_type_tuple {
($num: expr, $( $ty: ident),+) => {
impl<$($ty, )+> AbiType for ($($ty,)+) where
@ -310,6 +319,9 @@ mod tests {
ParamType::FixedArray(Box::new(ParamType::Uint(16)), 32),
<[u16; 32]>::param_type()
);
assert_eq!(ParamType::String, str::param_type());
assert_eq!(ParamType::String, <&str>::param_type());
}
#[test]