dcuddeback / libusb-rs

A safe Rust wrapper for libusb.
MIT License
199 stars 64 forks source link

Device method mutability? #6

Closed goertzenator closed 8 years ago

goertzenator commented 8 years ago

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());
}
kevinmehall commented 8 years ago

5 fixes this.

goertzenator commented 8 years ago

One step ahead of me, thanks. :) I'll give that a whirl today.