From 0f01dce6266b409fb8724f61584f74359e06da1a Mon Sep 17 00:00:00 2001 From: Praveen Perera Date: Tue, 15 Oct 2024 16:08:17 -0500 Subject: [PATCH] Rename `runtime` module and trait to `sleeper` --- src/async.rs | 14 +++++++------- src/lib.rs | 16 ++++++++-------- src/{runtime.rs => sleeper.rs} | 6 +++--- 3 files changed, 18 insertions(+), 18 deletions(-) rename src/{runtime.rs => sleeper.rs} (74%) diff --git a/src/async.rs b/src/async.rs index 764104a..d999bf2 100644 --- a/src/async.rs +++ b/src/async.rs @@ -27,14 +27,14 @@ use log::{debug, error, info, trace}; use reqwest::{header, Client, Response}; -use crate::runtime::{DefaultRuntime, Runtime}; +use crate::sleeper::{DefaultSleeper, Sleeper}; use crate::{ BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputStatus, Tx, TxStatus, BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES, }; #[derive(Debug, Clone)] -pub struct AsyncClient { +pub struct AsyncClient { /// The URL of the Esplora Server. url: String, /// The inner [`reqwest::Client`] to make HTTP requests. @@ -42,15 +42,15 @@ pub struct AsyncClient { /// Number of times to retry a request max_retries: usize, - runtime: PhantomData, + runtime: PhantomData, } -impl AsyncClient +impl AsyncClient where - R: Runtime, + S: Sleeper, { /// Build an async client from a builder - pub fn from_builder(builder: Builder) -> Result { + pub fn from_builder(builder: Builder) -> Result { let mut client_builder = Client::builder(); #[cfg(not(target_arch = "wasm32"))] @@ -441,7 +441,7 @@ where loop { match self.client.get(url).send().await? { resp if attempts < self.max_retries && is_status_retryable(resp.status()) => { - R::sleep(delay).await; + S::sleep(delay).await; attempts += 1; delay *= 2; } diff --git a/src/lib.rs b/src/lib.rs index c7f1783..c1e78a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,14 +78,14 @@ pub mod r#async; #[cfg(feature = "blocking")] pub mod blocking; #[cfg(feature = "async")] -pub mod runtime; +pub mod sleeper; pub use api::*; #[cfg(feature = "blocking")] pub use blocking::BlockingClient; #[cfg(feature = "async")] pub use r#async::AsyncClient; -use runtime::{DefaultRuntime, Runtime}; +use sleeper::{DefaultSleeper, Sleeper}; /// Response status codes for which the request may be retried. const RETRYABLE_ERROR_CODES: [u16; 3] = [ @@ -114,7 +114,7 @@ pub fn convert_fee_rate(target: usize, estimates: HashMap) -> Option { +pub struct Builder { /// The URL of the Esplora server. pub base_url: String, /// Optional URL of the proxy to use to make requests to the Esplora server @@ -137,7 +137,7 @@ pub struct Builder { /// Max retries pub max_retries: usize, /// Async runtime, trait must implement `sleep` function, default is `tokio` - pub runtime: PhantomData, + pub runtime: PhantomData, } impl Builder { @@ -160,9 +160,9 @@ impl Builder { } } -impl Builder +impl Builder where - R: Runtime, + S: Sleeper, { /// Instantiate a new builder, with a custom runtime #[cfg(feature = "async")] @@ -204,7 +204,7 @@ where // Build an asynchronous client from builder #[cfg(feature = "async")] - pub fn build_async(self) -> Result, Error> { + pub fn build_async(self) -> Result, Error> { AsyncClient::from_builder(self) } } @@ -1029,7 +1029,7 @@ mod test { struct TestRuntime; #[cfg(not(feature = "tokio"))] - impl Runtime for TestRuntime { + impl Sleeper for TestRuntime { async fn sleep(duration: Duration) { tokio::time::sleep(duration).await; } diff --git a/src/runtime.rs b/src/sleeper.rs similarity index 74% rename from src/runtime.rs rename to src/sleeper.rs index 113f62a..a2970e4 100644 --- a/src/runtime.rs +++ b/src/sleeper.rs @@ -1,13 +1,13 @@ use std::time::Duration; -pub trait Runtime { +pub trait Sleeper { fn sleep(duration: Duration) -> impl std::future::Future + Send; } -pub struct DefaultRuntime; +pub struct DefaultSleeper; #[cfg(feature = "tokio")] -impl Runtime for DefaultRuntime { +impl Sleeper for DefaultSleeper { async fn sleep(duration: Duration) { tokio::time::sleep(duration).await; }