I note that all the methods for Device require &mut instead of &. Is this needed? Can we consider those methods to be interior mutable and use a constant reference instead?
Motivation: I am trying to use Iterator::find() to locate a device of interest, and the predicate function must take a constant reference as a parameter. I need to call device_descriptor() inside the predicate but cannot without a mutable reference.
extern crate libusb;
fn check_device(device: &mut libusb::Device) -> bool {
let device_desc = device.device_descriptor().unwrap();
(device_desc.vendor_id() == 0xffff) && (device_desc.product_id() == 3)
}
fn main() {
let mut context = libusb::Context::new().unwrap();
let mut device = context.devices().unwrap().iter().find(check_device).unwrap(); // error: check_device must take immutable ref.
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());
}
I note that all the methods for
Device
require&mut
instead of&
. Is this needed? Can we consider those methods to be interior mutable and use a constant reference instead?Motivation: I am trying to use
Iterator::find()
to locate a device of interest, and the predicate function must take a constant reference as a parameter. I need to calldevice_descriptor()
inside the predicate but cannot without a mutable reference.