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
@@ -33,10 +32,10 @@ pub trait Runtime: Clone + Send + Sync + 'static {
3332 /// finish when `TracerProvider` gets shutdown. At the moment this happens by blocking the
3433 /// current thread. This means runtime implementations need to make sure they can still execute
3534 /// 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 ;
3736
3837 /// 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 ;
4039}
4140
4241/// Uses the given runtime to produce an interval stream.
@@ -67,13 +66,16 @@ pub struct Tokio;
6766 doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio" ) ) )
6867) ]
6968impl 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+ {
7173 #[ allow( clippy:: let_underscore_future) ]
7274 // we don't have to await on the returned future to execute
7375 let _ = tokio:: spawn ( future) ;
7476 }
7577
76- fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + Sync + ' static {
78+ fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + ' static {
7779 tokio:: time:: sleep ( duration)
7880 }
7981}
@@ -105,7 +107,10 @@ pub struct TokioCurrentThread;
105107 ) ) )
106108) ]
107109impl 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+ {
109114 // We cannot force push tracing in current thread tokio scheduler because we rely on
110115 // BatchSpanProcessor to export spans in a background task, meanwhile we need to block the
111116 // shutdown function so that the runtime will not finish the blocked task and kill any
@@ -121,7 +126,7 @@ impl Runtime for TokioCurrentThread {
121126 } ) ;
122127 }
123128
124- fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + Sync + ' static {
129+ fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + ' static {
125130 tokio:: time:: sleep ( duration)
126131 }
127132}
@@ -141,12 +146,15 @@ pub struct AsyncStd;
141146 doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-async-std" ) ) )
142147) ]
143148impl 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+ {
145153 #[ allow( clippy:: let_underscore_future) ]
146154 let _ = async_std:: task:: spawn ( future) ;
147155 }
148156
149- fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + Sync + ' static {
157+ fn delay ( & self , duration : Duration ) -> impl Future < Output =( ) > + Send + ' static {
150158 async_std:: task:: sleep ( duration)
151159 }
152160}
0 commit comments