From feafacc55f62caec8681cb1218e65578eb5bd4ad Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Thu, 3 Nov 2022 09:41:57 +0100 Subject: [PATCH 1/2] feat: Impl `IntoFuture` for `ContractCall` --- ethers-contract/src/call.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ethers-contract/src/call.rs b/ethers-contract/src/call.rs index 1f7534141..3e9d9e13c 100644 --- a/ethers-contract/src/call.rs +++ b/ethers-contract/src/call.rs @@ -14,7 +14,14 @@ use ethers_providers::{ Middleware, PendingTransaction, ProviderError, }; -use std::{borrow::Cow, fmt::Debug, future::Future, marker::PhantomData, sync::Arc}; +use std::{ + borrow::Cow, + fmt::Debug, + future::{Future, IntoFuture}, + marker::PhantomData, + pin::Pin, + sync::Arc, +}; use thiserror::Error as ThisError; @@ -211,3 +218,18 @@ where .map_err(ContractError::MiddlewareError) } } + +/// [`ContractCall`] can be turned into [`Future`] automatically with `.await`. +/// Defaults to calling [`ContractCall::call`]. +impl IntoFuture for ContractCall +where + M: Middleware + 'static, + D: Detokenize + 'static, +{ + type Output = Result>; + type IntoFuture = Pin>>; + + fn into_future(self) -> Self::IntoFuture { + Box::pin(async move { self.call().await }) + } +} From 7e6ea3adb5bb1a8a9b0941b68455b46a04534ca4 Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Thu, 3 Nov 2022 14:36:37 +0100 Subject: [PATCH 2/2] refactor: Move `'static` lifetime bound to `Self` --- ethers-contract/src/call.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ethers-contract/src/call.rs b/ethers-contract/src/call.rs index 3e9d9e13c..c9a8ab63e 100644 --- a/ethers-contract/src/call.rs +++ b/ethers-contract/src/call.rs @@ -223,8 +223,9 @@ where /// Defaults to calling [`ContractCall::call`]. impl IntoFuture for ContractCall where - M: Middleware + 'static, - D: Detokenize + 'static, + Self: 'static, + M: Middleware, + D: Detokenize, { type Output = Result>; type IntoFuture = Pin>>;