From cbd7237e76148fc9c7c89becedb12cd38bf317ec Mon Sep 17 00:00:00 2001 From: Julien Enoch Date: Wed, 11 Oct 2023 12:21:49 +0200 Subject: [PATCH] Fix Windows build --- zenoh-plugin-ros2dds/src/dds_types.rs | 8 ++++---- zenoh-plugin-ros2dds/src/dds_utils.rs | 18 +++++++++++++----- zenoh-plugin-ros2dds/src/ros_discovery.rs | 4 ++-- zenoh-plugin-ros2dds/src/route_subscriber.rs | 4 ++-- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/zenoh-plugin-ros2dds/src/dds_types.rs b/zenoh-plugin-ros2dds/src/dds_types.rs index 9a27fb4..f7c3c20 100644 --- a/zenoh-plugin-ros2dds/src/dds_types.rs +++ b/zenoh-plugin-ros2dds/src/dds_types.rs @@ -20,7 +20,7 @@ use zenoh::buffers::ZBuf; use zenoh::buffers::ZSlice; use zenoh::prelude::*; -use crate::dds_utils::ddsrt_iov_len_from; +use crate::dds_utils::ddsrt_iov_len_to_usize; #[derive(Debug)] pub struct TypeInfo { @@ -132,7 +132,7 @@ impl DDSRawSample { unsafe { slice::from_raw_parts( self.data.iov_base as *const u8, - ddsrt_iov_len_from(self.data.iov_len).unwrap(), + ddsrt_iov_len_to_usize(self.data.iov_len).unwrap(), ) } } @@ -147,7 +147,7 @@ impl DDSRawSample { } &slice::from_raw_parts( self.data.iov_base as *const u8, - ddsrt_iov_len_from(self.data.iov_len).unwrap(), + ddsrt_iov_len_to_usize(self.data.iov_len).unwrap(), )[4..] } } @@ -176,7 +176,7 @@ impl DDSRawSample { } #[cfg(not(feature = "dds_shm"))] - ddsrt_iov_len_from(self.data.iov_len).unwrap() + ddsrt_iov_len_to_usize(self.data.iov_len).unwrap() } } diff --git a/zenoh-plugin-ros2dds/src/dds_utils.rs b/zenoh-plugin-ros2dds/src/dds_utils.rs index 21bcf6a..b2ac840 100644 --- a/zenoh-plugin-ros2dds/src/dds_utils.rs +++ b/zenoh-plugin-ros2dds/src/dds_utils.rs @@ -37,13 +37,21 @@ pub const DDS_ENTITY_NULL: dds_entity_t = 0; // An atomic dds_entity_t (=i32), for safe concurrent creation/deletion of DDS entities pub type AtomicDDSEntity = AtomicI32; -#[inline] -pub fn ddsrt_iov_len_from(i: usize) -> Result { +pub fn ddsrt_iov_len_to_usize(len: ddsrt_iov_len_t) -> Result { // Depending the platform ddsrt_iov_len_t can have different typedef // See https://github.com/eclipse-cyclonedds/cyclonedds/blob/master/src/ddsrt/include/dds/ddsrt/iovec.h - // Thus this conversion is useless on most platforms, but not all! + // Thus this conversion is NOT useless on Windows where ddsrt_iov_len_t is a u32 ! #[allow(clippy::useless_conversion)] - i.try_into() + len.try_into() + .map_err(|e| format!("INTERNAL ERROR converting a ddsrt_iov_len_t to usize: {e}")) +} + +pub fn ddsrt_iov_len_from_usize(len: usize) -> Result { + // Depending the platform ddsrt_iov_len_t can have different typedef + // See https://github.com/eclipse-cyclonedds/cyclonedds/blob/master/src/ddsrt/include/dds/ddsrt/iovec.h + // Thus this conversion is NOT useless on Windows where ddsrt_iov_len_t is a u32 ! + #[allow(clippy::useless_conversion)] + len.try_into() .map_err(|e| format!("INTERNAL ERROR converting a usize to ddsrt_iov_len_t: {e}")) } @@ -176,7 +184,7 @@ pub fn dds_write(data_writer: dds_entity_t, data: Vec) -> Result<(), String> // that is not necessarily safe or guaranteed to be leak free. // TODO replace when stable https://github.com/rust-lang/rust/issues/65816 let (ptr, len, capacity) = vec_into_raw_parts(data); - let size: ddsrt_iov_len_t = ddsrt_iov_len_from(len)?; + let size: ddsrt_iov_len_t = ddsrt_iov_len_from_usize(len)?; let data_out = ddsrt_iovec_t { iov_base: ptr as *mut std::ffi::c_void, diff --git a/zenoh-plugin-ros2dds/src/ros_discovery.rs b/zenoh-plugin-ros2dds/src/ros_discovery.rs index 778f529..8ade79c 100644 --- a/zenoh-plugin-ros2dds/src/ros_discovery.rs +++ b/zenoh-plugin-ros2dds/src/ros_discovery.rs @@ -13,7 +13,7 @@ use crate::{ChannelEvent, ROS_DISCOVERY_INFO_PUSH_INTERVAL_MS}; // Contributors: // ZettaScale Zenoh Team, // -use crate::dds_utils::{ddsrt_iov_len_from, delete_dds_entity, get_guid}; +use crate::dds_utils::{ddsrt_iov_len_from_usize, delete_dds_entity, get_guid}; use crate::gid::Gid; use async_std::task; use cdr::{CdrLe, Infinite}; @@ -318,7 +318,7 @@ impl RosDiscoveryInfoMgr { // that is not necessarily safe or guaranteed to be leak free. // TODO replace when stable https://github.com/rust-lang/rust/issues/65816 let (ptr, len, capacity) = crate::vec_into_raw_parts(buf); - let size: ddsrt_iov_len_t = ddsrt_iov_len_from(len)?; + let size: ddsrt_iov_len_t = ddsrt_iov_len_from_usize(len)?; let data_out = ddsrt_iovec_t { iov_base: ptr as *mut std::ffi::c_void, diff --git a/zenoh-plugin-ros2dds/src/route_subscriber.rs b/zenoh-plugin-ros2dds/src/route_subscriber.rs index 05401f3..93acb1e 100644 --- a/zenoh-plugin-ros2dds/src/route_subscriber.rs +++ b/zenoh-plugin-ros2dds/src/route_subscriber.rs @@ -25,7 +25,7 @@ use zenoh::query::ReplyKeyExpr; use zenoh::{prelude::r#async::AsyncResolve, subscriber::Subscriber}; use zenoh_ext::{FetchingSubscriber, SubscriberBuilderExt}; -use crate::dds_utils::{create_dds_writer, ddsrt_iov_len_from, delete_dds_entity, get_guid}; +use crate::dds_utils::{create_dds_writer, ddsrt_iov_len_from_usize, delete_dds_entity, get_guid}; use crate::liveliness_mgt::new_ke_liveliness_sub; use crate::qos_helpers::is_transient_local; use crate::ros2_utils::{is_message_for_action, ros2_message_type_to_dds_type}; @@ -343,7 +343,7 @@ fn do_route_data(s: Sample, ros2_name: &str, data_writer: dds_entity_t) { // that is not necessarily safe or guaranteed to be leak free. // TODO replace when stable https://github.com/rust-lang/rust/issues/65816 let (ptr, len, capacity) = vec_into_raw_parts(bs); - let size: ddsrt_iov_len_t = match ddsrt_iov_len_from(len) { + let size: ddsrt_iov_len_t = match ddsrt_iov_len_from_usize(len) { Ok(s) => s, Err(_) => { log::warn!(