Skip to content

Commit 88d90a1

Browse files
committed
Add example for using RetryPolicy as member in struct
1 parent 4719a9c commit 88d90a1

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/util/retry.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ use std::time::Duration;
2828
///
2929
/// let result = retry(operation, &retry_policy);
3030
///```
31+
///
32+
/// To use a retry policy as a member in a [`Send`] & [`Sync`] safe struct which needs to have known
33+
/// size at compile time, we can specify its concrete type as follows:
34+
/// ```
35+
/// # use std::time::Duration;
36+
/// # use vss_client::error::VssError;
37+
/// # use vss_client::util::retry::{ExponentialBackoffRetryPolicy, FilteredRetryPolicy, retry, RetryPolicy};
38+
///
39+
/// type VssRetryPolicy = FilteredRetryPolicy<ExponentialBackoffRetryPolicy<VssError>, Box<dyn 'static + Send + Sync + Fn(&VssError) -> bool>>;
40+
///
41+
/// struct SomeStruct {
42+
/// retry_policy: VssRetryPolicy,
43+
/// }
44+
///
45+
/// impl SomeStruct {
46+
/// fn new() -> Self {
47+
/// let retry_policy = ExponentialBackoffRetryPolicy::new(Duration::from_millis(100))
48+
/// .skip_retry_on_error(Box::new(|e: &VssError| { matches!( e, VssError::NoSuchKeyError(..)) }) as _);
49+
/// Self { retry_policy }
50+
/// }
51+
/// }
52+
/// ```
3153
pub async fn retry<R, F, Fut, T, E>(mut operation: F, retry_policy: &R) -> Result<T, E>
3254
where
3355
R: RetryPolicy<E = E>,

0 commit comments

Comments
 (0)