Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Impl IntoFuture for ContractCall #1826

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion ethers-contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -211,3 +218,19 @@ where
.map_err(ContractError::MiddlewareError)
}
}

/// [`ContractCall`] can be turned into [`Future`] automatically with `.await`.
/// Defaults to calling [`ContractCall::call`].
impl<M, D> IntoFuture for ContractCall<M, D>
where
Self: 'static,
M: Middleware,
D: Detokenize,
{
type Output = Result<D, ContractError<M>>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>;

fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.call().await })
}
Comment on lines +231 to +235
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not fond of excessive allocations, but it helps to hide an opaque future type.

Copy link
Collaborator

@prestwich prestwich Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GATs may help avoid this one in the future, but because this is a static lifetime future, it may be impossible to avoid right now

}