6
6
//! [Tokio]: https://crates.io/crates/tokio
7
7
//! [async-std]: https://crates.io/crates/async-std
8
8
9
- use futures_util:: { future:: BoxFuture , stream:: Stream } ;
10
9
use std:: { fmt:: Debug , future:: Future , time:: Duration } ;
11
- use futures_util:: stream:: unfold;
10
+ use futures_util:: stream:: { unfold, Stream } ;
12
11
use thiserror:: Error ;
13
12
14
13
/// A runtime is an abstraction of an async runtime like [Tokio] or [async-std]. It allows
@@ -33,10 +32,10 @@ pub trait Runtime: Clone + Send + Sync + 'static {
33
32
/// finish when `TracerProvider` gets shutdown. At the moment this happens by blocking the
34
33
/// current thread. This means runtime implementations need to make sure they can still execute
35
34
/// the given future even if the main thread is blocked.
36
- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) ;
35
+ fn spawn < F > ( & self , future : F ) where F : Future < Output = ( ) > + Send + ' static ;
37
36
38
37
/// Return a new future, which resolves after the specified [std::time::Duration].
39
- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static ;
38
+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static ;
40
39
}
41
40
42
41
/// Uses the given runtime to produce an interval stream.
@@ -67,13 +66,16 @@ pub struct Tokio;
67
66
doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio" ) ) )
68
67
) ]
69
68
impl Runtime for Tokio {
70
- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
69
+ fn spawn < F > ( & self , future : F )
70
+ where
71
+ F : Future < Output =( ) > + Send + ' static
72
+ {
71
73
#[ allow( clippy:: let_underscore_future) ]
72
74
// we don't have to await on the returned future to execute
73
75
let _ = tokio:: spawn ( future) ;
74
76
}
75
77
76
- fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + Sync + ' static {
78
+ fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + ' static {
77
79
tokio:: time:: sleep ( duration)
78
80
}
79
81
}
@@ -105,7 +107,10 @@ pub struct TokioCurrentThread;
105
107
) ) )
106
108
) ]
107
109
impl Runtime for TokioCurrentThread {
108
- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
110
+ fn spawn < F > ( & self , future : F )
111
+ where
112
+ F : Future < Output =( ) > + Send + ' static
113
+ {
109
114
// We cannot force push tracing in current thread tokio scheduler because we rely on
110
115
// BatchSpanProcessor to export spans in a background task, meanwhile we need to block the
111
116
// shutdown function so that the runtime will not finish the blocked task and kill any
@@ -121,7 +126,7 @@ impl Runtime for TokioCurrentThread {
121
126
} ) ;
122
127
}
123
128
124
- fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + Sync + ' static {
129
+ fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + ' static {
125
130
tokio:: time:: sleep ( duration)
126
131
}
127
132
}
@@ -141,12 +146,15 @@ pub struct AsyncStd;
141
146
doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-async-std" ) ) )
142
147
) ]
143
148
impl Runtime for AsyncStd {
144
- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
149
+ fn spawn < F > ( & self , future : F )
150
+ where
151
+ F : Future < Output =( ) > + Send + ' static
152
+ {
145
153
#[ allow( clippy:: let_underscore_future) ]
146
154
let _ = async_std:: task:: spawn ( future) ;
147
155
}
148
156
149
- fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + Sync + ' static {
157
+ fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + ' static {
150
158
async_std:: task:: sleep ( duration)
151
159
}
152
160
}
0 commit comments