Skip to content

Commit

Permalink
refactor(custom_fc): implement copy trait for CFC
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoehler-dev committed Feb 23, 2024
1 parent c0261cd commit 5262d87
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 77 deletions.
2 changes: 1 addition & 1 deletion rodbus/examples/custom_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl RequestHandler for SimpleHandler {
}
}

fn process_cfc_69(&mut self, values: &CustomFunctionCode<u16>) -> Result<CustomFunctionCode<u16>, ExceptionCode> {
fn process_cfc_69(&mut self, values: CustomFunctionCode<u16>) -> Result<CustomFunctionCode<u16>, ExceptionCode> {
tracing::info!("processing custom function code: {}", values.function_code());
// increment each CFC value by 1 and return the result
// Create a new vector to hold the incremented values
Expand Down
4 changes: 2 additions & 2 deletions rodbus/src/client/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ impl Channel {
pub async fn send_custom_function_code(
&mut self,
param: RequestParam,
request: CustomFunctionCode<u16>,
) -> Result<CustomFunctionCode<u16>, RequestError> {
request: CustomFunctionCode,
) -> Result<CustomFunctionCode, RequestError> {
let (tx, rx) = tokio::sync::oneshot::channel::<Result<CustomFunctionCode<u16>, RequestError>>();
let request = wrap(
param,
Expand Down
2 changes: 1 addition & 1 deletion rodbus/src/client/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) enum RequestDetails {
WriteSingleRegister(SingleWrite<Indexed<u16>>),
WriteMultipleCoils(MultipleWriteRequest<bool>),
WriteMultipleRegisters(MultipleWriteRequest<u16>),
SendCustomFunctionCode(CustomFCRequest<CustomFunctionCode<u16>>),
SendCustomFunctionCode(CustomFCRequest<CustomFunctionCode>),
}

impl Request {
Expand Down
2 changes: 1 addition & 1 deletion rodbus/src/client/requests/send_custom_fc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
}
}

impl CustomFCOperation for CustomFunctionCode<u16> {
impl CustomFCOperation for CustomFunctionCode {
fn serialize(&self, cursor: &mut WriteCursor) -> Result<(), RequestError> {
cursor.write_u8(self.function_code())?;

Expand Down
2 changes: 1 addition & 1 deletion rodbus/src/common/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Parse for Indexed<u16> {
}
}

impl Parse for CustomFunctionCode<u16> {
impl Parse for CustomFunctionCode {
fn parse(cursor: &mut ReadCursor) -> Result<Self, RequestError> {
let fc = cursor.read_u8()?;
let len = cursor.remaining() / 2;
Expand Down
12 changes: 6 additions & 6 deletions rodbus/src/common/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl Serialize for WriteMultiple<u16> {
}
}

impl Serialize for &CustomFunctionCode<u16> {
impl Serialize for CustomFunctionCode {
fn serialize(&self, cursor: &mut WriteCursor) -> Result<(), RequestError> {
cursor.write_u8(self.function_code())?;

Expand All @@ -301,7 +301,7 @@ impl Serialize for &CustomFunctionCode<u16> {
}
}

impl Loggable for &CustomFunctionCode<u16> {
impl Loggable for CustomFunctionCode {
fn log(
&self,
payload: &[u8],
Expand Down Expand Up @@ -353,7 +353,7 @@ mod tests {

#[test]
fn serialize_succeeds_for_valid_cfc_of_single_min_value() {
let custom_fc = &CustomFunctionCode::new(1, vec![0x0000]);
let custom_fc = CustomFunctionCode::new(1, vec![0x0000]);
let mut buffer = [0u8; 4];
let mut cursor = WriteCursor::new(&mut buffer);
custom_fc.serialize(&mut cursor).unwrap();
Expand All @@ -362,7 +362,7 @@ mod tests {

#[test]
fn serialize_succeeds_for_valid_cfc_of_single_max_value() {
let custom_fc = &CustomFunctionCode::new(1, vec![0xFFFF]);
let custom_fc = CustomFunctionCode::new(1, vec![0xFFFF]);
let mut buffer = [0u8; 4];
let mut cursor = WriteCursor::new(&mut buffer);
custom_fc.serialize(&mut cursor).unwrap();
Expand All @@ -371,7 +371,7 @@ mod tests {

#[test]
fn serialize_succeeds_for_valid_cfc_of_multiple_min_values() {
let custom_fc = &CustomFunctionCode::new(3, vec![0x0000, 0x0000, 0x0000]);
let custom_fc = CustomFunctionCode::new(3, vec![0x0000, 0x0000, 0x0000]);
let mut buffer = [0u8; 8];
let mut cursor = WriteCursor::new(&mut buffer);
custom_fc.serialize(&mut cursor).unwrap();
Expand All @@ -380,7 +380,7 @@ mod tests {

#[test]
fn serialize_succeeds_for_valid_cfc_of_multiple_max_values() {
let custom_fc = &CustomFunctionCode::new(3, vec![0xFFFF, 0xFFFF, 0xFFFF]);
let custom_fc = CustomFunctionCode::new(3, vec![0xFFFF, 0xFFFF, 0xFFFF]);
let mut buffer = [0u8; 8];
let mut cursor = WriteCursor::new(&mut buffer);
custom_fc.serialize(&mut cursor).unwrap();
Expand Down
Loading

0 comments on commit 5262d87

Please sign in to comment.