@@ -123,23 +123,31 @@ pub fn hashcash(resource: String, bits: u32) -> String {
123123#[ error( "Timed out" ) ]
124124pub ( crate ) struct TimeoutError ;
125125
126+ /// Utility function to call async timer implementations for WASM and others
127+ pub ( crate ) async fn sleep ( timeout : std:: time:: Duration ) {
128+ #[ cfg( target_family = "wasm" ) ]
129+ wasmtimer:: tokio:: sleep ( timeout) . await ;
130+ #[ cfg( not( target_family = "wasm" ) ) ]
131+ async_io:: Timer :: after ( timeout) . await ;
132+ }
133+
126134/// Utility function to add a timeout to a future
127135///
128136/// This behaves the same as async std timeout, but with async-io
129- #[ cfg( not( target_family = "wasm" ) ) ]
130137pub ( crate ) fn timeout < ' a , R , F : std:: future:: Future < Output = R > + ' a > (
131138 timeout : std:: time:: Duration ,
132139 future : F ,
133140) -> impl Future < Output = Result < R , TimeoutError > > + ' a {
134141 let timeout_future = async move {
135- async_io :: Timer :: after ( timeout) . await ;
142+ sleep ( timeout) . await ;
136143 Err ( TimeoutError )
137144 } ;
138145
139146 futures_lite:: future:: or ( async { Ok ( future. await ) } , timeout_future)
140147}
141148
142149/// Utility function to spawn a future. We don't use crate::util::spawn, because not the entirety of smol compiles on WASM
150+ #[ allow( dead_code) ]
143151pub ( crate ) fn spawn < T : Send + ' static > (
144152 future : impl Future < Output = T > + Send + ' static ,
145153) -> async_task:: Task < T > {
0 commit comments