Skip to content

Commit

Permalink
feat(abi): add missing str abi trait impls (gakonst#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Aug 3, 2022
1 parent 8026904 commit 71933f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ethers-core/src/abi/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
}
12 changes: 12 additions & 0 deletions ethers-core/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl_abi_type!(
Address => Address,
bool => Bool,
String => String,
str => String,
H256 => FixedBytes(32),
H512 => FixedBytes(64),
U64 => Uint(64),
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 71933f0

Please sign in to comment.