Skip to content

Commit

Permalink
refactor: Take ownership of builder instead
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo committed Aug 23, 2024
1 parent af57964 commit c6e51b3
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn fetch() -> Result<String> {
}

fn main() -> Result<()> {
let content = fetch.retry(&ExponentialBuilder::default()).call()?;
let content = fetch.retry(ExponentialBuilder::default()).call()?;
println!("fetch succeeded: {}", content);

Ok(())
Expand All @@ -52,7 +52,7 @@ async fn fetch() -> Result<String> {

#[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
2 changes: 1 addition & 1 deletion examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn fetch() -> Result<String> {

#[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(())
Expand Down
2 changes: 1 addition & 1 deletion examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn fetch() -> Result<String> {
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(())
Expand Down
2 changes: 1 addition & 1 deletion examples/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> anyhow::Result<()> {
let var = 42;
// `f` can use input variables
let f = || Ok::<u32, anyhow::Error>(var);
let result = f.retry(&backon::ExponentialBuilder::default()).call()?;
let result = f.retry(backon::ExponentialBuilder::default()).call()?;
println!("var = {result}");

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/backoff/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
6 changes: 3 additions & 3 deletions src/backoff/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/backoff/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/backoff/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 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 @@ -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);
Expand Down Expand Up @@ -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);
/// },
Expand Down Expand Up @@ -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());
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/blocking_retry_with_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ pub trait BlockingRetryableWithContext<
>
{
/// Generate a new retry
fn retry(self, builder: &B) -> BlockingRetryWithContext<B::Backoff, T, E, Ctx, F>;
fn retry(self, builder: B) -> BlockingRetryWithContext<B::Backoff, T, E, Ctx, F>;
}

impl<B, T, E, Ctx, F> BlockingRetryableWithContext<B, T, E, Ctx, F> for F
where
B: BackoffBuilder,
F: FnMut(Ctx) -> (Ctx, Result<T, E>),
{
fn retry(self, builder: &B) -> BlockingRetryWithContext<B::Backoff, T, E, Ctx, F> {
fn retry(self, builder: B) -> BlockingRetryWithContext<B::Backoff, T, E, Ctx, F> {
BlockingRetryWithContext::new(self, builder.build())
}
}
Expand Down Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand All @@ -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?;
//!
Expand All @@ -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?;
//!
Expand Down Expand Up @@ -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?;
//!
Expand Down Expand Up @@ -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")
Expand Down
20 changes: 10 additions & 10 deletions src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 @@ -60,7 +60,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 @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
/// })
Expand Down Expand Up @@ -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());
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
Loading

0 comments on commit c6e51b3

Please sign in to comment.