$ show kevinmehall/nusb
nusb is a pure-Rust cross-platform library for low-level USB device access. It provides APIs for enumerating USB devices, opening them, claiming interfaces, and performing control, bulk, and interrupt transfers. The library is designed with an async-first approach while also supporting blocking usage through a custom `MaybeFuture` trait that can be waited on or awaited. Architecture is split into platform-specific backends (Linux usbfs, macOS IOKit, Windows WinUSB, WebUSB) with a common abstraction layer. Key features include zero-copy buffer allocation (Linux), hotplug event streams, fuzz testing for descriptor parsing, and high-level buffered IO adapters implementing standard `Read`/`Write` traits. The codebase is well-structured, thoroughly documented, and demonstrates high craftsmanship in areas like async transfer lifecycle management and cross-platform abstraction.
The LLM identified 3 files worth highlighting for reuse:
Implements a generic transfer lifecycle with `Idle` and `Pending` states, safe cancellation, and a `Notify` mechanism for waking async or blocking waiters. This pattern is cleanly separated and could be reused for any queue-based async operation that needs cancel safety and optional thread parking.
Provides the `MaybeFuture` trait and `Blocking` wrapper that allow library functions to be called either synchronously (`.wait()`) or asynchronously (`.await`). The `Blocking` type dispatches to a background thread pool when awaited (via `smol` or `tokio` features) or runs in place when called synchronously. This pattern is a reusable solution for libraries that want to offer both sync and async APIs without forcing a runtime.
Implements `EndpointRead`, a high-level buffered reader that manages multiple concurrent USB transfers to maximize throughput. It provides `std::io::Read` and `BufRead` implementations (blocking) and async versions for tokio and smol runtimes. Includes `EndpointReadUntilShortPacket` adapter for protocols that use short packets as message delimiters. This pattern demonstrates how to build efficient streaming IO on top of a queue-based transfer API and is adaptable to other hardware streaming scenarios.