Skip to content

Commit

Permalink
ConfigDescriptor and InterfaceDescriptor extra return just slice
Browse files Browse the repository at this point in the history
Close #90
  • Loading branch information
a1ien committed Aug 30, 2021
1 parent 73a4808 commit 9a72bd6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
8 changes: 4 additions & 4 deletions examples/list_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ fn print_config<T: UsbContext>(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");
}
Expand Down Expand Up @@ -194,8 +194,8 @@ fn print_interface<T: UsbContext>(
.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");
}
Expand Down
9 changes: 3 additions & 6 deletions src/config_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
_ => &[],
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/interface_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
_ => &[],
}
}
}
Expand Down

0 comments on commit 9a72bd6

Please sign in to comment.