From 9a72bd661dc898623b7c84f3252fa31caa6bf257 Mon Sep 17 00:00:00 2001 From: Ilya Averyanov Date: Mon, 30 Aug 2021 17:30:10 +0300 Subject: [PATCH] ConfigDescriptor and InterfaceDescriptor extra return just slice Close #90 --- examples/list_devices.rs | 8 ++++---- src/config_descriptor.rs | 9 +++------ src/interface_descriptor.rs | 9 +++------ 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/examples/list_devices.rs b/examples/list_devices.rs index d4c9a15..21744b5 100644 --- a/examples/list_devices.rs +++ b/examples/list_devices.rs @@ -149,8 +149,8 @@ fn print_config(config_desc: &ConfigDescriptor, handle: &mut Opti println!(" Remote Wakeup {:>5}", config_desc.remote_wakeup()); println!(" bMaxPower {:4}mW", config_desc.max_power()); - if let Some(extra) = config_desc.extra() { - println!(" {:?}", extra); + if !config_desc.extra().is_empty() { + println!(" {:?}", config_desc.extra()); } else { println!(" no extra data"); } @@ -194,8 +194,8 @@ fn print_interface( .unwrap_or(String::new())) ); - if let Some(extra) = interface_desc.extra() { - println!(" {:?}", extra); + if interface_desc.extra().is_empty() { + println!(" {:?}", interface_desc.extra()); } else { println!(" no extra data"); } diff --git a/src/config_descriptor.rs b/src/config_descriptor.rs index 11fd957..c3ca071 100644 --- a/src/config_descriptor.rs +++ b/src/config_descriptor.rs @@ -71,14 +71,11 @@ impl ConfigDescriptor { } /// Returns the unknown 'extra' bytes that libusb does not understand. - pub fn extra(&self) -> Option<&[u8]> { + pub fn extra(&self) -> &[u8] { unsafe { match (*self.descriptor).extra_length { - len if len > 0 => Some(slice::from_raw_parts( - (*self.descriptor).extra, - len as usize, - )), - _ => None, + len if len > 0 => slice::from_raw_parts((*self.descriptor).extra, len as usize), + _ => &[], } } } diff --git a/src/interface_descriptor.rs b/src/interface_descriptor.rs index 87d3e46..56b155b 100644 --- a/src/interface_descriptor.rs +++ b/src/interface_descriptor.rs @@ -104,14 +104,11 @@ impl<'a> InterfaceDescriptor<'a> { } /// Returns the unknown 'extra' bytes that libusb does not understand. - pub fn extra(&self) -> Option<&[u8]> { + pub fn extra(&self) -> &[u8] { unsafe { match (*self.descriptor).extra_length { - len if len > 0 => Some(slice::from_raw_parts( - (*self.descriptor).extra, - len as usize, - )), - _ => None, + len if len > 0 => slice::from_raw_parts((*self.descriptor).extra, len as usize), + _ => &[], } } }