@@ -137,6 +137,35 @@ impl Runtime for TokioCurrentThread {
137137 }
138138}
139139
140+ /// Runtime implementation, which works with Tokio's multi thread runtime.
141+ #[ cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio-with-wasm" ) ) ]
142+ #[ cfg_attr(
143+ docsrs,
144+ doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio-with-wasm" ) ) )
145+ ) ]
146+ #[ derive( Debug , Clone ) ]
147+ pub struct TokioWithWasm ;
148+
149+ #[ cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio-with-wasm" ) ) ]
150+ #[ cfg_attr(
151+ docsrs,
152+ doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio-with-wasm" ) ) )
153+ ) ]
154+ impl Runtime for TokioWithWasm {
155+ fn spawn < F > ( & self , future : F )
156+ where
157+ F : Future < Output = ( ) > + Send + ' static ,
158+ {
159+ #[ allow( clippy:: let_underscore_future) ]
160+ // we don't have to await on the returned future to execute
161+ let _ = tokio_with_wasm:: alias:: spawn ( future) ;
162+ }
163+
164+ fn delay ( & self , duration : Duration ) -> impl Future < Output = ( ) > + Send + ' static {
165+ futures_timer:: Delay :: new ( duration)
166+ }
167+ }
168+
140169/// `RuntimeChannel` is an extension to [`Runtime`]. Currently, it provides a
141170/// channel that is used by the [log] and [span] batch processors.
142171///
@@ -185,7 +214,7 @@ pub trait TrySend: Sync + Send {
185214
186215#[ cfg( all(
187216 feature = "experimental_async_runtime" ,
188- any( feature = "rt-tokio" , feature = "rt-tokio-current-thread" )
217+ any( feature = "rt-tokio" , feature = "rt-tokio-current-thread" , )
189218) ) ]
190219impl < T : Send > TrySend for tokio:: sync:: mpsc:: Sender < T > {
191220 type Message = T ;
@@ -198,6 +227,25 @@ impl<T: Send> TrySend for tokio::sync::mpsc::Sender<T> {
198227 }
199228}
200229
230+ #[ cfg( all(
231+ feature = "experimental_async_runtime" ,
232+ any( feature = "rt-tokio-with-wasm" )
233+ ) ) ]
234+ impl < T : Send > TrySend for tokio_with_wasm:: alias:: sync:: mpsc:: Sender < T > {
235+ type Message = T ;
236+
237+ fn try_send ( & self , item : Self :: Message ) -> Result < ( ) , TrySendError > {
238+ self . try_send ( item) . map_err ( |err| match err {
239+ tokio_with_wasm:: alias:: sync:: mpsc:: error:: TrySendError :: Full ( _) => {
240+ TrySendError :: ChannelFull
241+ }
242+ tokio_with_wasm:: alias:: sync:: mpsc:: error:: TrySendError :: Closed ( _) => {
243+ TrySendError :: ChannelClosed
244+ }
245+ } )
246+ }
247+ }
248+
201249#[ cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio" ) ) ]
202250#[ cfg_attr(
203251 docsrs,
@@ -245,3 +293,24 @@ impl RuntimeChannel for TokioCurrentThread {
245293 )
246294 }
247295}
296+
297+ #[ cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio-with-wasm" ) ) ]
298+ #[ cfg_attr(
299+ docsrs,
300+ doc( cfg( all( feature = "experimental_async_runtime" , feature = "rt-tokio-with-wasm" ) ) )
301+ ) ]
302+ impl RuntimeChannel for TokioWithWasm {
303+ type Receiver < T : Debug + Send > = tokio_stream:: wrappers:: ReceiverStream < T > ;
304+ type Sender < T : Debug + Send > = tokio_with_wasm:: alias:: sync:: mpsc:: Sender < T > ;
305+
306+ fn batch_message_channel < T : Debug + Send > (
307+ & self ,
308+ capacity : usize ,
309+ ) -> ( Self :: Sender < T > , Self :: Receiver < T > ) {
310+ let ( sender, receiver) = tokio_with_wasm:: alias:: sync:: mpsc:: channel ( capacity) ;
311+ (
312+ sender,
313+ tokio_stream:: wrappers:: ReceiverStream :: new ( receiver) ,
314+ )
315+ }
316+ }
0 commit comments