Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ smol = ["dep:blocking", "dep:futures-io"]
# Use `tokio`'s IO threadpool for making blocking IO async
tokio = ["dep:tokio"]

# Run blocking Futures on the async executor that should be run in an async threadpool
# (not recommended, intended for simple executors like pollster/swait)
blocking-in-async = []

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(fuzzing)'] }

Expand Down
19 changes: 17 additions & 2 deletions src/maybe_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,19 @@ pub mod blocking {
Self(tokio::task::spawn_blocking(f))
}

#[cfg(feature = "blocking-in-async")]
#[cfg(not(any(feature = "smol", feature = "tokio")))]
fn spawn(_f: impl FnOnce() -> R + Send + 'static) -> Self {
panic!("Awaiting blocking syscall without an async runtime: enable the `smol` or `tokio` feature of nusb.");
fn spawn(f: impl FnOnce() -> R + Send + 'static) -> Self {
Self(Some(f()))
}

#[cfg(not(any(feature = "smol", feature = "tokio", feature = "blocking-in-async")))]
compile_error!(concat!(
"Would await blocking syscall without an async runtime: ",
"enable nusb's `smol` or `tokio` feature to run blocking IO on an async threadpool, ",
"or `blocking-in-async` to run blocking IO directly on the async executor ",
"(not recommended unless you're intentionally minimizing dependencies)."
));
}

impl<R> Unpin for BlockingTask<R> {}
Expand All @@ -133,7 +142,13 @@ pub mod blocking {
}
}

#[cfg(feature = "blocking-in-async")]
#[cfg(not(any(feature = "smol", feature = "tokio")))]
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(self.get_mut().0.take().expect("polled after completion"))
}

#[cfg(not(any(feature = "smol", feature = "tokio", feature = "blocking-in-async")))]
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
unreachable!()
}
Expand Down
Loading