Skip to content

Commit 6d385d8

Browse files
committed
client: Fix call_async for MSRV
Changed `send_fn` argument in `call_async` to take an owned `Value` in order to workaround the lifetime constraint in the absence of `AsyncFn`.
1 parent 80f7db3 commit 6d385d8

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

examples/async.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1717
// Create RPC client
1818
let client = bdk_bitcoind_client::Client::new();
1919

20-
// Implement send request function
21-
use jsonrpc::{Request, Response};
22-
let send_fn = async |request: Request| -> Result<Response, bitreq::Error> {
23-
bitreq::post(URL)
24-
.with_header(
25-
"Authorization",
26-
format!("Basic {}", BASE64.encode(cookie.as_bytes())),
27-
)
28-
.with_json(&request)?
29-
.send_async()
30-
.await?
31-
.json::<jsonrpc::Response>()
20+
// Implement async send request function
21+
let auth_header = format!("Basic {}", BASE64.encode(cookie.as_bytes()));
22+
let send_fn = move |request_value: serde_json::Value| {
23+
let auth_header = auth_header.clone();
24+
async move {
25+
bitreq::post(URL)
26+
.with_header("Authorization", auth_header)
27+
.with_json(&request_value)?
28+
.send_async()
29+
.await?
30+
.json::<jsonrpc::Response>()
31+
}
3232
};
3333

3434
// Execute the RPC
3535
let blockchain_info = client
36-
.call_async::<v29::GetBlockchainInfo, _>(Rpc::GetBlockchainInfo, &[], send_fn)
36+
.call_async::<v29::GetBlockchainInfo, _, _, _>(Rpc::GetBlockchainInfo, &[], send_fn)
3737
.await?
3838
.into_model()?;
3939

src/client.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::future::Future;
12
use core::sync::atomic::{AtomicUsize, Ordering};
23

34
use jsonrpc::serde_json;
@@ -61,15 +62,17 @@ impl Client {
6162
}
6263

6364
/// Execute the RPC asynchronously.
64-
pub async fn call_async<T, E>(
65+
pub async fn call_async<T, E, F, Fut>(
6566
&self,
6667
rpc: impl RpcMethod,
6768
params: &[Value],
68-
send_fn: impl AsyncFn(Request) -> Result<Response, E>,
69+
send_fn: F,
6970
) -> Result<T, Error>
7071
where
7172
T: for<'de> Deserialize<'de>,
7273
E: std::error::Error + Send + Sync + 'static,
74+
F: Fn(Value) -> Fut,
75+
Fut: Future<Output = Result<Response, E>>,
7376
{
7477
let method = rpc.method();
7578
let raw_value = if params.is_empty() {
@@ -79,7 +82,8 @@ impl Client {
7982
};
8083
let request = self.request(&method, raw_value.as_deref());
8184
let request_id = request.id.clone();
82-
let response = send_fn(request).await.map_err(Error::transport)?;
85+
let value = serde_json::to_value(request)?;
86+
let response = send_fn(value).await.map_err(Error::transport)?;
8387
if response.id != request_id {
8488
return Err(Error::NonceMismatch);
8589
}

0 commit comments

Comments
 (0)