Skip to content

Commit

Permalink
feature: some cool combinators on rpccall
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Jul 12, 2023
1 parent 76bef7f commit d654407
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 4 additions & 3 deletions crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ where
'a: 'b,
{
Box::pin(async move {
let res = self.estimate_gas(tx).await;

res.map(|gas| tx.set_gas(gas)).convert_err()
self.estimate_gas(tx)
.await
.map(|gas| tx.set_gas(gas))
.convert_err()
})
}
}
Expand Down
29 changes: 29 additions & 0 deletions crates/transports/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,32 @@ where
Poll::Ready(RpcResult::from(res))
}
}

impl<'a, B, T, Resp> RpcCall<B, T, Resp>
where
B: Borrow<T> + Unpin + 'a,
T: Connection + 'a,
Resp: RpcResp + 'a,
{
/// Map the result of the future to a new type, returning a new future.
pub fn map<U, F>(self, op: F) -> Pin<Box<dyn Future<Output = U> + 'a>>
where
F: FnOnce(<Self as Future>::Output) -> U + 'a,
{
Box::pin(async move { op(self.await) })
}

/// Map the result of the future to a new type, returning a new future.
pub fn map_ok<U, F>(
self,
op: F,
) -> Pin<Box<dyn Future<Output = RpcResult<U, TransportError>> + 'a>>
where
F: FnOnce(Resp) -> U + 'a,
{
Box::pin(async move {
let resp = self.await;
resp.map(op)
})
}
}

0 comments on commit d654407

Please sign in to comment.