Skip to content

Commit

Permalink
Match by reference in item lookups.
Browse files Browse the repository at this point in the history
This is necessary because the upcoming HidDescriptor is not Copy.
  • Loading branch information
martinling committed Oct 22, 2024
1 parent 6a7ee56 commit 51ec581
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,9 +1934,9 @@ impl ItemSource<DeviceItem, DeviceViewMode> for CaptureReader {
{
use DeviceItemContent::*;
let data = self.device_data(parent.device_id)?;
let content = match parent.content {
let content = match &parent.content {
Device(desc_opt) => match index {
0 => DeviceDescriptor(desc_opt),
0 => DeviceDescriptor(*desc_opt),
n => {
let conf = ConfigNum(n.try_into()?);
let config = data.configuration(conf)?;
Expand All @@ -1949,72 +1949,76 @@ impl ItemSource<DeviceItem, DeviceViewMode> for CaptureReader {
},
DeviceDescriptor(desc_opt) => match desc_opt {
Some(desc) =>
DeviceDescriptorField(desc,
DeviceDescriptorField(*desc,
DeviceField(index.try_into()?)),
None => bail!("Device descriptor fields not available")
},
Configuration(conf, desc, class) => {
let config = data.configuration(conf)?;
let config = data.configuration(*conf)?;
let other_count = config.other_descriptors.len();
let func_count = config.functions.len();
match index.try_into()? {
0 => ConfigurationDescriptor(desc),
0 => ConfigurationDescriptor(*desc),
n if n < 1 + other_count =>
OtherDescriptor(
config
.other_descriptor(n - 1)?
.clone(),
class),
*class),
n if n < 1 + other_count + func_count =>
Function(conf, config
Function(*conf, config
.function(n - 1 - other_count)?
.descriptor),
n => Interface(conf, config
n => Interface(*conf, config
.unassociated_interfaces()
.nth(n - 1 - other_count - func_count)
.context("Failed to find unassociated interface")?
.descriptor)
}
},
ConfigurationDescriptor(desc) =>
ConfigurationDescriptorField(desc,
ConfigurationDescriptorField(*desc,
ConfigField(index.try_into()?)),
Function(conf, desc) => {
let config = data.configuration(conf)?;
let config = data.configuration(*conf)?;
match index.try_into()? {
0 => FunctionDescriptor(desc),
n => match config.associated_interfaces(&desc).nth(n - 1) {
0 => FunctionDescriptor(*desc),
n => match config.associated_interfaces(desc).nth(n - 1) {
Some(interface) =>
Interface(conf, interface.descriptor),
Interface(*conf, interface.descriptor),
None => bail!(
"Function has no interface with index {n}")
}
}
},
FunctionDescriptor(desc) =>
FunctionDescriptorField(desc,
FunctionDescriptorField(*desc,
IfaceAssocField(index.try_into()?)),
Interface(conf, if_desc) => {
let config = data.configuration(conf)?;
let config = data.configuration(*conf)?;
let interface = config.interface(if_desc.key())?;
let desc_count = interface.other_descriptors.len();
match index.try_into()? {
0 => InterfaceDescriptor(if_desc),
0 => InterfaceDescriptor(*if_desc),
n if n < 1 + desc_count => {
let desc = interface.other_descriptor(n - 1)?.clone();
OtherDescriptor(desc,
Some(interface.descriptor.interface_class))
if let Descriptor::Hid(hid_desc) = desc {
HidDescriptor(hid_desc.clone())
} else {
OtherDescriptor(desc,
Some(interface.descriptor.interface_class))
}
},
n => {
let ep_num = (n - 1 - desc_count).try_into()?;
Endpoint(conf, if_desc.key(), ep_num)
Endpoint(*conf, if_desc.key(), ep_num)
}
}
},
Endpoint(conf, if_key, ep_num) => {
let config = data.configuration(conf)?;
let interface = config.interface(if_key)?;
let endpoint = interface.endpoint(ep_num)?;
let config = data.configuration(*conf)?;
let interface = config.interface(*if_key)?;
let endpoint = interface.endpoint(*ep_num)?;
match index.try_into()? {
0 => EndpointDescriptor(endpoint.descriptor),
n => OtherDescriptor(
Expand All @@ -2027,10 +2031,10 @@ impl ItemSource<DeviceItem, DeviceViewMode> for CaptureReader {
}
},
InterfaceDescriptor(desc) =>
InterfaceDescriptorField(desc,
InterfaceDescriptorField(*desc,
InterfaceField(index.try_into()?)),
EndpointDescriptor(desc) =>
EndpointDescriptorField(desc,
EndpointDescriptorField(*desc,
EndpointField(index.try_into()?)),
_ => bail!("This device item type cannot have children")
};
Expand All @@ -2055,7 +2059,7 @@ impl ItemSource<DeviceItem, DeviceViewMode> for CaptureReader {
self.devices.len().saturating_sub(1) as usize),
Some(item) => {
let data = self.device_data(item.device_id)?;
match item.content {
match &item.content {
Device(_) => {
let count = data.configurations.load().len();
(Ongoing, if count == 0 { 1 } else { count })
Expand All @@ -2067,7 +2071,7 @@ impl ItemSource<DeviceItem, DeviceViewMode> for CaptureReader {
None => (Ongoing, 0),
},
Configuration(conf, ..) => {
let config = data.configuration(conf)?;
let config = data.configuration(*conf)?;
(Ongoing,
1 + config.other_descriptors.len()
+ config.functions.len()
Expand All @@ -2076,24 +2080,24 @@ impl ItemSource<DeviceItem, DeviceViewMode> for CaptureReader {
ConfigurationDescriptor(_) =>
(Ongoing, usb::ConfigDescriptor::NUM_FIELDS),
Function(conf, desc) => {
let config = data.configuration(conf)?;
let interfaces = config.associated_interfaces(&desc);
let config = data.configuration(*conf)?;
let interfaces = config.associated_interfaces(desc);
(Complete, 1 + interfaces.count())
}
FunctionDescriptor(_) =>
(Complete,
usb::InterfaceAssociationDescriptor::NUM_FIELDS),
Interface(conf, desc) => {
let config = data.configuration(conf)?;
let config = data.configuration(*conf)?;
let interface = config.interface(desc.key())?;
(Ongoing,
1 + interface.endpoints.len()
+ interface.other_descriptors.len())
},
Endpoint(conf, key, ep_num) => {
let config = data.configuration(conf)?;
let interface = config.interface(key)?;
let endpoint = interface.endpoint(ep_num)?;
let config = data.configuration(*conf)?;
let interface = config.interface(*key)?;
let endpoint = interface.endpoint(*ep_num)?;
(Complete, 1 + endpoint.other_descriptors.len())
}
InterfaceDescriptor(_) =>
Expand Down

0 comments on commit 51ec581

Please sign in to comment.