Skip to content

Commit

Permalink
Remove usage of the sequence_copy functions until they are released
Browse files Browse the repository at this point in the history
  • Loading branch information
nnmm committed Mar 13, 2022
1 parent 364e994 commit 3aecf1f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,6 @@ install(
DESTINATION share/${PROJECT_NAME}/rust/
)

install(
FILES "${rosidl_generator_rs_TEMPLATE_DIR}/build.rs"
DESTINATION share/${PROJECT_NAME}/rust/
)

if(BUILD_TESTING AND rosidl_generate_interfaces_ADD_LINTER_TESTS)
if(
NOT _generated_msg_rs_files STREQUAL "" OR
Expand Down
19 changes: 0 additions & 19 deletions rosidl_generator_rs/resource/build.rs

This file was deleted.

13 changes: 3 additions & 10 deletions rosidl_generator_rs/resource/msg.rs.em
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ extern "C" {
fn @(package_name)__@(subfolder)__@(type_name)__init(msg: *mut @(type_name)) -> bool;
fn @(package_name)__@(subfolder)__@(type_name)__Sequence__init(seq: *mut rosidl_runtime_rs::Sequence<@(type_name)>, size: libc::size_t) -> bool;
fn @(package_name)__@(subfolder)__@(type_name)__Sequence__fini(seq: *mut rosidl_runtime_rs::Sequence<@(type_name)>) -> ();
#[cfg(ros_distro = "rolling")]
fn @(package_name)__@(subfolder)__@(type_name)__Sequence__copy(in_seq: *const rosidl_runtime_rs::Sequence<@(type_name)>, out_seq: *mut rosidl_runtime_rs::Sequence<@(type_name)>) -> bool;
}

@# Drop is not needed, since the default drop glue does the same as fini here:
Expand Down Expand Up @@ -64,14 +62,9 @@ impl rosidl_runtime_rs::SequenceAlloc for @(type_name) {
unsafe { @(package_name)__@(subfolder)__@(type_name)__Sequence__fini(seq as *mut _) }
}
fn sequence_copy(in_seq: &rosidl_runtime_rs::Sequence<Self>, out_seq: &mut rosidl_runtime_rs::Sequence<Self>) -> bool {
#[cfg(ros_distro = "rolling")]
unsafe { @(package_name)__@(subfolder)__@(type_name)__Sequence__copy(in_seq as *const _, out_seq as *mut _) }
#[cfg(not(ros_distro = "rolling"))]
{
out_seq.resize_to_at_least(in_seq.len());
out_seq.clone_from_slice(in_seq.as_slice());
true
}
out_seq.resize_to_at_least(in_seq.len());
out_seq.clone_from_slice(in_seq.as_slice());
true
}
}

Expand Down
4 changes: 0 additions & 4 deletions rosidl_runtime_rs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::env;
use std::path::Path;

const AMENT_PREFIX_PATH: &str = "AMENT_PREFIX_PATH";
const ROS_DISTRO: &str = "ROS_DISTRO";

fn get_env_var_or_abort(env_var: &'static str) -> String {
if let Ok(value) = env::var(env_var) {
Expand All @@ -21,7 +20,4 @@ fn main() {
let library_path = Path::new(ament_prefix_path).join("lib");
println!("cargo:rustc-link-search=native={}", library_path.display());
}

let distro = get_env_var_or_abort(ROS_DISTRO);
println!("cargo:rustc-cfg=ros_distro=\"{}\"", distro);
}
81 changes: 34 additions & 47 deletions rosidl_runtime_rs/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,18 @@ where
}

