Skip to content

Commit 377e192

Browse files
committed
feat: add "disable" feature
This feature will transparently disable the spawning of a thread that runs a tokio runtime.
1 parent 9761a9d commit 377e192

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ documentation = "https://docs.rs/async-compat"
1414
keywords = ["tokio", "futures", "convert", "context"]
1515
categories = ["asynchronous"]
1616

17+
[features]
18+
disable = []
19+
1720
[dependencies]
1821
futures-core = "0.3.5"
1922
futures-io = "0.3.5"

src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,8 @@ use std::future::Future;
131131
use std::io;
132132
use std::pin::Pin;
133133
use std::task::{Context, Poll};
134-
use std::thread;
135134

136135
use futures_core::ready;
137-
use once_cell::sync::Lazy;
138136
use pin_project_lite::pin_project;
139137

140138
/// Applies the [`Compat`] adapter to futures and I/O types.
@@ -208,7 +206,9 @@ pin_project! {
208206
if this.inner.is_some() {
209207
// If the inner future wasn't moved out using into_inner,
210208
// enter the tokio context while the inner value is dropped.
209+
#[cfg(not(feature = "disable"))]
211210
let _guard = get_runtime_handle().enter();
211+
212212
this.project().inner.set(None);
213213
}
214214
}
@@ -326,7 +326,9 @@ impl<T: Future> Future for Compat<T> {
326326
type Output = T::Output;
327327

328328
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
329+
#[cfg(not(feature = "disable"))]
329330
let _guard = get_runtime_handle().enter();
331+
330332
self.get_pin_mut().poll(cx)
331333
}
332334
}
@@ -453,12 +455,14 @@ impl<T: futures_io::AsyncSeek> tokio::io::AsyncSeek for Compat<T> {
453455
}
454456
}
455457

458+
#[cfg(not(feature = "disable"))]
456459
fn get_runtime_handle() -> tokio::runtime::Handle {
457460
tokio::runtime::Handle::try_current().unwrap_or_else(|_| TOKIO1.handle().clone())
458461
}
459462

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()
462466
.name("async-compat/tokio-1".into())
463467
.spawn(|| TOKIO1.block_on(Pending))
464468
.unwrap();
@@ -480,11 +484,13 @@ impl Future for Pending {
480484

481485
#[cfg(test)]
482486
mod tests {
483-
use super::Lazy;
484-
use crate::{CompatExt, TOKIO1};
485-
487+
#[cfg(not(feature = "disable"))]
486488
#[test]
487489
fn fallback_runtime_is_created_if_and_only_if_outside_tokio_context() {
490+
use super::Lazy;
491+
use crate::CompatExt;
492+
use crate::TOKIO1;
493+
488494
// Use compat inside of a tokio context.
489495
tokio::runtime::Builder::new_multi_thread()
490496
.enable_all()
@@ -504,6 +510,7 @@ mod tests {
504510
assert!(Lazy::get(&TOKIO1).is_some());
505511
}
506512

513+
#[cfg(not(feature = "disable"))]
507514
async fn use_tokio() {
508515
tokio::time::sleep(std::time::Duration::from_micros(1)).await
509516
}

0 commit comments

Comments
 (0)