fix: use syn::Index for tuple access (#515)

* fix: use syn::Index for tuple access

* rustfmt
This commit is contained in:
Matthias Seitz 2021-10-16 18:45:42 +02:00 committed by GitHub
parent 956a87a924
commit c3528b78db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View File

@ -1,9 +1,9 @@
//! Helper functions for deriving `Display` //! Helper functions for deriving `Display`
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::{format_ident, quote}; use quote::quote;
use syn::spanned::Spanned as _; use syn::spanned::Spanned as _;
use syn::{parse::Error, Data, DeriveInput, Fields}; use syn::{parse::Error, Data, DeriveInput, Fields, Index};
use ethers_contract_abigen::ethers_core_crate; use ethers_contract_abigen::ethers_core_crate;
use ethers_core::abi::ParamType; use ethers_core::abi::ParamType;
@ -40,7 +40,11 @@ pub(crate) fn derive_eth_display_impl(input: DeriveInput) -> Result<TokenStream,
let ident = field let ident = field
.ident .ident
.clone() .clone()
.unwrap_or_else(|| format_ident!("{}", idx)); .map(|id| quote! {#id})
.unwrap_or_else(|| {
let idx = Index::from(idx);
quote! {#idx}
});
let tokens = if let Ok(param) = utils::find_parameter_type(&field.ty) { let tokens = if let Ok(param) = utils::find_parameter_type(&field.ty) {
match param { match param {
ParamType::Address | ParamType::Uint(_) | ParamType::Int(_) => { ParamType::Address | ParamType::Uint(_) | ParamType::Int(_) => {

View File

@ -212,9 +212,11 @@ impl<M: Middleware> Multicall<M> {
/// If more than the maximum number of supported calls are added. The maximum /// If more than the maximum number of supported calls are added. The maximum
/// limits is constrained due to tokenization/detokenization support for tuples /// limits is constrained due to tokenization/detokenization support for tuples
pub fn add_call<D: Detokenize>(&mut self, call: ContractCall<M, D>) -> &mut Self { pub fn add_call<D: Detokenize>(&mut self, call: ContractCall<M, D>) -> &mut Self {
if self.calls.len() >= 16 { assert!(
panic!("Cannot support more than {} calls", 16); !(self.calls.len() >= 16),
} "Cannot support more than {} calls",
16
);
match (call.tx.to(), call.tx.data()) { match (call.tx.to(), call.tx.data()) {
(Some(NameOrAddress::Address(target)), Some(data)) => { (Some(NameOrAddress::Address(target)), Some(data)) => {

View File

@ -371,3 +371,21 @@ fn eth_display_works() {
assert_eq!(val, format!("{}", item)); assert_eq!(val, format!("{}", item));
} }
#[test]
fn eth_display_works_for_human_readable() {
ethers_contract::abigen!(
HevmConsole,
r#"[
event log(string)
event log2(string x)
]"#,
);
let log = LogFilter("abc".to_string());
assert_eq!("abc".to_string(), format!("{}", log));
let log = Log2Filter {
x: "abc".to_string(),
};
assert_eq!("abc".to_string(), format!("{}", log));
}