Skip to content

Commit

Permalink
Replace some Option<u8> to Option<NonZeroU8>
Browse files Browse the repository at this point in the history
manufacturer_string_index
serial_number_string_index
product_string_index
description_string_index

ref #88
  • Loading branch information
a1ien committed Jul 27, 2021
1 parent 009254b commit 6b15215
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 47 deletions.
19 changes: 14 additions & 5 deletions examples/list_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: UsbContext> {
Expand Down Expand Up @@ -101,23 +102,27 @@ fn print_device<T: UsbContext>(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)
.unwrap_or(String::new()))
);
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)
.unwrap_or(String::new()))
);
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)
Expand All @@ -138,7 +143,9 @@ fn print_config<T: UsbContext>(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)
Expand Down Expand Up @@ -187,7 +194,9 @@ fn print_interface<T: UsbContext>(
);
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)
Expand Down
13 changes: 5 additions & 8 deletions src/config_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -42,13 +43,8 @@ impl ConfigDescriptor {
}

/// Returns the index of the string descriptor that describes the configuration.
pub fn description_string_index(&self) -> Option<u8> {
unsafe {
match (*self.descriptor).iConfiguration {
0 => None,
n => Some(n),
}
}
pub fn description_string_index(&self) -> Option<NonZeroU8> {
NonZeroU8::new(unsafe { (*self.descriptor).iConfiguration })
}

/// Returns the number of interfaces for this configuration.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
});
}

Expand Down
37 changes: 18 additions & 19 deletions src/device_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use libusb1_sys::*;

use crate::fields::Version;
use std::num::NonZeroU8;

/// Describes a device.
pub struct DeviceDescriptor {
Expand All @@ -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<u8> {
match self.descriptor.iManufacturer {
0 => None,
n => Some(n),
}
pub fn manufacturer_string_index(&self) -> Option<NonZeroU8> {
NonZeroU8::new(self.descriptor.iManufacturer)
}

/// Returns the index of the string descriptor that contains the product name.
pub fn product_string_index(&self) -> Option<u8> {
match self.descriptor.iProduct {
0 => None,
n => Some(n),
}
pub fn product_string_index(&self) -> Option<NonZeroU8> {
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<u8> {
match self.descriptor.iSerialNumber {
0 => None,
n => Some(n),
}
pub fn serial_number_string_index(&self) -> Option<NonZeroU8> {
NonZeroU8::new(self.descriptor.iSerialNumber)
}

/// Returns the device's class code.
Expand Down Expand Up @@ -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()
);
}

Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand Down
24 changes: 15 additions & 9 deletions src/device_handle.rs
Original file line number Diff line number Diff line change
@@ -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::*, *};
Expand Down Expand Up @@ -726,7 +732,7 @@ impl<T: UsbContext> DeviceHandle<T> {
) -> crate::Result<String> {
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()),
}
}

Expand All @@ -739,15 +745,15 @@ impl<T: UsbContext> DeviceHandle<T> {
) -> crate::Result<String> {
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),
}
}

/// Reads the device's product string descriptor (ascii).
pub fn read_product_string_ascii(&self, device: &DeviceDescriptor) -> crate::Result<String> {
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()),
}
}

Expand All @@ -760,7 +766,7 @@ impl<T: UsbContext> DeviceHandle<T> {
) -> crate::Result<String> {
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),
}
}

Expand All @@ -771,7 +777,7 @@ impl<T: UsbContext> DeviceHandle<T> {
) -> crate::Result<String> {
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()),
}
}

Expand All @@ -784,7 +790,7 @@ impl<T: UsbContext> DeviceHandle<T> {
) -> crate::Result<String> {
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),
}
}

Expand All @@ -797,7 +803,7 @@ impl<T: UsbContext> DeviceHandle<T> {
) -> crate::Result<String> {
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),
}
}

Expand All @@ -810,7 +816,7 @@ impl<T: UsbContext> DeviceHandle<T> {
) -> crate::Result<String> {
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),
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/interface_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<u8> {
match self.descriptor.iInterface {
0 => None,
n => Some(n),
}
pub fn description_string_index(&self) -> Option<NonZeroU8> {
NonZeroU8::new(self.descriptor.iInterface)
}

/// Returns the number of endpoints belonging to this interface.
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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::<Vec<_>>()
);
}
Expand Down

0 comments on commit 6b15215

Please sign in to comment.