Skip to content

Commit

Permalink
Update to zerocopy 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
birkenfeld committed Nov 8, 2024
1 parent cec254a commit 2972212
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 116 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ rust-version = "1.63"
byteorder = "1.5.0"
crossbeam-channel = "0.5.13"
itertools = "0.13.0"
thiserror = "1.0.63"
zerocopy = { version = "0.7.35", features = ["derive"] }
thiserror = "2.0"
zerocopy = { version = "0.8.9", features = ["derive"] }

[dev-dependencies]
once_cell = "1.19.0"
once_cell = "1.20.2"
parse_int = "0.6.0"
quick-xml = "0.36.1"
quick-xml = "0.37.0"
regex = "<1.10.0"
chrono = "0.4.38"
clap = { version = "3.2.25", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions examples/values_via_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! by redefining it as a Rust struct with zerocopy traits.
use ads::{Client, Source, Timeouts, symbol::Handle};
use zerocopy::{AsBytes, FromZeroes, FromBytes};
use zerocopy::{FromBytes, IntoBytes};

#[derive(Default, AsBytes, FromZeroes, FromBytes)]
#[derive(Default, FromBytes, IntoBytes)]
#[repr(packed)]
struct Motor {
position: f32,
Expand Down
142 changes: 71 additions & 71 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::errors::{ads_error, ErrContext};
use crate::notif;
use crate::{AmsAddr, AmsNetId, Error, Result};

use zerocopy::byteorder::{U16, U32};
use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::byteorder::little_endian::{U16, U32};
use zerocopy::{IntoBytes, FromBytes, FromZeros, Immutable};

/// An ADS protocol command.
// https://infosys.beckhoff.com/content/1033/tc3_ads_intro/115847307.html?id=7738940192708835096
Expand Down Expand Up @@ -529,7 +529,7 @@ impl Device<'_> {
pub fn get_info(&self) -> Result<DeviceInfo> {
let mut data = DeviceInfoRaw::new_zeroed();
self.client.communicate(Command::DevInfo, self.addr,
&[], &mut [data.as_bytes_mut()])?;
&[], &mut [data.as_mut_bytes()])?;

// Decode the name string, which is null-terminated. Technically it's
// Windows-1252, but in practice no non-ASCII occurs.
Expand All @@ -551,10 +551,10 @@ impl Device<'_> {
index_offset: U32::new(index_offset),
length: U32::new(data.len().try_into()?),
};
let mut read_len = U32::<LE>::new(0);
let mut read_len = U32::new(0);

self.client.communicate(Command::Read, self.addr,
&[header.as_bytes()], &mut [read_len.as_bytes_mut(), data])?;
&[header.as_bytes()], &mut [read_len.as_mut_bytes(), data])?;

