Merge pull request #147 from kevinmehall/blocking-no-runtime

Panic if awaiting a blocking syscall without `smol` or `tokio` features.
This commit is contained in:
Kevin Mehall 2025-06-15 11:25:07 -06:00 committed by GitHub
commit 04e8ddfc30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 17 deletions

View file

@ -120,16 +120,17 @@
//! This allows for async usage in an async context, or blocking usage in a
//! non-async context.
//!
//! Operations such as [`DeviceInfo::open`], [`Device::set_configuration`],
//! [`Device::reset`], [`Device::claim_interface`],
//! [`Interface::set_alt_setting`], and [`Endpoint::clear_halt`] require
//! blocking system calls. To use these in an asynchronous context, `nusb`
//! relies on an async runtime to run these operations on an IO thread to avoid
//! blocking in async code. Enable the cargo feature `tokio` or `smol` to use
//! the corresponding runtime for blocking IO. If neither feature is enabled,
//! `.await` on these methods will log a warning and block the calling thread.
//! `.wait()` always runs the blocking operation directly without the overhead
//! of handing off to an IO thread.
//! Operations such as [`list_devices`], [`list_buses`], [`DeviceInfo::open`],
//! [`Device::set_configuration`], [`Device::reset`],
//! [`Device::claim_interface`], [`Interface::set_alt_setting`], and
//! [`Endpoint::clear_halt`] require blocking system calls. To use these in an
//! asynchronous context, `nusb` requires an async runtime to run these
//! operations on an IO thread to avoid blocking in async code. Enable the cargo
//! feature `tokio` or `smol` to use the corresponding runtime for blocking IO.
//! If neither feature is enabled, `.await` on these methods will panic.
//!
//! For blocking usage, `.wait()` always runs the blocking operation directly
//! without the overhead of handing off to an IO thread.
//!
//! These features do not affect and are not required for transfers, which are
//! implemented on top of natively-async OS APIs.

View file

@ -107,13 +107,7 @@ pub mod blocking {
#[cfg(not(any(feature = "smol", feature = "tokio")))]
fn spawn(f: impl FnOnce() -> R + Send + 'static) -> Self {
static ONCE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
if ONCE.swap(false, std::sync::atomic::Ordering::Relaxed) {
log::warn!("Awaiting blocking syscall without an async runtime: enable the `smol` or `tokio` feature of `nusb` to avoid blocking the thread.")
}
Self(Some(f()))
panic!("Awaiting blocking syscall without an async runtime: enable the `smol` or `tokio` feature of nusb.");
}
}