From 71933f0d33638d16ae7977e1c0181d9de30a9948 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 3 Aug 2022 18:19:33 +0200 Subject: [PATCH] feat(abi): add missing str abi trait impls (#1554) --- ethers-core/src/abi/codec.rs | 13 +++++++++++++ ethers-core/src/abi/mod.rs | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/ethers-core/src/abi/codec.rs b/ethers-core/src/abi/codec.rs index a953eef0..45b0ad2d 100644 --- a/ethers-core/src/abi/codec.rs +++ b/ethers-core/src/abi/codec.rs @@ -77,6 +77,12 @@ impl_abi_codec!( i128 ); +impl<'a> AbiEncode for &'a str { + fn encode(self) -> Vec { + self.to_string().encode() + } +} + impl AbiEncode for [T; N] { fn encode(self) -> Vec { 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()); + } } diff --git a/ethers-core/src/abi/mod.rs b/ethers-core/src/abi/mod.rs index da12946b..bf31b833 100644 --- a/ethers-core/src/abi/mod.rs +++ b/ethers-core/src/abi/mod.rs @@ -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]