Skip to content

Commit a379d62

Browse files
authored
feat: Implement BackoffBuilder for Backoff itself (#142)
Signed-off-by: Xuanwo <[email protected]> Signed-off-by: Xuanwo <[email protected]>
1 parent 1c34c4a commit a379d62

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

backon/src/backoff/api.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
use core::time::Duration;
22

3+
/// Backoff is an [`Iterator`] that returns [`Duration`].
4+
///
5+
/// - `Some(Duration)` indicates the caller should `sleep(Duration)` and retry the request.
6+
/// - `None` indicates the limits have been reached, and the caller should return the current error instead.
7+
pub trait Backoff: Iterator<Item = Duration> + Send + Sync + Unpin {}
8+
impl<T> Backoff for T where T: Iterator<Item = Duration> + Send + Sync + Unpin {}
9+
310
/// BackoffBuilder is utilized to construct a new backoff.
411
pub trait BackoffBuilder: Send + Sync + Unpin {
512
/// The associated backoff returned by this builder.
@@ -9,9 +16,24 @@ pub trait BackoffBuilder: Send + Sync + Unpin {
916
fn build(self) -> Self::Backoff;
1017
}
1118

12-
/// Backoff is an [`Iterator`] that returns [`Duration`].
13-
///
14-
/// - `Some(Duration)` indicates the caller should `sleep(Duration)` and retry the request.
15-
/// - `None` indicates the limits have been reached, and the caller should return the current error instead.
16-
pub trait Backoff: Iterator<Item = Duration> + Send + Sync + Unpin {}
17-
impl<T> Backoff for T where T: Iterator<Item = Duration> + Send + Sync + Unpin {}
19+
impl<B: Backoff> BackoffBuilder for B {
20+
type Backoff = B;
21+
22+
fn build(self) -> Self::Backoff {
23+
self
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
31+
fn test_fn_builder(b: impl BackoffBuilder) {
32+
let _ = b.build();
33+
}
34+
35+
#[test]
36+
fn test_backoff_builder() {
37+
test_fn_builder([Duration::from_secs(1)].into_iter())
38+
}
39+
}

0 commit comments

Comments
 (0)