From 6b15215638d881bb52cc5a03b2e6c3162d0d0b39 Mon Sep 17 00:00:00 2001 From: Ilya Averyanov Date: Tue, 27 Jul 2021 12:46:55 +0300 Subject: [PATCH] Replace some Option to Option manufacturer_string_index serial_number_string_index product_string_index description_string_index ref #88 --- examples/list_devices.rs | 19 ++++++++++++++----- src/config_descriptor.rs | 13 +++++-------- src/device_descriptor.rs | 37 ++++++++++++++++++------------------- src/device_handle.rs | 24 +++++++++++++++--------- src/interface_descriptor.rs | 12 ++++++------ 5 files changed, 58 insertions(+), 47 deletions(-) diff --git a/examples/list_devices.rs b/examples/list_devices.rs index d4c9a15..1205603 100644 --- a/examples/list_devices.rs +++ b/examples/list_devices.rs @@ -2,6 +2,7 @@ use rusb::{ ConfigDescriptor, DeviceDescriptor, DeviceHandle, DeviceList, EndpointDescriptor, InterfaceDescriptor, Language, Result, Speed, UsbContext, }; +use std::num::NonZeroU8; use std::time::Duration; struct UsbDevice { @@ -101,7 +102,9 @@ fn print_device(device_desc: &DeviceDescriptor, handle: &mut Opti ); println!( " iManufacturer {:3} {}", - device_desc.manufacturer_string_index().unwrap_or(0), + device_desc + .manufacturer_string_index() + .map_or(0, NonZeroU8::get), handle.as_mut().map_or(String::new(), |h| h .handle .read_manufacturer_string(h.language, device_desc, h.timeout) @@ -109,7 +112,7 @@ fn print_device(device_desc: &DeviceDescriptor, handle: &mut Opti ); println!( " iProduct {:3} {}", - device_desc.product_string_index().unwrap_or(0), + device_desc.product_string_index().map_or(0, NonZeroU8::get), handle.as_mut().map_or(String::new(), |h| h .handle .read_product_string(h.language, device_desc, h.timeout) @@ -117,7 +120,9 @@ fn print_device(device_desc: &DeviceDescriptor, handle: &mut Opti ); println!( " iSerialNumber {:3} {}", - device_desc.serial_number_string_index().unwrap_or(0), + device_desc + .serial_number_string_index() + .map_or(0, NonZeroU8::get), handle.as_mut().map_or(String::new(), |h| h .handle .read_serial_number_string(h.language, device_desc, h.timeout) @@ -138,7 +143,9 @@ fn print_config(config_desc: &ConfigDescriptor, handle: &mut Opti println!(" bConfigurationValue {:3}", config_desc.number()); println!( " iConfiguration {:3} {}", - config_desc.description_string_index().unwrap_or(0), + config_desc + .description_string_index() + .map_or(0, NonZeroU8::get), handle.as_mut().map_or(String::new(), |h| h .handle .read_configuration_string(h.language, config_desc, h.timeout) @@ -187,7 +194,9 @@ fn print_interface( ); println!( " iInterface {:3} {}", - interface_desc.description_string_index().unwrap_or(0), + interface_desc + .description_string_index() + .map_or(0, NonZeroU8::get), handle.as_mut().map_or(String::new(), |h| h .handle .read_interface_string(h.language, interface_desc, h.timeout) diff --git a/src/config_descriptor.rs b/src/config_descriptor.rs index 11fd957..2c1d2a3 100644 --- a/src/config_descriptor.rs +++ b/src/config_descriptor.rs @@ -3,6 +3,7 @@ use std::{fmt, slice}; use libusb1_sys::*; use crate::interface_descriptor::{self, Interface}; +use std::num::NonZeroU8; /// Describes a configuration. pub struct ConfigDescriptor { @@ -42,13 +43,8 @@ impl ConfigDescriptor { } /// Returns the index of the string descriptor that describes the configuration. - pub fn description_string_index(&self) -> Option { - unsafe { - match (*self.descriptor).iConfiguration { - 0 => None, - n => Some(n), - } - } + pub fn description_string_index(&self) -> Option { + NonZeroU8::new(unsafe { (*self.descriptor).iConfiguration }) } /// Returns the number of interfaces for this configuration. @@ -130,6 +126,7 @@ pub(crate) unsafe fn from_libusb(config: *const libusb_config_descriptor) -> Con #[cfg(test)] mod test { + use super::*; use std::mem; // The Drop trait impl calls libusb_free_config_descriptor(), which would attempt to free @@ -185,7 +182,7 @@ mod test { #[test] fn it_has_description_string_index() { with_config!(config: config_descriptor!(iConfiguration: 42) => { - assert_eq!(Some(42), config.description_string_index()); + assert_eq!(Some(42), config.description_string_index().map(NonZeroU8::get)); }); } diff --git a/src/device_descriptor.rs b/src/device_descriptor.rs index 60a3582..329dd2d 100644 --- a/src/device_descriptor.rs +++ b/src/device_descriptor.rs @@ -3,6 +3,7 @@ use std::fmt; use libusb1_sys::*; use crate::fields::Version; +use std::num::NonZeroU8; /// Describes a device. pub struct DeviceDescriptor { @@ -21,27 +22,18 @@ impl DeviceDescriptor { } /// Returns the index of the string descriptor that contains the manufacturer name. - pub fn manufacturer_string_index(&self) -> Option { - match self.descriptor.iManufacturer { - 0 => None, - n => Some(n), - } + pub fn manufacturer_string_index(&self) -> Option { + NonZeroU8::new(self.descriptor.iManufacturer) } /// Returns the index of the string descriptor that contains the product name. - pub fn product_string_index(&self) -> Option { - match self.descriptor.iProduct { - 0 => None, - n => Some(n), - } + pub fn product_string_index(&self) -> Option { + NonZeroU8::new(self.descriptor.iProduct) } /// Returns the index of the string descriptor that contains the device's serial number. - pub fn serial_number_string_index(&self) -> Option { - match self.descriptor.iSerialNumber { - 0 => None, - n => Some(n), - } + pub fn serial_number_string_index(&self) -> Option { + NonZeroU8::new(self.descriptor.iSerialNumber) } /// Returns the device's class code. @@ -110,13 +102,14 @@ pub fn from_libusb(device: libusb_device_descriptor) -> DeviceDescriptor { #[cfg(test)] mod test { + use super::*; use crate::fields::Version; #[test] fn it_has_usb_version() { assert_eq!( Version::from_bcd(0x1234), - super::from_libusb(device_descriptor!(bcdUSB: 0x1234)).usb_version() + from_libusb(device_descriptor!(bcdUSB: 0x1234)).usb_version() ); } @@ -132,7 +125,9 @@ mod test { fn it_has_manufacturer_string_index() { assert_eq!( Some(42), - super::from_libusb(device_descriptor!(iManufacturer: 42)).manufacturer_string_index() + from_libusb(device_descriptor!(iManufacturer: 42)) + .manufacturer_string_index() + .map(NonZeroU8::get) ); } @@ -148,7 +143,9 @@ mod test { fn it_has_product_string_index() { assert_eq!( Some(42), - super::from_libusb(device_descriptor!(iProduct: 42)).product_string_index() + super::from_libusb(device_descriptor!(iProduct: 42)) + .product_string_index() + .map(NonZeroU8::get) ); } @@ -164,7 +161,9 @@ mod test { fn it_has_serial_number_string_index() { assert_eq!( Some(42), - super::from_libusb(device_descriptor!(iSerialNumber: 42)).serial_number_string_index() + super::from_libusb(device_descriptor!(iSerialNumber: 42)) + .serial_number_string_index() + .map(NonZeroU8::get) ); } diff --git a/src/device_handle.rs b/src/device_handle.rs index b3ee37e..3032535 100644 --- a/src/device_handle.rs +++ b/src/device_handle.rs @@ -1,4 +1,10 @@ -use std::{mem, fmt::{self, Debug}, ptr::NonNull, time::Duration, u8}; +use std::{ + fmt::{self, Debug}, + mem, + ptr::NonNull, + time::Duration, + u8, +}; use libc::{c_int, c_uchar, c_uint}; use libusb1_sys::{constants::*, *}; @@ -726,7 +732,7 @@ impl DeviceHandle { ) -> crate::Result { match device.manufacturer_string_index() { None => Err(Error::InvalidParam), - Some(n) => self.read_string_descriptor_ascii(n), + Some(n) => self.read_string_descriptor_ascii(n.get()), } } @@ -739,7 +745,7 @@ impl DeviceHandle { ) -> crate::Result { match device.manufacturer_string_index() { None => Err(Error::InvalidParam), - Some(n) => self.read_string_descriptor(language, n, timeout), + Some(n) => self.read_string_descriptor(language, n.get(), timeout), } } @@ -747,7 +753,7 @@ impl DeviceHandle { pub fn read_product_string_ascii(&self, device: &DeviceDescriptor) -> crate::Result { match device.product_string_index() { None => Err(Error::InvalidParam), - Some(n) => self.read_string_descriptor_ascii(n), + Some(n) => self.read_string_descriptor_ascii(n.get()), } } @@ -760,7 +766,7 @@ impl DeviceHandle { ) -> crate::Result { match device.product_string_index() { None => Err(Error::InvalidParam), - Some(n) => self.read_string_descriptor(language, n, timeout), + Some(n) => self.read_string_descriptor(language, n.get(), timeout), } } @@ -771,7 +777,7 @@ impl DeviceHandle { ) -> crate::Result { match device.serial_number_string_index() { None => Err(Error::InvalidParam), - Some(n) => self.read_string_descriptor_ascii(n), + Some(n) => self.read_string_descriptor_ascii(n.get()), } } @@ -784,7 +790,7 @@ impl DeviceHandle { ) -> crate::Result { match device.serial_number_string_index() { None => Err(Error::InvalidParam), - Some(n) => self.read_string_descriptor(language, n, timeout), + Some(n) => self.read_string_descriptor(language, n.get(), timeout), } } @@ -797,7 +803,7 @@ impl DeviceHandle { ) -> crate::Result { match configuration.description_string_index() { None => Err(Error::InvalidParam), - Some(n) => self.read_string_descriptor(language, n, timeout), + Some(n) => self.read_string_descriptor(language, n.get(), timeout), } } @@ -810,7 +816,7 @@ impl DeviceHandle { ) -> crate::Result { match interface.description_string_index() { None => Err(Error::InvalidParam), - Some(n) => self.read_string_descriptor(language, n, timeout), + Some(n) => self.read_string_descriptor(language, n.get(), timeout), } } } diff --git a/src/interface_descriptor.rs b/src/interface_descriptor.rs index 87d3e46..e23e288 100644 --- a/src/interface_descriptor.rs +++ b/src/interface_descriptor.rs @@ -3,6 +3,7 @@ use std::{fmt, slice}; use libusb1_sys::{libusb_endpoint_descriptor, libusb_interface, libusb_interface_descriptor}; use crate::endpoint_descriptor::{self, EndpointDescriptor}; +use std::num::NonZeroU8; /// A device interface. /// @@ -77,11 +78,8 @@ impl<'a> InterfaceDescriptor<'a> { } /// Returns the index of the string descriptor that describes the interface. - pub fn description_string_index(&self) -> Option { - match self.descriptor.iInterface { - 0 => None, - n => Some(n), - } + pub fn description_string_index(&self) -> Option { + NonZeroU8::new(self.descriptor.iInterface) } /// Returns the number of endpoints belonging to this interface. @@ -165,6 +163,8 @@ pub(crate) unsafe fn from_libusb(interface: &libusb_interface) -> Interface { #[cfg(test)] mod test { + use super::*; + #[test] fn it_has_interface_number() { assert_eq!( @@ -241,7 +241,7 @@ mod test { vec!(Some(42)), unsafe { super::from_libusb(&interface!(interface_descriptor!(iInterface: 42))) } .descriptors() - .map(|setting| setting.description_string_index()) + .map(|setting| setting.description_string_index().map(NonZeroU8::get)) .collect::>() ); }