Skip to content

Commit

Permalink
Remove use for async-trait and improve docs and feature usage
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Nov 6, 2024
1 parent ca6cfe8 commit 15a2592
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ reqwest = { version = "0.11", features = ["json"], default-features = false, op

# default async runtime
tokio = { version = "1.38", features = ["time"], optional = true }
async-trait = { version = "0.1", optional = true }

[dev-dependencies]
serde_json = "1.0"
Expand All @@ -42,8 +41,8 @@ blocking-https-rustls = ["blocking", "minreq/https-rustls"]
blocking-https-native = ["blocking", "minreq/https-native"]
blocking-https-bundled = ["blocking", "minreq/https-bundled"]

tokio = ["dep:tokio", "async"]
async = ["reqwest", "reqwest/socks", "async-trait"]
tokio = ["dep:tokio"]
async = ["reqwest", "reqwest/socks"]
async-https = ["async", "reqwest/default-tls"]
async-https-native = ["async", "reqwest/native-tls"]
async-https-rustls = ["async", "reqwest/rustls-tls"]
Expand Down
20 changes: 8 additions & 12 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

//! Esplora by way of `reqwest` HTTP client.
use async_trait::async_trait;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::str::FromStr;
use std::time::Duration;

use bitcoin::consensus::{deserialize, serialize, Decodable, Encodable};
use bitcoin::hashes::{sha256, Hash};
Expand Down Expand Up @@ -43,6 +41,7 @@ pub struct AsyncClient<S = DefaultSleeper> {
/// Number of times to retry a request
max_retries: usize,

/// Marker for the type of sleeper used
marker: PhantomData<S>,
}

Expand Down Expand Up @@ -453,22 +452,19 @@ fn is_status_retryable(status: reqwest::StatusCode) -> bool {
RETRYABLE_ERROR_CODES.contains(&status.as_u16())
}

/// Trait that defines the ability to sleep within an async runtime
#[async_trait]
pub trait Sleeper {
/// Wait until the specified `duration` has elapsed
async fn sleep(duration: Duration);
pub trait Sleeper: 'static {
type Sleep: std::future::Future<Output = ()>;
fn sleep(dur: std::time::Duration) -> Self::Sleep;
}

/// Default sleeper. Note this may only be used as a [`Sleeper`] implementation
/// if the "tokio" feature is enabled.
#[derive(Debug, Clone, Copy)]
pub struct DefaultSleeper;

#[cfg(any(test, feature = "tokio"))]
#[async_trait]
impl Sleeper for DefaultSleeper {
async fn sleep(duration: Duration) {
tokio::time::sleep(duration).await;
type Sleep = tokio::time::Sleep;

fn sleep(dur: std::time::Duration) -> Self::Sleep {
tokio::time::sleep(dur)
}
}
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Builder {
}

/// Build an asynchronous client from builder
#[cfg(feature = "tokio")]
#[cfg(all(feature = "async", feature = "tokio"))]
pub fn build_async(self) -> Result<AsyncClient, Error> {
AsyncClient::from_builder(self)
}
Expand Down Expand Up @@ -271,7 +271,6 @@ mod test {
bitcoind::bitcoincore_rpc::json::AddressType, bitcoind::bitcoincore_rpc::RpcApi,
electrum_client::ElectrumApi,
},
r#async::DefaultSleeper,
std::time::Duration,
tokio::sync::OnceCell,
};
Expand Down Expand Up @@ -330,8 +329,13 @@ mod test {
let blocking_client = builder.build_blocking();

let builder_async = Builder::new(&format!("http://{}", esplora_url));

#[cfg(feature = "tokio")]
let async_client = builder_async.build_async().unwrap();

#[cfg(not(feature = "tokio"))]
let async_client = builder_async
.build_async_with_sleeper::<DefaultSleeper>()
.build_async_with_sleeper::<r#async::DefaultSleeper>()
.unwrap();

(blocking_client, async_client)
Expand Down Expand Up @@ -1005,7 +1009,7 @@ mod test {
assert_eq!(tx, tx_async);
}

#[cfg(feature = "tokio")]
#[cfg(all(feature = "async", feature = "tokio"))]
#[test]
fn use_builder_with_tokio_as_normal() {
let builder = Builder::new("https://blockstream.info/testnet/api");
Expand Down

0 comments on commit 15a2592

Please sign in to comment.