Skip to content

Commit

Permalink
Fix Windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
JEnoch committed Oct 11, 2023
1 parent 37b8535 commit cbd7237
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
8 changes: 4 additions & 4 deletions zenoh-plugin-ros2dds/src/dds_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
)
}
}
Expand All @@ -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..]
}
}
Expand Down Expand Up @@ -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()
}
}

Expand Down
18 changes: 13 additions & 5 deletions zenoh-plugin-ros2dds/src/dds_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ddsrt_iov_len_t, String> {
pub fn ddsrt_iov_len_to_usize(len: ddsrt_iov_len_t) -> Result<usize, String> {
// 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<ddsrt_iov_len_t, String> {
// 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}"))
}

Expand Down Expand Up @@ -176,7 +184,7 @@ pub fn dds_write(data_writer: dds_entity_t, data: Vec<u8>) -> 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,
Expand Down
4 changes: 2 additions & 2 deletions zenoh-plugin-ros2dds/src/ros_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{ChannelEvent, ROS_DISCOVERY_INFO_PUSH_INTERVAL_MS};
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
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};
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions zenoh-plugin-ros2dds/src/route_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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!(
Expand Down

0 comments on commit cbd7237

Please sign in to comment.