impl<T: Default + SequenceAlloc> Sequence<T> {
/// Internal function for the sequence_copy impl. To be removed when rosidl#650 is backported.
///
/// This function is not available in Rolling since Rolling should use rcutils_allocator_t.
#[cfg(not(ros_distro = "rolling"))]
/// Internal function for the sequence_copy impl. To be removed when rosidl#650 is backported and released.
pub fn resize_to_at_least(&mut self, len: usize) {
let allocation_size = std::mem::size_of::<Self>() * len;
if self.capacity < len {
// SAFETY: The memory in self.data is owned by C.
let data = unsafe { libc::realloc(self.data as *mut _, allocation_size) } as *mut T;
if data.is_null() {
panic!("realloc failed");
}
// Initialize the new memory
for i in self.capacity..len {
// SAFETY: i is in bounds, and write() is appropriate for initializing uninitialized memory
unsafe {
data.add(i).write(T::default());
}
Expand Down Expand Up @@ -505,17 +504,12 @@ impl Display for SequenceExceedsBoundsError {

impl std::error::Error for SequenceExceedsBoundsError {}

macro_rules! sequence_alloc_impl {
macro_rules! impl_sequence_alloc_for_primitive_type {
($rust_type:ty, $init_func:ident, $fini_func:ident, $copy_func:ident) => {
#[link(name = "rosidl_runtime_c")]
extern "C" {
fn $init_func(seq: *mut Sequence<$rust_type>, size: libc::size_t) -> bool;
fn $fini_func(seq: *mut Sequence<$rust_type>);
#[cfg(ros_distro = "rolling")]
fn $copy_func(
in_seq: *const Sequence<$rust_type>,
out_seq: *mut Sequence<$rust_type>,
) -> bool;
}

impl SequenceAlloc for $rust_type {
Expand All @@ -534,34 +528,27 @@ macro_rules! sequence_alloc_impl {
unsafe { $fini_func(seq as *mut _) }
}
fn sequence_copy(in_seq: &Sequence<Self>, out_seq: &mut Sequence<Self>) -> bool {
// SAFETY: There are no special preconditions to the sequence_copy function.
#[cfg(ros_distro = "rolling")]
unsafe {
$copy_func(in_seq as *const _, out_seq as *mut _)
}

#[cfg(not(ros_distro = "rolling"))]
{
let allocation_size = std::mem::size_of::<Self>() * in_seq.size;
if out_seq.capacity < in_seq.size {
let data =
unsafe { libc::realloc(out_seq.data as *mut _, allocation_size) };
if data.is_null() {
return false;
}
out_seq.data = data as *mut _;
out_seq.capacity = in_seq.size;
let allocation_size = std::mem::size_of::<Self>() * in_seq.size;
if out_seq.capacity < in_seq.size {
// SAFETY: The memory in out_seq.data is owned by C.
let data =
unsafe { libc::realloc(out_seq.data as *mut _, allocation_size) };
if data.is_null() {
return false;
}
unsafe {
libc::memcpy(
out_seq.data as *mut _,
in_seq.data as *const _,
allocation_size,
);
}
out_seq.size = in_seq.size;
true
out_seq.data = data as *mut _;
out_seq.capacity = in_seq.size;
}
// SAFETY: The memory areas don't overlap.
unsafe {
libc::memcpy(
out_seq.data as *mut _,
in_seq.data as *const _,
allocation_size,
);
}
out_seq.size = in_seq.size;
true
}
}
};
Expand All @@ -571,67 +558,67 @@ macro_rules! sequence_alloc_impl {
//
// See https://github.com/ros2/rosidl/blob/master/rosidl_runtime_c/include/rosidl_runtime_c/primitives_sequence.h
// Long double isn't available in Rust, so it is skipped.
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
f32,
rosidl_runtime_c__float__Sequence__init,
rosidl_runtime_c__float__Sequence__fini,
rosidl_runtime_c__float__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
f64,
rosidl_runtime_c__double__Sequence__init,
rosidl_runtime_c__double__Sequence__fini,
rosidl_runtime_c__double__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
bool,
rosidl_runtime_c__boolean__Sequence__init,
rosidl_runtime_c__boolean__Sequence__fini,
rosidl_runtime_c__boolean__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
u8,
rosidl_runtime_c__uint8__Sequence__init,
rosidl_runtime_c__uint8__Sequence__fini,
rosidl_runtime_c__uint8__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
i8,
rosidl_runtime_c__int8__Sequence__init,
rosidl_runtime_c__int8__Sequence__fini,
rosidl_runtime_c__int8__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
u16,
rosidl_runtime_c__uint16__Sequence__init,
rosidl_runtime_c__uint16__Sequence__fini,
rosidl_runtime_c__uint16__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
i16,
rosidl_runtime_c__int16__Sequence__init,
rosidl_runtime_c__int16__Sequence__fini,
rosidl_runtime_c__int16__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
u32,
rosidl_runtime_c__uint32__Sequence__init,
rosidl_runtime_c__uint32__Sequence__fini,
rosidl_runtime_c__uint32__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
i32,
rosidl_runtime_c__int32__Sequence__init,
rosidl_runtime_c__int32__Sequence__fini,
rosidl_runtime_c__int32__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
u64,
rosidl_runtime_c__uint64__Sequence__init,
rosidl_runtime_c__uint64__Sequence__fini,
rosidl_runtime_c__uint64__Sequence__copy
);
sequence_alloc_impl!(
impl_sequence_alloc_for_primitive_type!(
i64,
rosidl_runtime_c__int64__Sequence__init,
rosidl_runtime_c__int64__Sequence__fini,
Expand Down
19 changes: 3 additions & 16 deletions rosidl_runtime_rs/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ macro_rules! string_impl {
fn $assignn(s: *mut $string, value: *const $char_type, n: libc::size_t) -> bool;
fn $sequence_init(seq: *mut Sequence<$string>, size: libc::size_t) -> bool;
fn $sequence_fini(seq: *mut Sequence<$string>);
#[cfg(ros_distro = "rolling")]
fn $sequence_copy(
in_seq: *const Sequence<$string>,
out_seq: *mut Sequence<$string>,
) -> bool;
}

impl Default for $string {
Expand Down Expand Up @@ -228,17 +223,9 @@ macro_rules! string_impl {
unsafe { $sequence_fini(seq as *mut _) }
}
fn sequence_copy(in_seq: &Sequence<Self>, out_seq: &mut Sequence<Self>) -> bool {
// SAFETY: There are no special preconditions to the sequence_copy function.
#[cfg(ros_distro = "rolling")]
unsafe {
$sequence_copy(in_seq as *const _, out_seq as *mut _)
}
#[cfg(not(ros_distro = "rolling"))]
{
out_seq.resize_to_at_least(in_seq.len());
out_seq.clone_from_slice(in_seq.as_slice());
true
}
out_seq.resize_to_at_least(in_seq.len());
out_seq.clone_from_slice(in_seq.as_slice());
true
}
}
};
Expand Down

0 comments on commit 3aecf1f

Please sign in to comment.