Skip to content

Commit 8f5e0f7

Browse files
authored
Use new NullStr based SpComponent (#2545)
Codegen impl changes necessary to support oxidecomputer/management-gateway-service#496.
1 parent 64e146f commit 8f5e0f7

5 files changed

Lines changed: 29 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

task/control-plane-agent/src/inventory.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,21 @@ impl Inventory {
129129
if !device.sensors.is_empty() {
130130
capabilities |= DeviceCapabilities::HAS_MEASUREMENT_CHANNELS;
131131
}
132+
133+
// NOTE: the `from_bstr_unchecked` method expects that:
134+
//
135+
// 1. The given bytes contain utf-8 data
136+
// 2. The given slice is <= SpComponent::MAX_ID_LENGTH
137+
//
138+
// Since we pass the bytes of a `str` (always good utf-8!), and our
139+
// `str`s are built (and length-checked) at compile time, use of this
140+
// method is justified. You don't see an unsafe block here, because
141+
// SpComponent can be received over the wire, so even if we violated
142+
// the rules above, there would be no potential soundness concerns.
143+
let component = SpComponent::from_bstr_unchecked(device.id.as_bytes());
144+
132145
DeviceDescription {
133-
component: SpComponent { id: device.id },
146+
component,
134147
device: device.device,
135148
description: device.description,
136149
capabilities,
@@ -178,8 +191,13 @@ impl TryFrom<&'_ SpComponent> for Index {
178191
type Error = SpError;
179192

180193
fn try_from(component: &'_ SpComponent) -> Result<Self, Self::Error> {
194+
// TODO(AJM): implement PartialEq/PartialOrd for `SpComponent` et. al,
195+
// then make this nicer. We'll want this for some follow-up PMBus
196+
// changes as well.
181197
if let Ok(entry_idx) = task_validate_api::DEVICE_INDICES_BY_SORTED_ID
182-
.binary_search_by_key(&component.id, |&(id, _)| id)
198+
.binary_search_by_key(&component.as_bstr(), |&(id, _)| {
199+
id.as_bytes()
200+
})
183201
{
184202
let &(_, index) = task_validate_api::DEVICE_INDICES_BY_SORTED_ID
185203
.get(entry_idx)

task/control-plane-agent/src/update/rot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl ComponentUpdater for RotUpdate {
7878
.map_err(SpError::OtherComponentUpdateInProgress)?;
7979

8080
// Which target are we updating?
81-
ringbuf_entry!(Trace::Target(update.component.id[0], update.slot));
81+
ringbuf_entry!(Trace::Target(update.component.id()[0], update.slot));
8282
let target = match (update.component, update.slot) {
8383
(SpComponent::ROT, 0) => UpdateTarget::ImageA,
8484
(SpComponent::ROT, 1) => UpdateTarget::ImageB,

task/validate-api/build.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ fn write_pub_device_descriptions() -> anyhow::Result<()> {
6363
writeln!(file, " DeviceDescription {{")?;
6464
writeln!(file, " device: {:?},", dev.device)?;
6565
writeln!(file, " description: {:?},", dev.description)?;
66-
if let Some(id) = dev.device_id {
67-
if let Ok(component) = SpComponent::try_from(id.as_ref()) {
68-
writeln!(file, " id: {:?},", component.id)?;
69-
if id2idx.insert(component.id, idx).is_some() {
66+
if let Some(id) = dev.device_id.as_ref() {
67+
if id.len() <= SpComponent::MAX_ID_LENGTH {
68+
writeln!(file, " id: \"{id}\",")?;
69+
if id2idx.insert(id.to_string(), idx).is_some() {
7070
println!("cargo::error=duplicate device id {id:?}",);
7171
duplicate_ids += 1;
7272
}
@@ -106,11 +106,11 @@ fn write_pub_device_descriptions() -> anyhow::Result<()> {
106106

107107
writeln!(
108108
file,
109-
"pub static DEVICE_INDICES_BY_SORTED_ID: [([u8; MAX_ID_LENGTH], usize); {}] = [",
109+
"pub static DEVICE_INDICES_BY_SORTED_ID: [(&str, usize); {}] = [",
110110
id2idx.len()
111111
)?;
112112
for (id, idx) in id2idx {
113-
writeln!(file, " ({id:?}, {idx}),")?;
113+
writeln!(file, " (\"{id}\", {idx}),")?;
114114
}
115115
writeln!(file, "];")?;
116116

task/validate-api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct DeviceDescription {
7474
pub device: &'static str,
7575
pub description: &'static str,
7676
pub sensors: &'static [SensorDescription],
77-
pub id: [u8; MAX_ID_LENGTH],
77+
pub id: &'static str,
7878
pub is_pmbus: bool,
7979
}
8080

0 commit comments

Comments
 (0)