1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! Asychronous versions of the common retry functions
//!
//!
//! # Usage
//!
//! ```
//! use retry_block::async_retry;
//! use retry_block::OperationResult;
//! use retry_block::delay::Fixed;
//! use tokio::sync::Mutex;
//! use std::sync::Arc;
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() {
//! let mut collection = vec![1, 2, 3].into_iter();
//!
//! let result = async_retry!(Fixed::new(Duration::from_millis(100)), {
//! match collection.next() {
//! Some(n) if n == 3 => Ok("n is 3!"),
//! Some(_) => Err("n must be 3!"),
//! None => Err("n was never 3!"),
//! }
//! });
//!
//! assert!(result.is_ok());
//! }
//! ```
//!
//! ```
//! use retry_block::future::async_retry_fn;
//! use retry_block::OperationResult;
//! use retry_block::delay::Fixed;
//! use tokio::sync::Mutex;
//! use std::sync::Arc;
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() {
//! let collection = Arc::new(Mutex::new(vec![1, 2, 3].into_iter()));
//!
//! let result = async_retry_fn(Fixed::new(Duration::from_millis(100)), || async {
//! match collection.clone().lock().await.next() {
//! Some(n) if n == 3 => Ok("n is 3!"),
//! Some(_) => Err("n must be 3!"),
//! None => Err("n was never 3!"),
//! }
//! }).await;
//!
//! assert!(result.is_ok());
//! }
//! ```
//!
//! ```
//! use retry_block::async_retry;
//! use retry_block::OperationResult;
//! use retry_block::delay::Fixed;
//! use retry_block::RetryConfig;
//! use tokio::sync::Mutex;
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() {
//! let config = RetryConfig {
//! count: 1,
//! min_backoff: 100,
//! max_backoff: 300,
//! };
//! let mut collection = vec![1, 2, 3].into_iter();
//!
//! let result = async_retry!(config, {
//! match collection.next() {
//! Some(n) if n == 3 => Ok("n is 3!"),
//! Some(_) => Err("n must be 3!"),
//! None => Err("n was never 3!"),
//! }
//! });
//!
//! assert!(result.is_err());
//! }
//! ```
use crateasync_retry;
use crateOperationResult;
use Duration;
/// Retry the given operation until it succeeds, or until the given `Duration`
/// iterator ends.
///
/// <div class="example-wrap" style="display:inline-block">
/// <pre class="ignore" style="white-space:normal;font:inherit;">
///
/// **Warning**: Capturing outside values in async blocks of `FnMut`s will not work all the
/// time because async blocks may create references that outlive their scope.
///
/// You may have to wrap your data with `Arc<Mutex<_>>` or use `futures::Stream`
///
/// </pre></div>
pub async