Skip to content

Commit

Permalink
refactor: Retryable should accept a reference (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo committed Feb 8, 2023
1 parent f902488 commit fd445ba
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions src/blocking_retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ 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(())
/// }
/// ```
pub trait BlockingRetryable<B: BackoffBuilder, T, E, F: FnMut() -> Result<T, E>> {
/// Generate a new retry
fn retry(self, builder: B) -> BlockingRetry<B::Backoff, T, E, F>;
fn retry(self, builder: &B) -> BlockingRetry<B::Backoff, T, E, F>;
}

impl<B, T, E, F> BlockingRetryable<B, T, E, F> for F
where
B: BackoffBuilder,
F: FnMut() -> Result<T, E>,
{
fn retry(self, builder: B) -> BlockingRetry<B::Backoff, T, E, F> {
fn retry(self, builder: &B) -> BlockingRetry<B::Backoff, T, E, F> {
BlockingRetry::new(self, builder.build())
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
/// },
Expand Down Expand Up @@ -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());
Expand All @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion src/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand All @@ -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?;
//!
Expand Down
16 changes: 8 additions & 8 deletions src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand All @@ -69,7 +69,7 @@ pub trait Retryable<
>
{
/// Generate a new retry
fn retry(self, builder: B) -> Retry<B::Backoff, T, E, Fut, FutureFn>;
fn retry(self, builder: &B) -> Retry<B::Backoff, T, E, Fut, FutureFn>;
}

impl<B, T, E, Fut, FutureFn> Retryable<B, T, E, Fut, FutureFn> for FutureFn
Expand All @@ -78,7 +78,7 @@ where
Fut: Future<Output = Result<T, E>>,
FutureFn: FnMut() -> Fut,
{
fn retry(self, builder: B) -> Retry<B::Backoff, T, E, Fut, FutureFn> {
fn retry(self, builder: &B) -> Retry<B::Backoff, T, E, Fut, FutureFn> {
Retry::new(self, builder.build())
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
/// })
Expand Down Expand Up @@ -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());
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit fd445ba

Please sign in to comment.