Skip to content

Commit 3efdb54

Browse files
committed
a more applied doc list_buses example
1 parent a0d5066 commit 3efdb54

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ pub fn list_devices() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
153153

154154
/// Get an iterator listing the system USB buses.
155155
///
156-
/// ### Example
156+
/// ### Examples
157+
///
158+
/// Filter for a specific bus ID:
157159
///
158160
/// ```no_run
159161
/// use nusb;
@@ -162,6 +164,20 @@ pub fn list_devices() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
162164
/// .expect("bus #1 not found");
163165
/// ```
164166
///
167+
/// Group devices by bus:
168+
///
169+
/// ```no_run
170+
/// use std::collections::HashMap;
171+
///
172+
/// let devices = nusb::list_devices().unwrap().collect::<Vec<_>>();
173+
/// let buses: HashMap<String, (nusb::BusInfo, Vec::<nusb::DeviceInfo>)> = nusb::list_buses().unwrap()
174+
/// .map(|bus| {
175+
/// let bus_id = bus.bus_id().to_owned();
176+
/// (bus.bus_id().to_owned(), (bus, devices.clone().into_iter().filter(|dev| dev.bus_id() == bus_id).collect()))
177+
/// })
178+
/// .collect();
179+
/// ```
180+
///
165181
/// ### Platform-specific notes
166182
/// * On Linux, the abstraction of the "bus" is a phony device known as the root hub. This device is available at bus.root_hub()
167183
pub fn list_buses() -> Result<impl Iterator<Item = BusInfo>, Error> {

0 commit comments

Comments
 (0)