66//! [Tokio]: https://crates.io/crates/tokio
77//! [async-std]: https://crates.io/crates/async-std
88
9- use futures_util:: { future:: BoxFuture , stream:: Stream } ;
109use std:: { fmt:: Debug , future:: Future , time:: Duration } ;
11- use futures_util:: stream:: unfold;
10+ use futures_util:: stream:: { unfold, Stream } ;
1211use thiserror:: Error ;
1312
1413/// A runtime is an abstraction of an async runtime like [Tokio] or [async-std]. It allows
@@ -19,7 +18,7 @@ use thiserror::Error;
1918///
2019/// # Note
2120///
22- /// OpenTelemetry expects a *multi-threaded * runtime because its types can move across threads.
21+ /// OpenTelemetry expects a *multithreaded * runtime because its types can move across threads.
2322/// For this reason, this trait requires the `Send` and `Sync` bounds. Single-threaded runtimes
2423/// can implement this trait in a way that spawns the tasks on the same thread as the calling code.
2524#[ cfg( feature = "experimental_async_runtime" ) ]
@@ -30,13 +29,15 @@ pub trait Runtime: Clone + Send + Sync + 'static {
3029 ///
3130 /// This is mainly used to run batch span processing in the background. Note, that the function
3231 /// does not return a handle. OpenTelemetry will use a different way to wait for the future to
33- /// finish when `TracerProvider` gets shutdown. At the moment this happens by blocking the
32+ /// finish when the caller shuts down.
33+ ///
34+ /// At the moment, the shutdown happens by blocking the
3435 /// current thread. This means runtime implementations need to make sure they can still execute
3536 /// the given future even if the main thread is blocked.
36- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) ;
37+ fn spawn < F > ( & self , future : F ) where F : Future < Output = ( ) > + Send + ' static ;
3738
38- /// Return a new future, which resolves after the specified [std::time:: Duration].
39- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static ;
39+ /// Return a future that resolves after the specified [Duration].
40+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static ;
4041}
4142
4243/// Uses the given runtime to produce an interval stream.
@@ -67,13 +68,16 @@ pub struct Tokio;
6768 doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio" ) ) )
6869) ]
6970impl Runtime for Tokio {
70- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
71+ fn spawn < F > ( & self , future : F )
72+ where
73+ F : Future < Output = ( ) > + Send + ' static
74+ {
7175 #[ allow( clippy:: let_underscore_future) ]
7276 // we don't have to await on the returned future to execute
7377 let _ = tokio:: spawn ( future) ;
7478 }
7579
76- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static {
80+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static {
7781 tokio:: time:: sleep ( duration)
7882 }
7983}
@@ -105,7 +109,10 @@ pub struct TokioCurrentThread;
105109 ) ) )
106110) ]
107111impl Runtime for TokioCurrentThread {
108- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
112+ fn spawn < F > ( & self , future : F )
113+ where
114+ F : Future < Output = ( ) > + Send + ' static
115+ {
109116 // We cannot force push tracing in current thread tokio scheduler because we rely on
110117 // BatchSpanProcessor to export spans in a background task, meanwhile we need to block the
111118 // shutdown function so that the runtime will not finish the blocked task and kill any
@@ -121,7 +128,7 @@ impl Runtime for TokioCurrentThread {
121128 } ) ;
122129 }
123130
124- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static {
131+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static {
125132 tokio:: time:: sleep ( duration)
126133 }
127134}
@@ -141,12 +148,15 @@ pub struct AsyncStd;
141148 doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-async-std" ) ) )
142149) ]
143150impl Runtime for AsyncStd {
144- fn spawn ( & self , future : BoxFuture < ' static , ( ) > ) {
151+ fn spawn < F > ( & self , future : F )
152+ where
153+ F : Future < Output = ( ) > + Send + ' static
154+ {
145155 #[ allow( clippy:: let_underscore_future) ]
146156 let _ = async_std:: task:: spawn ( future) ;
147157 }
148158
149- fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + Sync + ' static {
159+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static {
150160 async_std:: task:: sleep ( duration)
151161 }
152162}
0 commit comments