From ac8445185929b6589265ceb5b47ddc1b96663d3b Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Thu, 5 Sep 2024 14:51:50 +0800 Subject: [PATCH] feat: Allow users to use &XxxBuilder (#148) Signed-off-by: Xuanwo --- backon/src/backoff/api.rs | 11 ++++++++++- backon/src/backoff/constant.rs | 8 ++++++++ backon/src/backoff/exponential.rs | 8 ++++++++ backon/src/backoff/fibonacci.rs | 8 ++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/backon/src/backoff/api.rs b/backon/src/backoff/api.rs index 5c65b70..0f5b5cb 100644 --- a/backon/src/backoff/api.rs +++ b/backon/src/backoff/api.rs @@ -27,6 +27,7 @@ impl BackoffBuilder for B { #[cfg(test)] mod tests { use super::*; + use crate::{ConstantBuilder, ExponentialBuilder, FibonacciBuilder}; fn test_fn_builder(b: impl BackoffBuilder) { let _ = b.build(); @@ -34,6 +35,14 @@ mod tests { #[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()); + } } } diff --git a/backon/src/backoff/constant.rs b/backon/src/backoff/constant.rs index 749c7bf..39aa918 100644 --- a/backon/src/backoff/constant.rs +++ b/backon/src/backoff/constant.rs @@ -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`]. diff --git a/backon/src/backoff/exponential.rs b/backon/src/backoff/exponential.rs index a292452..780289b 100644 --- a/backon/src/backoff/exponential.rs +++ b/backon/src/backoff/exponential.rs @@ -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`]. diff --git a/backon/src/backoff/fibonacci.rs b/backon/src/backoff/fibonacci.rs index 2137c0b..49ec073 100644 --- a/backon/src/backoff/fibonacci.rs +++ b/backon/src/backoff/fibonacci.rs @@ -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`].