From f1fa9dd19d7bc395d5908b5a65c571d869d297b6 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Wed, 14 Aug 2024 17:40:06 -0500 Subject: [PATCH] Added `get_device_list` for wasm, changed `Descriptor` to `DeviceInfo` --- Cargo.toml | 9 +++------ src/backend/native.rs | 18 +++++++++--------- src/backend/wasm.rs | 24 +++++++++++------------- src/lib.rs | 23 +++++++++++------------ src/usb.rs | 3 +-- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 771dd84..6cc1a41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,14 +48,11 @@ features = [ [target.'cfg(not(target_family = "wasm"))'.dependencies] nusb = "0.1" -[profile.release] -lto = true -strip = true -opt-level = "z" -codegen-units = 1 - [package.metadata.wasm-pack.profile.dev.wasm-bindgen] dwarf-debug-info = true [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "x86_64-apple-darwin", "aarch64-apple-darwin", "wasm32-unknown-unknown"] + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(web_sys_unstable_apis)'] } diff --git a/src/backend/native.rs b/src/backend/native.rs index 52d7f43..1d9e134 100644 --- a/src/backend/native.rs +++ b/src/backend/native.rs @@ -1,15 +1,15 @@ use crate::usb::{ - ControlIn, ControlOut, ControlType, UsbDescriptor, UsbDevice, UsbInterface, Recipient, UsbError, + ControlIn, ControlOut, ControlType, UsbDeviceInfo, UsbDevice, UsbInterface, Recipient, UsbError, }; #[derive(Clone, Debug)] -pub struct Descriptor { +pub struct DeviceInfo { device_info: nusb::DeviceInfo, } #[derive(Clone)] pub struct Device { - device_info: Descriptor, + device_info: DeviceInfo, device: nusb::Device, } @@ -60,7 +60,7 @@ impl DeviceFilter { pub async fn get_device( device_filters: Vec -) -> Result { +) -> Result { let devices = nusb::list_devices().unwrap(); let mut device_info = None; @@ -101,12 +101,12 @@ pub async fn get_device( None => return Err(UsbError::DeviceNotFound), }; - Ok(Descriptor { device_info }) + Ok(DeviceInfo { device_info }) } pub async fn get_device_list( device_filters: Vec, -) -> Result, UsbError> { +) -> Result, UsbError> { let devices_info = nusb::list_devices().unwrap(); let mut devices = Vec::new(); @@ -145,15 +145,15 @@ pub async fn get_device_list( return Err(UsbError::DeviceNotFound); } - let devices_opened: Vec = devices + let devices_opened: Vec = devices .into_iter() - .map(|d| Descriptor { device_info: d }) + .map(|d| DeviceInfo { device_info: d }) .collect(); Ok(devices_opened.into_iter()) } -impl UsbDescriptor for Descriptor { +impl UsbDeviceInfo for DeviceInfo { type Device = Device; async fn open(self) -> Result { diff --git a/src/backend/wasm.rs b/src/backend/wasm.rs index d909876..637adc0 100644 --- a/src/backend/wasm.rs +++ b/src/backend/wasm.rs @@ -10,12 +10,12 @@ use web_sys::{ // Crate stuff use crate::usb::{ - ControlIn, ControlOut, ControlType, UsbDescriptor, UsbDevice, UsbInterface, Recipient, UsbError, + ControlIn, ControlOut, ControlType, UsbDeviceInfo, UsbDevice, UsbInterface, Recipient, UsbError, }; #[wasm_bindgen] #[derive(Debug)] -pub struct Descriptor { +pub struct DeviceInfo { device: WasmUsbDevice, } @@ -61,7 +61,7 @@ impl DeviceFilter { } #[wasm_bindgen] -pub async fn get_device(device_filter: Vec) -> Result { +pub async fn get_device(device_filter: Vec) -> Result { let window = web_sys::window().unwrap(); let navigator = window.navigator(); @@ -102,7 +102,7 @@ pub async fn get_device(device_filter: Vec) -> Result) -> Result) -> Result, js_sys::Error> { +pub async fn get_device_list(device_filter: Vec) -> Result, js_sys::Error> { let window = web_sys::window().unwrap(); let navigator = window.navigator(); @@ -208,7 +207,7 @@ pub async fn get_device_list(device_filter: Vec) -> Result) -> Result Result { @@ -283,11 +281,11 @@ impl UsbDescriptor for Descriptor { } async fn product_id(&self) -> u16 { - self.device.vendor_id() + self.device.product_id() } async fn vendor_id(&self) -> u16 { - self.device.product_id() + self.device.vendor_id() } async fn class(&self) -> u8 { diff --git a/src/lib.rs b/src/lib.rs index 05e9b22..08bee22 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,16 +9,12 @@ //! //! When an [`Interface`] is dropped, it is automatically released. //! -//! ### CURRENT LIMITATIONS: +//! ## CURRENT LIMITATIONS: //! * Hotplug support is not implemented. Waiting on [hotplug support in nusb](https://github.com/kevinmehall/nusb/pull/20). //! -//! * Until [this pull request](https://github.com/rustwasm/wasm-bindgen/issues/3155) -//! is merged into wasm bindgen, getting a list of USB devices is not possible on WASM -//! targets. However, this isn't a huge deal as the user gets a list to select from anyway. -//! -//! * When compiling this crate on a WASM target, you must use either +//! * When compiling this crate on a WASM target, you **must** use either //! `RUSTFLAGS=--cfg=web_sys_unstable_apis` or by passing the argument in a -//! `.cargo/config.toml` file. Read more here: https://rustwasm.github.io/wasm-bindgen/web-sys/unstable-apis.html +//! `.cargo/config.toml` file. Read more here: //! //! ## Example: //! ```no_run @@ -63,7 +59,7 @@ pub mod usb; /// use cross_usb::prelude::*; /// ``` pub mod prelude { - pub use crate::usb::UsbDescriptor; + pub use crate::usb::UsbDeviceInfo; pub use crate::usb::UsbDevice; pub use crate::usb::UsbInterface; } @@ -79,9 +75,12 @@ mod context; mod context; #[doc(inline)] -/// A descriptor of a USB device, containing information about a device +/// Information about a USB device, containing information about a device /// without claiming it -pub use crate::context::Descriptor; +/// +/// **Note:** On WASM targets, a device *must* be claimed in order to get +/// information about it in normal circumstances. +pub use crate::context::DeviceInfo; #[doc(inline)] /// A USB device, you must open an [`Interface`] to perform transfers @@ -96,7 +95,7 @@ pub use crate::context::Interface; #[doc(inline)] pub use crate::context::DeviceFilter; -/// Gets a single (the first found) device as a [`Descriptor`] from a list of VendorID +/// Gets a single (the first found) device as a [`DeviceInfo`] from a list of VendorID /// and ProductIDs /// /// ## Example @@ -115,7 +114,7 @@ pub use crate::context::DeviceFilter; #[doc(inline)] pub use crate::context::get_device; -/// Gets a list of [`Descriptor`]s from a list of VendorID and ProductIDs +/// Gets a list of [`DeviceInfo`]s from a list of VendorID and ProductIDs /// /// ## Example /// ```no_run diff --git a/src/usb.rs b/src/usb.rs index 4f0b023..4b30aa3 100644 --- a/src/usb.rs +++ b/src/usb.rs @@ -4,7 +4,7 @@ use thiserror::Error; -pub trait UsbDescriptor { +pub trait UsbDeviceInfo { /// A unique USB Device type Device; @@ -96,7 +96,6 @@ pub trait UsbInterface<'a> { async fn bulk_out(&self, endpoint: u8, data: &[u8]) -> Result; /* TODO: Figure out interrupt transfers on Web USB - /// A USB interrupt in transfer (device to host). /// Takes in an endpoint and a buffer to fill async fn interrupt_in(&self, endpoint: u8, length: usize) -> Result, UsbError>;