From f9f368d4732db6f794a3228efcff5f58873364cc Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Wed, 8 Feb 2023 15:39:41 +0800 Subject: [PATCH] refactor: Retryable should accept a reference Signed-off-by: Xuanwo --- src/blocking_retry.rs | 16 ++++++++-------- src/constant.rs | 2 +- src/exponential.rs | 2 +- src/lib.rs | 4 ++-- src/retry.rs | 16 ++++++++-------- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/blocking_retry.rs b/src/blocking_retry.rs index a08fac1..9f5038f 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()) } } @@ -97,7 +97,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); @@ -129,7 +129,7 @@ where /// /// #[tokio::main] /// async 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); /// }, @@ -187,7 +187,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()); @@ -207,7 +207,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(); @@ -233,7 +233,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(); diff --git a/src/constant.rs b/src/constant.rs index 1f8b524..181c8bf 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -25,7 +25,7 @@ use crate::backoff::BackoffBuilder; /// /// #[tokio::main] /// async fn main() -> Result<()> { -/// let content = fetch.retry(ConstantBuilder::default()).await?; +/// let content = fetch.retry(&ConstantBuilder::default()).await?; /// println!("fetch succeeded: {}", content); /// /// Ok(()) diff --git a/src/exponential.rs b/src/exponential.rs index 4081c7a..fae5dab 100644 --- a/src/exponential.rs +++ b/src/exponential.rs @@ -30,7 +30,7 @@ use crate::backoff::BackoffBuilder; /// /// #[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/src/lib.rs b/src/lib.rs index d55303e..2227edc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ //! //! #[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(()) @@ -55,7 +55,7 @@ //! #[tokio::main] //! async fn main() -> Result<()> { //! let content = fetch -//! .retry(ExponentialBuilder::default()) +//! .retry(&ExponentialBuilder::default()) //! .when(|e| e.to_string() == "retryable") //! .await?; //! diff --git a/src/retry.rs b/src/retry.rs index 0dce45f..529816b 100644 --- a/src/retry.rs +++ b/src/retry.rs @@ -54,7 +54,7 @@ use crate::Backoff; /// /// #[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(()) @@ -69,7 +69,7 @@ pub trait Retryable< > { /// Generate a new retry - fn retry(self, builder: B) -> Retry; + fn retry(self, builder: &B) -> Retry; } impl Retryable for FutureFn @@ -78,7 +78,7 @@ where Fut: Future>, FutureFn: FnMut() -> Fut, { - fn retry(self, builder: B) -> Retry { + fn retry(self, builder: &B) -> Retry { Retry::new(self, builder.build()) } } @@ -133,7 +133,7 @@ where /// #[tokio::main] /// async fn main() -> Result<()> { /// let content = fetch - /// .retry(ExponentialBuilder::default()) + /// .retry(&ExponentialBuilder::default()) /// .when(|e| e.to_string() == "EOF") /// .await?; /// println!("fetch succeeded: {}", content); @@ -169,7 +169,7 @@ where /// #[tokio::main] /// 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); /// }) @@ -270,7 +270,7 @@ mod tests { #[tokio::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()); @@ -290,7 +290,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; @@ -315,7 +315,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;