Ok(read_len.get() as usize)
}
Expand All @@ -577,10 +577,10 @@ impl Device<'_> {
///
/// Note: to be independent of the host's byte order, use the integer types
/// defined in `zerocopy::byteorder`.
pub fn read_value<T: Default + AsBytes + FromBytes>(&self, index_group: u32,
index_offset: u32) -> Result<T> {
pub fn read_value<T: Default + IntoBytes + FromBytes>(&self, index_group: u32,
index_offset: u32) -> Result<T> {
let mut buf = T::default();
self.read_exact(index_group, index_offset, buf.as_bytes_mut())?;
self.read_exact(index_group, index_offset, buf.as_mut_bytes())?;
Ok(buf)
}

Expand Down Expand Up @@ -623,13 +623,13 @@ impl Device<'_> {
read_length: U32::new(rlen.try_into()?),
write_length: U32::new(wlen.try_into()?),
};
let mut read_len = U32::<LE>::new(0);
let mut read_len = U32::new(0);
let mut w_buffers = vec![header.as_bytes()];
let mut r_buffers = (0..2*nreq + 1).map(|_| &mut [][..]).collect_vec();
r_buffers[0] = read_len.as_bytes_mut();
r_buffers[0] = read_len.as_mut_bytes();
for (i, req) in requests.iter_mut().enumerate() {
w_buffers.push(req.req.as_bytes());
r_buffers[1 + i] = req.res.as_bytes_mut();
r_buffers[1 + i] = req.res.as_mut_bytes();
r_buffers[1 + nreq + i] = req.rbuf;
}
self.client.communicate(Command::ReadWrite, self.addr, &w_buffers, &mut r_buffers)?;
Expand All @@ -651,8 +651,8 @@ impl Device<'_> {
/// Write data of given type.
///
/// See `read_value` for details.
pub fn write_value<T: AsBytes>(&self, index_group: u32, index_offset: u32,
value: &T) -> Result<()> {
pub fn write_value<T: IntoBytes + Immutable>(&self, index_group: u32, index_offset: u32,
value: &T) -> Result<()> {
self.write(index_group, index_offset, value.as_bytes())
}

Expand All @@ -673,14 +673,14 @@ impl Device<'_> {
read_length: U32::new(rlen.try_into()?),
write_length: U32::new(wlen.try_into()?),
};
let mut read_len = U32::<LE>::new(0);
let mut read_len = U32::new(0);
let mut w_buffers = vec![&[][..]; 2*nreq + 1];
let mut r_buffers = vec![read_len.as_bytes_mut()];
let mut r_buffers = vec![read_len.as_mut_bytes()];
w_buffers[0] = header.as_bytes();
for (i, req) in requests.iter_mut().enumerate() {
w_buffers[1 + i] = req.req.as_bytes();
w_buffers[1 + nreq + i] = req.wbuf;
r_buffers.push(req.res.as_bytes_mut());
r_buffers.push(req.res.as_mut_bytes());
}
self.client.communicate(Command::ReadWrite, self.addr, &w_buffers, &mut r_buffers)?;
Ok(())
Expand All @@ -697,10 +697,10 @@ impl Device<'_> {
read_length: U32::new(read_data.len().try_into()?),
write_length: U32::new(write_data.len().try_into()?),
};
let mut read_len = U32::<LE>::new(0);
let mut read_len = U32::new(0);
self.client.communicate(Command::ReadWrite, self.addr,
&[header.as_bytes(), write_data],
&mut [read_len.as_bytes_mut(), read_data])?;
&mut [read_len.as_mut_bytes(), read_data])?;
Ok(read_len.get() as usize)
}

Expand Down Expand Up @@ -732,15 +732,15 @@ impl Device<'_> {
read_length: U32::new(rlen.try_into()?),
write_length: U32::new(wlen.try_into()?),
};
let mut read_len = U32::<LE>::new(0);
let mut read_len = U32::new(0);
let mut w_buffers = vec![&[][..]; 2*nreq + 1];
let mut r_buffers = (0..2*nreq + 1).map(|_| &mut [][..]).collect_vec();
w_buffers[0] = header.as_bytes();
r_buffers[0] = read_len.as_bytes_mut();
r_buffers[0] = read_len.as_mut_bytes();
for (i, req) in requests.iter_mut().enumerate() {
w_buffers[1 + i] = req.req.as_bytes();
w_buffers[1 + nreq + i] = req.wbuf;
r_buffers[1 + i] = req.res.as_bytes_mut();
r_buffers[1 + i] = req.res.as_mut_bytes();
r_buffers[1 + nreq + i] = req.rbuf;
}
self.client.communicate(Command::ReadWrite, self.addr, &w_buffers, &mut r_buffers)?;
Expand All @@ -754,7 +754,7 @@ impl Device<'_> {
pub fn get_state(&self) -> Result<(AdsState, u16)> {
let mut state = ReadState::new_zeroed();
self.client.communicate(Command::ReadState, self.addr,
&[], &mut [state.as_bytes_mut()])?;
&[], &mut [state.as_mut_bytes()])?;

