From c6e51b324ca9b4e2f18c632dbc5b05c0b09717f3 Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Fri, 23 Aug 2024 18:04:10 +0800 Subject: [PATCH] refactor: Take ownership of builder instead Signed-off-by: Xuanwo --- README.md | 4 ++-- examples/async.rs | 2 +- examples/blocking.rs | 2 +- examples/closure.rs | 2 +- examples/sqlx.rs | 2 +- src/backoff/api.rs | 4 ++-- src/backoff/constant.rs | 6 +++--- src/backoff/exponential.rs | 6 +++--- src/backoff/fibonacci.rs | 6 +++--- src/blocking_retry.rs | 18 +++++++++--------- src/blocking_retry_with_context.rs | 6 +++--- src/lib.rs | 10 +++++----- src/retry.rs | 20 ++++++++++---------- src/retry_with_context.rs | 12 ++++++------ 14 files changed, 50 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 3f3d4f7..a116aba 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ fn fetch() -> Result { } fn main() -> Result<()> { - let content = fetch.retry(&ExponentialBuilder::default()).call()?; + let content = fetch.retry(ExponentialBuilder::default()).call()?; println!("fetch succeeded: {}", content); Ok(()) @@ -52,7 +52,7 @@ async fn fetch() -> Result { #[tokio::main] async fn main() -> Result<()> { - let content = fetch.retry(&ExponentialBuilder::default()).await?; + let content = fetch.retry(ExponentialBuilder::default()).await?; println!("fetch succeeded: {}", content); Ok(()) diff --git a/examples/async.rs b/examples/async.rs index 3690c04..1a62f8e 100644 --- a/examples/async.rs +++ b/examples/async.rs @@ -16,7 +16,7 @@ async fn fetch() -> Result { #[tokio::main(flavor = "current_thread")] async fn main() -> Result<()> { - let _ = fetch.retry(&ExponentialBuilder::default()).await?; + let _ = fetch.retry(ExponentialBuilder::default()).await?; println!("fetch succeeded"); Ok(()) diff --git a/examples/blocking.rs b/examples/blocking.rs index b3b8850..887f0c7 100644 --- a/examples/blocking.rs +++ b/examples/blocking.rs @@ -11,7 +11,7 @@ fn fetch() -> Result { fn main() -> Result<()> { use backon::BlockingRetryable; - let content = fetch.retry(&backon::ExponentialBuilder::default()).call()?; + let content = fetch.retry(backon::ExponentialBuilder::default()).call()?; println!("fetch succeeded: {}", content); Ok(()) diff --git a/examples/closure.rs b/examples/closure.rs index b6e0e1f..d865330 100644 --- a/examples/closure.rs +++ b/examples/closure.rs @@ -8,7 +8,7 @@ fn main() -> anyhow::Result<()> { let var = 42; // `f` can use input variables let f = || Ok::(var); - let result = f.retry(&backon::ExponentialBuilder::default()).call()?; + let result = f.retry(backon::ExponentialBuilder::default()).call()?; println!("var = {result}"); Ok(()) diff --git a/examples/sqlx.rs b/examples/sqlx.rs index 7162db9..cafa140 100644 --- a/examples/sqlx.rs +++ b/examples/sqlx.rs @@ -12,7 +12,7 @@ async fn main() -> anyhow::Result<()> { .await?; let row: (i64,) = (|| sqlx::query_as("SELECT $1").bind(150_i64).fetch_one(&pool)) - .retry(&backon::ExponentialBuilder::default()) + .retry(backon::ExponentialBuilder::default()) .await?; assert_eq!(row.0, 150); diff --git a/src/backoff/api.rs b/src/backoff/api.rs index 2a34abc..5c008b0 100644 --- a/src/backoff/api.rs +++ b/src/backoff/api.rs @@ -2,12 +2,12 @@ use std::fmt::Debug; use std::time::Duration; /// BackoffBuilder is used to build a new backoff. -pub trait BackoffBuilder: Clone + Debug + Send + Sync + Unpin { +pub trait BackoffBuilder: Debug + Send + Sync + Unpin { /// The associated backoff that returned by this builder. type Backoff: Backoff; /// Builder a new backoff via builder. - fn build(&self) -> Self::Backoff; + fn build(self) -> Self::Backoff; } /// Backoff is an [`Iterator`] that returns [`Duration`]. diff --git a/src/backoff/constant.rs b/src/backoff/constant.rs index 6fa3583..d02a7c8 100644 --- a/src/backoff/constant.rs +++ b/src/backoff/constant.rs @@ -25,13 +25,13 @@ use crate::backoff::BackoffBuilder; /// /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<()> { -/// let content = fetch.retry(&ConstantBuilder::default()).await?; +/// let content = fetch.retry(ConstantBuilder::default()).await?; /// println!("fetch succeeded: {}", content); /// /// Ok(()) /// } /// ``` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct ConstantBuilder { delay: Duration, max_times: Option, @@ -71,7 +71,7 @@ impl ConstantBuilder { impl BackoffBuilder for ConstantBuilder { type Backoff = ConstantBackoff; - fn build(&self) -> Self::Backoff { + fn build(self) -> Self::Backoff { ConstantBackoff { delay: self.delay, max_times: self.max_times, diff --git a/src/backoff/exponential.rs b/src/backoff/exponential.rs index b2c2cee..ada0330 100644 --- a/src/backoff/exponential.rs +++ b/src/backoff/exponential.rs @@ -28,13 +28,13 @@ use crate::backoff::BackoffBuilder; /// /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<()> { -/// let content = fetch.retry(&ExponentialBuilder::default()).await?; +/// let content = fetch.retry(ExponentialBuilder::default()).await?; /// println!("fetch succeeded: {}", content); /// /// Ok(()) /// } /// ``` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct ExponentialBuilder { jitter: bool, factor: f32, @@ -103,7 +103,7 @@ impl ExponentialBuilder { impl BackoffBuilder for ExponentialBuilder { type Backoff = ExponentialBackoff; - fn build(&self) -> Self::Backoff { + fn build(self) -> Self::Backoff { ExponentialBackoff { jitter: self.jitter, factor: self.factor, diff --git a/src/backoff/fibonacci.rs b/src/backoff/fibonacci.rs index 64a9b69..300e023 100644 --- a/src/backoff/fibonacci.rs +++ b/src/backoff/fibonacci.rs @@ -27,13 +27,13 @@ use crate::backoff::BackoffBuilder; /// /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<()> { -/// let content = fetch.retry(&FibonacciBuilder::default()).await?; +/// let content = fetch.retry(FibonacciBuilder::default()).await?; /// println!("fetch succeeded: {}", content); /// /// Ok(()) /// } /// ``` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct FibonacciBuilder { jitter: bool, min_delay: Duration, @@ -88,7 +88,7 @@ impl FibonacciBuilder { impl BackoffBuilder for FibonacciBuilder { type Backoff = FibonacciBackoff; - fn build(&self) -> Self::Backoff { + fn build(self) -> Self::Backoff { FibonacciBackoff { jitter: self.jitter, min_delay: self.min_delay, diff --git a/src/blocking_retry.rs b/src/blocking_retry.rs index c515dc4..e545cf8 100644 --- a/src/blocking_retry.rs +++ b/src/blocking_retry.rs @@ -36,7 +36,7 @@ use crate::Backoff; /// } /// /// fn main() -> Result<()> { -/// let content = fetch.retry(&ExponentialBuilder::default()).call()?; +/// let content = fetch.retry(ExponentialBuilder::default()).call()?; /// println!("fetch succeeded: {}", content); /// /// Ok(()) @@ -44,7 +44,7 @@ use crate::Backoff; /// ``` pub trait BlockingRetryable Result> { /// Generate a new retry - fn retry(self, builder: &B) -> BlockingRetry; + fn retry(self, builder: B) -> BlockingRetry; } impl BlockingRetryable for F @@ -52,7 +52,7 @@ where B: BackoffBuilder, F: FnMut() -> Result, { - fn retry(self, builder: &B) -> BlockingRetry { + fn retry(self, builder: B) -> BlockingRetry { BlockingRetry::new(self, builder.build()) } } @@ -111,7 +111,7 @@ where /// /// fn main() -> Result<()> { /// let retry = fetch - /// .retry(&ExponentialBuilder::default()) + /// .retry(ExponentialBuilder::default()) /// .when(|e| e.to_string() == "EOF"); /// let content = retry.call()?; /// println!("fetch succeeded: {}", content); @@ -146,7 +146,7 @@ where /// } /// /// fn main() -> Result<()> { - /// let retry = fetch.retry(&ExponentialBuilder::default()).notify( + /// let retry = fetch.retry(ExponentialBuilder::default()).notify( /// |err: &anyhow::Error, dur: Duration| { /// println!("retrying error {:?} with sleeping {:?}", err, dur); /// }, @@ -208,7 +208,7 @@ mod tests { #[test] fn test_retry() -> anyhow::Result<()> { let result = always_error - .retry(&ExponentialBuilder::default().with_min_delay(Duration::from_millis(1))) + .retry(ExponentialBuilder::default().with_min_delay(Duration::from_millis(1))) .call(); assert!(result.is_err()); @@ -228,7 +228,7 @@ mod tests { let backoff = ExponentialBuilder::default().with_min_delay(Duration::from_millis(1)); let result = f - .retry(&backoff) + .retry(backoff) // Only retry If error message is `retryable` .when(|e| e.to_string() == "retryable") .call(); @@ -254,7 +254,7 @@ mod tests { let backoff = ExponentialBuilder::default().with_min_delay(Duration::from_millis(1)); let result = f - .retry(&backoff) + .retry(backoff) // Only retry If error message is `retryable` .when(|e| e.to_string() == "retryable") .call(); @@ -276,7 +276,7 @@ mod tests { let backoff = ExponentialBuilder::default().with_min_delay(Duration::from_millis(1)); let result = f - .retry(&backoff) + .retry(backoff) .when(|_| { calls_retryable.push(()); true diff --git a/src/blocking_retry_with_context.rs b/src/blocking_retry_with_context.rs index 9dabc3c..458cc23 100644 --- a/src/blocking_retry_with_context.rs +++ b/src/blocking_retry_with_context.rs @@ -14,7 +14,7 @@ pub trait BlockingRetryableWithContext< > { /// Generate a new retry - fn retry(self, builder: &B) -> BlockingRetryWithContext; + fn retry(self, builder: B) -> BlockingRetryWithContext; } impl BlockingRetryableWithContext for F @@ -22,7 +22,7 @@ where B: BackoffBuilder, F: FnMut(Ctx) -> (Ctx, Result), { - fn retry(self, builder: &B) -> BlockingRetryWithContext { + fn retry(self, builder: B) -> BlockingRetryWithContext { BlockingRetryWithContext::new(self, builder.build()) } } @@ -177,7 +177,7 @@ mod tests { (v, res) } } - .retry(&backoff) + .retry(backoff) .context(test) // Only retry If error message is `retryable` .when(|e| e.to_string() == "retryable") diff --git a/src/lib.rs b/src/lib.rs index 93116cc..1e14d09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ //! //! #[tokio::main(flavor = "current_thread")] //! async fn main() -> Result<()> { -//! let content = fetch.retry(&ExponentialBuilder::default()).await?; +//! let content = fetch.retry(ExponentialBuilder::default()).await?; //! //! println!("fetch succeeded: {}", content); //! Ok(()) @@ -60,7 +60,7 @@ //! #[tokio::main(flavor = "current_thread")] //! async fn main() -> Result<()> { //! let content = fetch -//! .retry(&ExponentialBuilder::default()) +//! .retry(ExponentialBuilder::default()) //! .when(|e| e.to_string() == "retryable") //! .await?; //! @@ -83,7 +83,7 @@ //! #[tokio::main(flavor = "current_thread")] //! async fn main() -> Result<()> { //! let content = (|| async { fetch("https://www.rust-lang.org").await }) -//! .retry(&ExponentialBuilder::default()) +//! .retry(ExponentialBuilder::default()) //! .when(|e| e.to_string() == "retryable") //! .await?; //! @@ -111,7 +111,7 @@ //! async fn main() -> Result<()> { //! let test = Test; //! let content = (|| async { test.fetch("https://www.rust-lang.org").await }) -//! .retry(&ExponentialBuilder::default()) +//! .retry(ExponentialBuilder::default()) //! .when(|e| e.to_string() == "retryable") //! .await?; //! @@ -144,7 +144,7 @@ //! // Return input context back. //! (v, res) //! }) -//! .retry(&ExponentialBuilder::default()) +//! .retry(ExponentialBuilder::default()) //! // Passing context in. //! .context(test) //! .when(|e| e.to_string() == "retryable") diff --git a/src/retry.rs b/src/retry.rs index 5004be1..202f878 100644 --- a/src/retry.rs +++ b/src/retry.rs @@ -51,7 +51,7 @@ pub trait Retryable< > { /// Generate a new retry - fn retry(self, builder: &B) -> Retry; + fn retry(self, builder: B) -> Retry; } impl Retryable for FutureFn @@ -60,7 +60,7 @@ where Fut: Future>, FutureFn: FnMut() -> Fut, { - fn retry(self, builder: &B) -> Retry { + fn retry(self, builder: B) -> Retry { Retry::new(self, builder.build()) } } @@ -137,7 +137,7 @@ where /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<()> { /// let content = fetch - /// .retry(&ExponentialBuilder::default()) + /// .retry(ExponentialBuilder::default()) /// .sleep(|_| ready(())) /// .await?; /// println!("fetch succeeded: {}", content); @@ -177,7 +177,7 @@ where /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<()> { /// let content = fetch - /// .retry(&ExponentialBuilder::default()) + /// .retry(ExponentialBuilder::default()) /// .when(|e| e.to_string() == "EOF") /// .await?; /// println!("fetch succeeded: {}", content); @@ -222,7 +222,7 @@ where /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<()> { /// let content = fetch - /// .retry(&ExponentialBuilder::default()) + /// .retry(ExponentialBuilder::default()) /// .notify(|err: &anyhow::Error, dur: Duration| { /// println!("retrying error {:?} with sleeping {:?}", err, dur); /// }) @@ -349,7 +349,7 @@ mod tests { #[test] async fn test_retry() -> anyhow::Result<()> { let result = always_error - .retry(&ExponentialBuilder::default().with_min_delay(Duration::from_millis(1))) + .retry(ExponentialBuilder::default().with_min_delay(Duration::from_millis(1))) .await; assert!(result.is_err()); @@ -360,7 +360,7 @@ mod tests { #[test] async fn test_retry_with_sleep() -> anyhow::Result<()> { let result = always_error - .retry(&ExponentialBuilder::default().with_min_delay(Duration::from_millis(1))) + .retry(ExponentialBuilder::default().with_min_delay(Duration::from_millis(1))) .sleep(|_| ready(())) .await; @@ -381,7 +381,7 @@ mod tests { let backoff = ExponentialBuilder::default().with_min_delay(Duration::from_millis(1)); let result = f - .retry(&backoff) + .retry(backoff) // Only retry If error message is `retryable` .when(|e| e.to_string() == "retryable") .await; @@ -406,7 +406,7 @@ mod tests { let backoff = ExponentialBuilder::default().with_min_delay(Duration::from_millis(1)); let result = f - .retry(&backoff) + .retry(backoff) // Only retry If error message is `retryable` .when(|e| e.to_string() == "retryable") .await; @@ -428,7 +428,7 @@ mod tests { let backoff = ExponentialBuilder::default().with_min_delay(Duration::from_millis(1)); let result = f - .retry(&backoff) + .retry(backoff) .when(|_| { calls_retryable.push(()); true diff --git a/src/retry_with_context.rs b/src/retry_with_context.rs index 6f6889e..66206c4 100644 --- a/src/retry_with_context.rs +++ b/src/retry_with_context.rs @@ -67,7 +67,7 @@ use crate::Sleeper; /// (v, res) /// } /// } -/// .retry(&ExponentialBuilder::default()) +/// .retry(ExponentialBuilder::default()) /// .context(test) /// .await; /// @@ -84,7 +84,7 @@ pub trait RetryableWithContext< > { /// Generate a new retry - fn retry(self, builder: &B) -> RetryWithContext; + fn retry(self, builder: B) -> RetryWithContext; } impl RetryableWithContext for FutureFn @@ -93,7 +93,7 @@ where Fut: Future)>, FutureFn: FnMut(Ctx) -> Fut, { - fn retry(self, builder: &B) -> RetryWithContext { + fn retry(self, builder: B) -> RetryWithContext { RetryWithContext::new(self, builder.build()) } } @@ -210,7 +210,7 @@ where /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<()> { /// let content = fetch - /// .retry(&ExponentialBuilder::default()) + /// .retry(ExponentialBuilder::default()) /// .when(|e| e.to_string() == "EOF") /// .await?; /// println!("fetch succeeded: {}", content); @@ -255,7 +255,7 @@ where /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<()> { /// let content = fetch - /// .retry(&ExponentialBuilder::default()) + /// .retry(ExponentialBuilder::default()) /// .notify(|err: &anyhow::Error, dur: Duration| { /// println!("retrying error {:?} with sleeping {:?}", err, dur); /// }) @@ -400,7 +400,7 @@ mod tests { (v, res) } } - .retry(&backoff) + .retry(backoff) .context(test) // Only retry If error message is `retryable` .when(|e| e.to_string() == "retryable")