Skip to content

Commit

Permalink
feat: Allow users to use &XxxBuilder (#148)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored Sep 5, 2024
1 parent b3ca11e commit ac84451
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
11 changes: 10 additions & 1 deletion backon/src/backoff/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ impl<B: Backoff> BackoffBuilder for B {
#[cfg(test)]
mod tests {
use super::*;
use crate::{ConstantBuilder, ExponentialBuilder, FibonacciBuilder};

fn test_fn_builder(b: impl BackoffBuilder) {
let _ = b.build();
}

#[test]
fn test_backoff_builder() {
test_fn_builder([Duration::from_secs(1)].into_iter())
test_fn_builder([Duration::from_secs(1)].into_iter());

// Just for test if user can keep using &XxxBuilder.
#[allow(clippy::needless_borrows_for_generic_args)]
{
test_fn_builder(&ConstantBuilder::default());
test_fn_builder(&FibonacciBuilder::default());
test_fn_builder(&ExponentialBuilder::default());
}
}
}
8 changes: 8 additions & 0 deletions backon/src/backoff/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ impl BackoffBuilder for ConstantBuilder {
}
}

impl BackoffBuilder for &ConstantBuilder {
type Backoff = ConstantBackoff;

fn build(self) -> Self::Backoff {
(*self).build()
}
}

/// ConstantBackoff offers a consistent delay with a limited number of retries.
///
/// This backoff strategy is constructed by [`ConstantBuilder`].
Expand Down
8 changes: 8 additions & 0 deletions backon/src/backoff/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ impl BackoffBuilder for ExponentialBuilder {
}
}

impl BackoffBuilder for &ExponentialBuilder {
type Backoff = ExponentialBackoff;

fn build(self) -> Self::Backoff {
(*self).build()
}
}

/// ExponentialBackoff provides a delay with exponential retries.
///
/// This backoff strategy is constructed by [`ExponentialBuilder`].
Expand Down
8 changes: 8 additions & 0 deletions backon/src/backoff/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ impl BackoffBuilder for FibonacciBuilder {
}
}

impl BackoffBuilder for &FibonacciBuilder {
type Backoff = FibonacciBackoff;

fn build(self) -> Self::Backoff {
(*self).build()
}
}

/// FibonacciBackoff offers a delay with Fibonacci-based retries.
///
/// This backoff strategy is constructed by [`FibonacciBuilder`].
Expand Down

0 comments on commit ac84451

Please sign in to comment.