@@ -23,13 +23,19 @@ use tokio::time::{self};
2323/// check and the current availability check
2424pub type OldStatus = Status ;
2525
26+ /// Type for a boxed trait object implementing [Target]
27+ type BoxedTarget < ' a > = Box < dyn Target + Send + ' a > ;
28+
29+ /// Type containing a boxed trait object implementing [FnMut] that is called with each async check.
30+ type BoxedHandler < ' a > = Box < dyn FnMut ( & dyn Target , Status , OldStatus , Option < CheckTargetError > ) + Send + ' a > ;
31+
2632/// Struct storing all data used during asynchronous execution.
2733///
2834/// For async check execution, wrap the instances of [Target] in [AsyncTarget] and hand them to
2935/// [AsyncTargetExecutor::start].
3036pub struct AsyncTarget < ' a > {
31- target : Box < dyn Target + Send + ' a > ,
32- check_handler : Box < dyn FnMut ( & dyn Target , Status , OldStatus , Option < CheckTargetError > ) + Send + ' a > ,
37+ target : BoxedTarget < ' a > ,
38+ check_handler : BoxedHandler < ' a > ,
3339 check_interval : Duration ,
3440 status : Status ,
3541}
@@ -44,15 +50,11 @@ impl<'a> AsyncTarget<'a> {
4450 ///
4551 /// # Returns
4652 /// Instance of [AsyncTarget].
47- pub fn new (
48- target : Box < dyn Target + Send + ' a > ,
49- check_handler : Box < dyn FnMut ( & dyn Target , Status , OldStatus , Option < CheckTargetError > ) + Send + ' a > ,
50- check_interval : Duration ,
51- ) -> Self {
53+ pub fn new ( target : BoxedTarget < ' a > , check_handler : BoxedHandler < ' a > , check_interval : Duration ) -> Self {
5254 AsyncTarget {
53- target : target ,
54- check_handler : check_handler ,
55- check_interval : check_interval ,
55+ target,
56+ check_handler,
57+ check_interval,
5658 status : Status :: Unknown ,
5759 }
5860 }
@@ -152,13 +154,19 @@ impl AsyncTargetExecutor {
152154 }
153155}
154156
157+ impl Default for AsyncTargetExecutor {
158+ fn default ( ) -> Self {
159+ AsyncTargetExecutor :: new ( )
160+ }
161+ }
162+
155163impl Drop for AsyncTargetExecutor {
156164 fn drop ( & mut self ) {
157165 self . stop ( )
158166 }
159167}
160168
161- async fn check_target_periodically ( mut target : AsyncTarget < ' static > , mut teardown_recv : Receiver < ( ) > ) -> ( ) {
169+ async fn check_target_periodically ( mut target : AsyncTarget < ' static > , mut teardown_recv : Receiver < ( ) > ) {
162170 loop {
163171 target = select ! {
164172 // Teardown message was not received. Perform next check.
@@ -170,7 +178,7 @@ async fn check_target_periodically(mut target: AsyncTarget<'static>, mut teardow
170178 }
171179}
172180
173- async fn check_target ( mut target : AsyncTarget < ' static > ) -> AsyncTarget < ' _ > {
181+ async fn check_target ( mut target : AsyncTarget < ' static > ) -> AsyncTarget < ' static > {
174182 // Setup sleep timer to wait, to prevent further execution before the check_interval elapsed.
175183 let sleep = time:: sleep ( target. check_interval ) ;
176184
0 commit comments