-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`DeviceInfo::open`, `Device::from_fd`, `Device::set_configuration`, `Device::reset`, `Interface::set_alt_setting`, `Interface::clear_halt` all perform IO but are currently blocking because the underlying OS APIs are blocking. `list_devices`,`list_buses`, `Device::claim_interface` `Device::detach_and_claim_interface` theoretically don't perform IO, but are also included here because they need to be async on WebUSB. The `IoAction` trait allows defering these actions to the thread pool from the `blocking` crate when used asynchronously with `.await` / `IntoFuture`, or directly runs the blocking syscall synchronously with a `.wait()` method.
1 parent
a34c449
commit a4662e6
Showing
21 changed files
with
588 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use nusb::IoAction; | ||
|
||
fn main() { | ||
env_logger::init(); | ||
for dev in nusb::list_buses().unwrap() { | ||
for dev in nusb::list_buses().wait().unwrap() { | ||
println!("{:#?}", dev); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
//! Detach the kernel driver for an FTDI device, claim the USB interface, and | ||
//! then reattach it. | ||
use std::{thread::sleep, time::Duration}; | ||
|
||
use nusb::IoAction; | ||
fn main() { | ||
env_logger::init(); | ||
let di = nusb::list_devices() | ||
.wait() | ||
.unwrap() | ||
.find(|d| d.vendor_id() == 0x0403 && d.product_id() == 0x6010) | ||
.expect("device should be connected"); | ||
|
||
let device = di.open().unwrap(); | ||
let interface = device.detach_and_claim_interface(0).unwrap(); | ||
let device = di.open().wait().unwrap(); | ||
let interface = device.detach_and_claim_interface(0).wait().unwrap(); | ||
sleep(Duration::from_secs(1)); | ||
drop(interface); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use nusb::IoAction; | ||
|
||
fn main() { | ||
env_logger::init(); | ||
for dev in nusb::list_devices().unwrap() { | ||
for dev in nusb::list_devices().wait().unwrap() { | ||
println!("{:#?}", dev); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::future::IntoFuture; | ||
|
||
/// IO that may be performed synchronously or asynchronously. | ||
/// | ||
/// An `IOAction` can be run asynchronously with `.await`, or | ||
/// run synchronously (blocking the current thread) with `.wait()`. | ||
pub trait IoAction: IntoFuture { | ||
/// Block waiting for the action to complete | ||
#[cfg(not(target_arch = "wasm32"))] | ||
fn wait(self) -> Self::Output; | ||
} | ||
|
||
#[cfg(any( | ||
target_os = "linux", | ||
target_os = "android", | ||
target_os = "windows", | ||
target_os = "macos" | ||
))] | ||
pub mod blocking { | ||
use super::IoAction; | ||
use std::future::IntoFuture; | ||
|
||
/// Wrapper that invokes a FnOnce on a background thread when | ||
/// called asynchronously, or directly when called synchronously. | ||
pub struct Blocking<F> { | ||
f: F, | ||
} | ||
|
||
impl<F> Blocking<F> { | ||
pub fn new(f: F) -> Self { | ||
Self { f } | ||
} | ||
} | ||
|
||
impl<F, R> IntoFuture for Blocking<F> | ||
where | ||
F: FnOnce() -> R + Send + 'static, | ||
R: Send + 'static, | ||
{ | ||
type Output = R; | ||
|
||
type IntoFuture = blocking::Task<R, ()>; | ||
|
||
fn into_future(self) -> Self::IntoFuture { | ||
blocking::unblock(self.f) | ||
} | ||
} | ||
|
||
impl<F, R> IoAction for Blocking<F> | ||
where | ||
F: FnOnce() -> R + Send + 'static, | ||
R: Send + 'static, | ||
{ | ||
fn wait(self) -> R { | ||
(self.f)() | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct Ready<T>(pub(crate) T); | ||
|
||
impl<T> IntoFuture for Ready<T> { | ||
type Output = T; | ||
type IntoFuture = std::future::Ready<T>; | ||
|
||
fn into_future(self) -> Self::IntoFuture { | ||
std::future::ready(self.0) | ||
} | ||
} | ||
|
||
impl<T> IoAction for Ready<T> { | ||
fn wait(self) -> Self::Output { | ||
self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters