@@ -96,6 +96,15 @@ impl Delay {
96
96
Self :: exponential_backoff_capped ( initial, multiplier, Duration :: from_secs ( std:: u64:: MAX ) )
97
97
}
98
98
99
+ /// Call a function every tick, expecting some kind of side effect (e.g. a progress
100
+ /// bar).
101
+ pub fn side_effect < F > ( function : F ) -> Self
102
+ where
103
+ F : ' static + Sync + Send + Clone + Fn ( ) -> Result < ( ) , WaiterError > ,
104
+ {
105
+ Self :: from ( Box :: new ( SideEffectWaiter :: new ( function) ) )
106
+ }
107
+
99
108
pub fn builder ( ) -> DelayBuilder {
100
109
DelayBuilder { inner : None }
101
110
}
@@ -142,6 +151,12 @@ impl DelayBuilder {
142
151
) -> Self {
143
152
self . with ( Delay :: exponential_backoff_capped ( initial, multiplier, cap) )
144
153
}
154
+ pub fn side_effect < F > ( self , function : F ) -> Self
155
+ where
156
+ F : ' static + Sync + Send + Clone + Fn ( ) -> Result < ( ) , WaiterError > ,
157
+ {
158
+ self . with ( Delay :: side_effect ( function) )
159
+ }
145
160
pub fn build ( mut self ) -> Delay {
146
161
self . inner . take ( ) . unwrap_or_else ( Delay :: instant)
147
162
}
@@ -289,3 +304,28 @@ impl Waiter for ExponentialBackoffWaiter {
289
304
Ok ( ( ) )
290
305
}
291
306
}
307
+
308
+ #[ derive( Clone ) ]
309
+ struct SideEffectWaiter < F >
310
+ where
311
+ F : ' static + Sync + Send + Clone + Fn ( ) -> Result < ( ) , WaiterError > ,
312
+ {
313
+ function : F ,
314
+ }
315
+
316
+ impl < F > SideEffectWaiter < F >
317
+ where
318
+ F : ' static + Sync + Send + Clone + Fn ( ) -> Result < ( ) , WaiterError > ,
319
+ {
320
+ pub fn new ( function : F ) -> Self {
321
+ Self { function }
322
+ }
323
+ }
324
+ impl < F > Waiter for SideEffectWaiter < F >
325
+ where
326
+ F : ' static + Sync + Send + Clone + Fn ( ) -> Result < ( ) , WaiterError > ,
327
+ {
328
+ fn wait ( & self ) -> Result < ( ) , WaiterError > {
329
+ ( self . function ) ( )
330
+ }
331
+ }
0 commit comments