Skip to content

Commit

Permalink
Using Cow for RegistryCharacteristic and RegistryMeasurement name
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerZ committed Nov 25, 2024
1 parent f30190e commit 087bb69
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 54 deletions.
2 changes: 1 addition & 1 deletion examples/protobuf_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn main() -> Result<()> {
xcp.get_registry()
.lock()
.add_measurement(RegistryMeasurement::new(
"test_data".to_string(),
"test_data",
RegistryDataType::Blob,
1,
1,
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,22 @@ macro_rules! cal_register_static {
let name = stringify!($variable);
let datatype = ($variable).get_type();
let addr = &($variable) as *const _ as u64;
let c = RegistryCharacteristic::new(None, name.to_string(), datatype, "", datatype.get_min(), datatype.get_max(), "", 1, 1, addr);
let c = RegistryCharacteristic::new(None, name, datatype, "", datatype.get_min(), datatype.get_max(), "", 1, 1, addr);
Xcp::get().get_registry().lock().add_characteristic(c).expect("Duplicate");
}};
( $variable:expr, $comment:expr ) => {{
let name = stringify!($variable);
let datatype = ($variable).get_type();
let addr = &($variable) as *const _ as u64;
let c = RegistryCharacteristic::new(None, name.to_string(), datatype, $comment, datatype.get_min(), datatype.get_max(), "", 1, 1, addr);
let c = RegistryCharacteristic::new(None, name, datatype, $comment, datatype.get_min(), datatype.get_max(), "", 1, 1, addr);
Xcp::get().get_registry().lock().add_characteristic(c).expect("Duplicate");
}};

( $variable:expr, $comment:expr, $unit:expr ) => {{
let name = stringify!($variable);
let datatype = ($variable).get_type();
let addr = &($variable) as *const _ as u64;
let c = RegistryCharacteristic::new(None, name.to_string(), datatype, $comment, datatype.get_min(), datatype.get_max(), $unit, 1, 1, addr);
let c = RegistryCharacteristic::new(None, name, datatype, $comment, datatype.get_min(), datatype.get_max(), $unit, 1, 1, addr);
Xcp::get().get_registry().lock().add_characteristic(c).expect("Duplicate");
}};
}
Expand All @@ -159,15 +159,15 @@ macro_rules! daq_register_static {
let name = stringify!($variable);
let datatype = ($variable).get_type();
let addr = &($variable) as *const _ as u64;
let mut c = RegistryCharacteristic::new(None, name.to_string(), datatype, "", datatype.get_min(), datatype.get_max(), "", 1, 1, addr);
let mut c = RegistryCharacteristic::new(None, name, datatype, "", datatype.get_min(), datatype.get_max(), "", 1, 1, addr);
c.set_event($event);
Xcp::get().get_registry().lock().add_characteristic(c).expect("Duplicate");
}};
( $variable:expr, $event:ident, $comment:expr ) => {{
let name = stringify!($variable);
let datatype = ($variable).get_type();
let addr = &($variable) as *const _ as u64;
let mut c = RegistryCharacteristic::new(None, name.to_string(), datatype, $comment, datatype.get_min(), datatype.get_max(), "", 1, 1, addr);
let mut c = RegistryCharacteristic::new(None, name, datatype, $comment, datatype.get_min(), datatype.get_max(), "", 1, 1, addr);
c.set_event($event);
Xcp::get().get_registry().lock().add_characteristic(c).expect("Duplicate");
}};
Expand All @@ -176,7 +176,7 @@ macro_rules! daq_register_static {
let name = stringify!($variable);
let datatype = ($variable).get_type();
let addr = &($variable) as *const _ as u64;
let mut c = RegistryCharacteristic::new(None, name.to_string(), datatype, $comment, datatype.get_min(), datatype.get_max(), $unit, 1, 1, addr);
let mut c = RegistryCharacteristic::new(None, name, datatype, $comment, datatype.get_min(), datatype.get_max(), $unit, 1, 1, addr);
c.set_event($event);
Xcp::get().get_registry().lock().add_characteristic(c).expect("Duplicate");
}};
Expand Down
16 changes: 8 additions & 8 deletions src/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod registry_tests {

reg.add_characteristic(RegistryCharacteristic::new(
Some("test_cal_seg_1"),
"test_characteristic_1".to_string(),
"test_characteristic_1",
crate::RegistryDataType::Sbyte,
"comment",
-128.0,
Expand All @@ -53,7 +53,7 @@ mod registry_tests {
.unwrap();
reg.add_characteristic(RegistryCharacteristic::new(
Some("test_cal_seg_1"),
"test_characteristic_2".to_string(),
"test_characteristic_2",
crate::RegistryDataType::Sbyte,
"comment",
-128.0,
Expand All @@ -66,7 +66,7 @@ mod registry_tests {
.unwrap();

reg.add_measurement(RegistryMeasurement::new(
"test_measurement_1".to_string(),
"test_measurement_1",
crate::RegistryDataType::Ubyte,
1,
1,
Expand All @@ -82,7 +82,7 @@ mod registry_tests {
.unwrap();

reg.add_measurement(RegistryMeasurement::new(
"test_measurement_1".to_string(),
"test_measurement_1",
crate::RegistryDataType::Ubyte,
1,
1,
Expand All @@ -98,7 +98,7 @@ mod registry_tests {
.unwrap();

reg.add_measurement(RegistryMeasurement::new(
"test_measurement_2".to_string(),
"test_measurement_2",
crate::RegistryDataType::Ubyte,
1,
1,
Expand Down Expand Up @@ -182,7 +182,7 @@ mod registry_tests {
let mut reg = reg_ref.lock();

reg.add_measurement(RegistryMeasurement::new(
"test_measurement_1".to_string(),
"test_measurement_1",
crate::RegistryDataType::Ubyte,
1,
1,
Expand All @@ -198,7 +198,7 @@ mod registry_tests {
.unwrap();

reg.add_measurement(RegistryMeasurement::new(
"test_measurement_1".to_string(),
"test_measurement_1",
crate::RegistryDataType::Ubyte,
1,
1,
Expand All @@ -214,7 +214,7 @@ mod registry_tests {
.unwrap();

reg.add_measurement(RegistryMeasurement::new(
"test_measurement_2".to_string(),
"test_measurement_2",
crate::RegistryDataType::Ubyte,
1,
1,
Expand Down
28 changes: 14 additions & 14 deletions src/reg/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#![allow(dead_code)]

use core::panic;
use std::net::Ipv4Addr;

#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};

use core::panic;
use std::{borrow::Cow, net::Ipv4Addr};

use crate::xcp;
use xcp::XcpEvent;

Expand All @@ -27,7 +27,7 @@ pub enum RegistryError {
Io(#[from] std::io::Error),

#[error("registry error: duplicate symbol `{0}` ")]
Duplicate(String),
Duplicate(Cow<'static, str>),

#[error("registry error: `{0}` not found")]
NotFound(&'static str),
Expand Down Expand Up @@ -409,7 +409,7 @@ impl RegistryEpk {
/// Used by the register macros
#[derive(Clone, Debug)]
pub struct RegistryMeasurement {
name: String,
name: Cow<'static, str>,
// Type
datatype: RegistryDataType, // Basic types Ubyte, SByte, AUint64, Float64Ieee, ... or Blob
x_dim: u16, // 1 = basic type (A2L MEASUREMENT), >1 = array[dim] of basic type (A2L MEASUREMENT with MATRIX_DIM x (max u16))
Expand All @@ -430,7 +430,7 @@ impl RegistryMeasurement {
/// Create a new measurement signal
#[allow(clippy::too_many_arguments)]
pub fn new(
name: String,
name: &'static str,
datatype: RegistryDataType,
x_dim: u16,
y_dim: u16,
Expand All @@ -445,7 +445,7 @@ impl RegistryMeasurement {
) -> Self {
assert!((x_dim as usize * y_dim as usize) * datatype.get_size() <= u16::MAX as usize / 2);
RegistryMeasurement {
name,
name: name.into(),
datatype,
x_dim,
y_dim,
Expand Down Expand Up @@ -482,7 +482,7 @@ impl RegistryMeasurementList {
}

fn sort(&mut self) {
self.0.sort_by(|a, b| a.name.as_str().cmp(b.name.as_str()));
self.0.sort_by(|a, b| a.name.cmp(&b.name));
}
}

Expand All @@ -493,7 +493,7 @@ impl RegistryMeasurementList {
/// Used by the register macros
#[derive(Clone, Debug)]
pub struct RegistryCharacteristic {
name: String,
name: Cow<'static, str>,
// Type
datatype: RegistryDataType,
x_dim: usize,
Expand All @@ -513,9 +513,9 @@ pub struct RegistryCharacteristic {
#[allow(clippy::too_many_arguments)]
impl RegistryCharacteristic {
/// Create a new calibration parameter
pub fn new(
pub fn new<T: std::convert::Into<Cow<'static, str>>>(
calseg_name: Option<&'static str>,
name: String,
name: T,
datatype: RegistryDataType,
comment: &'static str,
min: f64,
Expand All @@ -527,7 +527,7 @@ impl RegistryCharacteristic {
) -> Self {
RegistryCharacteristic {
calseg_name,
name,
name: name.into(),
datatype,
comment,
min,
Expand Down Expand Up @@ -571,7 +571,7 @@ impl RegistryCharacteristicList {
}

pub fn sort(&mut self) {
self.0.sort_by(|a, b| a.name.as_str().cmp(b.name.as_str()));
self.0.sort_by(|a, b| a.name.cmp(&b.name));
}

pub fn iter(&self) -> std::slice::Iter<RegistryCharacteristic> {
Expand Down Expand Up @@ -737,7 +737,7 @@ impl Registry {

// Append event index to name in case of a multi instance event (index>0)
if m.xcp_event.get_index() > 0 {
m.name = format!("{}_{}", m.name, m.xcp_event.get_index());
m.name = std::borrow::Cow::Owned(format!("{}_{}", m.name, m.xcp_event.get_index()));
}

// Panic if symbol_name with same name already exists
Expand Down
2 changes: 1 addition & 1 deletion src/reg/registry/a2l_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl<'a> A2lWriter<'a> {
for c in self.registry.characteristic_list.iter() {
if let Some(calseg_name) = c.calseg_name {
if s.name == calseg_name {
write!(self, " {} ", c.name.as_str())?;
write!(self, " {} ", c.name)?;
}
}
}
Expand Down
30 changes: 6 additions & 24 deletions src/xcp/daq/daq_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ impl Xcp {
.get_registry()
.lock()
.add_measurement(RegistryMeasurement::new(
name.to_string(),
data_type,
x_dim,
y_dim,
event,
0, // byte_offset
0,
1.0, // factor
name, data_type, x_dim, y_dim, event, 0, // byte_offset
0, 1.0, // factor
0.0, // offset
comment,
"", // unit
comment, "", // unit
None,
))
.is_err()
Expand Down Expand Up @@ -156,7 +149,7 @@ impl<const N: usize> DaqEvent<N> {
.get_registry()
.lock()
.add_measurement(RegistryMeasurement::new(
name.to_string(),
name,
datatype,
x_dim,
y_dim,
Expand Down Expand Up @@ -188,7 +181,7 @@ impl<const N: usize> DaqEvent<N> {
.get_registry()
.lock()
.add_measurement(RegistryMeasurement::new(
name.to_string(),
name,
datatype,
x_dim,
y_dim,
Expand Down Expand Up @@ -216,18 +209,7 @@ impl<const N: usize> DaqEvent<N> {
.get_registry()
.lock()
.add_measurement(RegistryMeasurement::new(
name.to_string(),
datatype,
x_dim,
y_dim,
self.event,
0i16,
ptr as u64,
factor,
offset,
comment,
unit,
None,
name, datatype, x_dim, y_dim, self.event, 0i16, ptr as u64, factor, offset, comment, unit, None,
))
.is_err()
{
Expand Down

0 comments on commit 087bb69

Please sign in to comment.