I'm examining rust to see if it suits my project requirements. One of my requirements is to watch for a USB insertion and removal which is identified by product id and vendor id. I found two crates libusb-rs and rusb. Both provide the following usage example:
fn main() {
for device in rusb::devices().unwrap().iter() {
let device_desc = device.device_descriptor().unwrap();
println!("Bus {:03} Device {:03} ID {:04x}:{:04x}",
device.bus_number(),
device.address(),
device_desc.vendor_id(),
device_desc.product_id());
}
}
This code iterates through all the system USB devices and list their properties. To watch for USB insertion and removal I can run the code every 500ms and check if the target USB is there or if it is removed. The thing is this method seems like a workaround to me and i think there should be some OS events that rust can hook to and makes USB detection more reliable and efficient. So what is the best way to do it in rust?