// Convert ADS state to the enum type
let ads_state = AdsState::try_from(state.ads_state.get())
Expand Down Expand Up @@ -794,9 +794,9 @@ impl Device<'_> {
cycle_time: U32::new(attributes.cycle_time.as_millis().try_into()?),
reserved: [0; 16],
};
let mut handle = U32::<LE>::new(0);
let mut handle = U32::new(0);
self.client.communicate(Command::AddNotification, self.addr,
&[data.as_bytes()], &mut [handle.as_bytes_mut()])?;
&[data.as_bytes()], &mut [handle.as_mut_bytes()])?;
self.client.notif_handles.borrow_mut().insert((self.addr, handle.get()));
Ok(handle.get())
}
Expand All @@ -818,12 +818,12 @@ impl Device<'_> {
read_length: U32::new(rlen.try_into()?),
write_length: U32::new(wlen.try_into()?),
};
let mut read_len = U32::<LE>::new(0);
let mut read_len = U32::new(0);
let mut w_buffers = vec![header.as_bytes()];
let mut r_buffers = vec![read_len.as_bytes_mut()];
let mut r_buffers = vec![read_len.as_mut_bytes()];
for req in requests.iter_mut() {
w_buffers.push(req.req.as_bytes());
r_buffers.push(req.res.as_bytes_mut());
r_buffers.push(req.res.as_mut_bytes());
}
self.client.communicate(Command::ReadWrite, self.addr, &w_buffers, &mut r_buffers)?;
for req in requests {
Expand All @@ -837,7 +837,7 @@ impl Device<'_> {
/// Delete a notification with given handle.
pub fn delete_notification(&self, handle: notif::Handle) -> Result<()> {
self.client.communicate(Command::DeleteNotification, self.addr,
&[U32::<LE>::new(handle).as_bytes()], &mut [])?;
&[U32::new(handle).as_bytes()], &mut [])?;
self.client.notif_handles.borrow_mut().remove(&(self.addr, handle));
Ok(())
}
Expand All @@ -859,12 +859,12 @@ impl Device<'_> {
read_length: U32::new(rlen.try_into()?),
write_length: U32::new(wlen.try_into()?),
};
let mut read_len = U32::<LE>::new(0);
let mut read_len = U32::new(0);
let mut w_buffers = vec![header.as_bytes()];
let mut r_buffers = vec![read_len.as_bytes_mut()];
let mut r_buffers = vec![read_len.as_mut_bytes()];
for req in requests.iter_mut() {
w_buffers.push(req.req.as_bytes());
r_buffers.push(req.res.as_bytes_mut());
r_buffers.push(req.res.as_mut_bytes());
}
self.client.communicate(Command::ReadWrite, self.addr, &w_buffers, &mut r_buffers)?;
for req in requests {
Expand Down Expand Up @@ -979,7 +979,7 @@ impl FromStr for AdsState {
// Structures used in communication, not exposed to user,
// but pub(crate) for the test suite.

#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
#[derive(FromBytes, IntoBytes, Immutable, Debug)]
#[repr(C)]
pub(crate) struct AdsHeader {
/// 0x0 - ADS command
Expand All @@ -988,12 +988,12 @@ pub(crate) struct AdsHeader {
/// 0x1001 - note from router (router state changed)
/// 0x1002 - get local netid
pub ams_cmd: u16,
pub length: U32<LE>,
pub length: U32,
pub dest_netid: AmsNetId,
pub dest_port: U16<LE>,
pub dest_port: U16,
pub src_netid: AmsNetId,
pub src_port: U16<LE>,
pub command: U16<LE>,
pub src_port: U16,
pub command: U16,
/// 0x01 - response
/// 0x02 - no return
/// 0x04 - ADS command
Expand All @@ -1003,69 +1003,69 @@ pub(crate) struct AdsHeader {
/// 0x40 - UDP
/// 0x80 - command during init phase
/// 0x8000 - broadcast
pub state_flags: U16<LE>,
pub data_length: U32<LE>,
pub error_code: U32<LE>,
pub invoke_id: U32<LE>,
pub state_flags: U16,
pub data_length: U32,
pub error_code: U32,
pub invoke_id: U32,
}

#[derive(FromBytes, FromZeroes, AsBytes)]
#[derive(FromBytes, IntoBytes, Immutable)]
#[repr(C)]
pub(crate) struct DeviceInfoRaw {
pub major: u8,
pub minor: u8,
pub version: U16<LE>,
pub version: U16,
pub name: [u8; 16],
}

#[derive(FromBytes, FromZeroes, AsBytes)]
#[derive(FromBytes, IntoBytes, Immutable)]
#[repr(C)]
pub(crate) struct IndexLength {
pub index_group: U32<LE>,
pub index_offset: U32<LE>,
pub length: U32<LE>,
pub index_group: U32,
pub index_offset: U32,
pub length: U32,
}

#[derive(FromBytes, FromZeroes, AsBytes)]
#[derive(FromBytes, IntoBytes, Immutable)]
#[repr(C)]
pub(crate) struct ResultLength {
pub result: U32<LE>,
pub length: U32<LE>,
pub result: U32,
pub length: U32,
}

#[derive(FromBytes, FromZeroes, AsBytes)]
#[derive(FromBytes, IntoBytes, Immutable)]
#[repr(C)]
pub(crate) struct IndexLengthRW {
pub index_group: U32<LE>,
pub index_offset: U32<LE>,
pub read_length: U32<LE>,
pub write_length: U32<LE>,
pub index_group: U32,
pub index_offset: U32,
pub read_length: U32,
pub write_length: U32,
}

#[derive(FromBytes, FromZeroes, AsBytes)]
#[derive(FromBytes, IntoBytes, Immutable)]
#[repr(C)]
pub(crate) struct ReadState {
pub ads_state: U16<LE>,
pub dev_state: U16<LE>,
pub ads_state: U16,
pub dev_state: U16,
}

#[derive(FromBytes, FromZeroes, AsBytes)]
#[derive(FromBytes, IntoBytes, Immutable)]
#[repr(C)]
pub(crate) struct WriteControl {
pub ads_state: U16<LE>,
pub dev_state: U16<LE>,
pub data_length: U32<LE>,
pub ads_state: U16,
pub dev_state: U16,
pub data_length: U32,
}

#[derive(FromBytes, FromZeroes, AsBytes)]
#[derive(FromBytes, IntoBytes, Immutable)]
#[repr(C)]
pub(crate) struct AddNotif {
pub index_group: U32<LE>,
pub index_offset: U32<LE>,
pub length: U32<LE>,
pub trans_mode: U32<LE>,
pub max_delay: U32<LE>,
pub cycle_time: U32<LE>,
pub index_group: U32,
pub index_offset: U32,
pub length: U32,
pub trans_mode: U32,
pub max_delay: U32,
pub cycle_time: U32,
pub reserved: [u8; 16],
}

Expand Down Expand Up @@ -1105,7 +1105,7 @@ impl<'buf> ReadRequest<'buf> {
/// A single request for a [`Device::write_multi`] request.
pub struct WriteRequest<'buf> {
req: IndexLength,
res: U32<LE>,
res: U32,
wbuf: &'buf [u8],
}

Expand Down Expand Up @@ -1211,8 +1211,8 @@ impl AddNotifRequest {

/// A single request for a [`Device::delete_notification_multi`] request.
pub struct DelNotifRequest {
req: U32<LE>,
res: U32<LE>,
req: U32,
res: U32,
}

impl DelNotifRequest {
Expand Down
4 changes: 2 additions & 2 deletions src/netid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str::FromStr;

use byteorder::{ReadBytesExt, WriteBytesExt, LE};
use itertools::Itertools;
use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{IntoBytes, FromBytes, Immutable};

/// Represents an AMS NetID.
///
Expand All @@ -20,7 +20,7 @@ use zerocopy::{AsBytes, FromBytes, FromZeroes};
/// sometimes even are identical to the device's IP address, there is no
/// requirement for this, and one should never rely on it.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Debug,
AsBytes, FromZeroes, FromBytes)]
FromBytes, IntoBytes, Immutable)]
#[repr(C)]
pub struct AmsNetId(pub [u8; 6]);

Expand Down
Loading

0 comments on commit 2972212

Please sign in to comment.