@@ -131,10 +131,8 @@ use std::future::Future;
131
131
use std:: io;
132
132
use std:: pin:: Pin ;
133
133
use std:: task:: { Context , Poll } ;
134
- use std:: thread;
135
134
136
135
use futures_core:: ready;
137
- use once_cell:: sync:: Lazy ;
138
136
use pin_project_lite:: pin_project;
139
137
140
138
/// Applies the [`Compat`] adapter to futures and I/O types.
@@ -208,7 +206,9 @@ pin_project! {
208
206
if this. inner. is_some( ) {
209
207
// If the inner future wasn't moved out using into_inner,
210
208
// enter the tokio context while the inner value is dropped.
209
+ #[ cfg( not( feature = "disable" ) ) ]
211
210
let _guard = get_runtime_handle( ) . enter( ) ;
211
+
212
212
this. project( ) . inner. set( None ) ;
213
213
}
214
214
}
@@ -326,7 +326,9 @@ impl<T: Future> Future for Compat<T> {
326
326
type Output = T :: Output ;
327
327
328
328
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
329
+ #[ cfg( not( feature = "disable" ) ) ]
329
330
let _guard = get_runtime_handle ( ) . enter ( ) ;
331
+
330
332
self . get_pin_mut ( ) . poll ( cx)
331
333
}
332
334
}
@@ -453,12 +455,14 @@ impl<T: futures_io::AsyncSeek> tokio::io::AsyncSeek for Compat<T> {
453
455
}
454
456
}
455
457
458
+ #[ cfg( not( feature = "disable" ) ) ]
456
459
fn get_runtime_handle ( ) -> tokio:: runtime:: Handle {
457
460
tokio:: runtime:: Handle :: try_current ( ) . unwrap_or_else ( |_| TOKIO1 . handle ( ) . clone ( ) )
458
461
}
459
462
460
- static TOKIO1 : Lazy < tokio:: runtime:: Runtime > = Lazy :: new ( || {
461
- thread:: Builder :: new ( )
463
+ #[ cfg( not( feature = "disable" ) ) ]
464
+ static TOKIO1 : once_cell:: sync:: Lazy < tokio:: runtime:: Runtime > = once_cell:: sync:: Lazy :: new ( || {
465
+ std:: thread:: Builder :: new ( )
462
466
. name ( "async-compat/tokio-1" . into ( ) )
463
467
. spawn ( || TOKIO1 . block_on ( Pending ) )
464
468
. unwrap ( ) ;
@@ -480,11 +484,13 @@ impl Future for Pending {
480
484
481
485
#[ cfg( test) ]
482
486
mod tests {
483
- use super :: Lazy ;
484
- use crate :: { CompatExt , TOKIO1 } ;
485
-
487
+ #[ cfg( not( feature = "disable" ) ) ]
486
488
#[ test]
487
489
fn fallback_runtime_is_created_if_and_only_if_outside_tokio_context ( ) {
490
+ use crate :: CompatExt ;
491
+ use crate :: TOKIO1 ;
492
+ use once_cell:: sync:: Lazy ;
493
+
488
494
// Use compat inside of a tokio context.
489
495
tokio:: runtime:: Builder :: new_multi_thread ( )
490
496
. enable_all ( )
@@ -504,6 +510,7 @@ mod tests {
504
510
assert ! ( Lazy :: get( & TOKIO1 ) . is_some( ) ) ;
505
511
}
506
512
513
+ #[ cfg( not( feature = "disable" ) ) ]
507
514
async fn use_tokio ( ) {
508
515
tokio:: time:: sleep ( std:: time:: Duration :: from_micros ( 1 ) ) . await
509
516
}
0 commit comments