diff --git a/rosidl_generator_rs/cmake/rosidl_generator_rs_generate_interfaces.cmake b/rosidl_generator_rs/cmake/rosidl_generator_rs_generate_interfaces.cmake index 2937ad0db..6940177e9 100644 --- a/rosidl_generator_rs/cmake/rosidl_generator_rs_generate_interfaces.cmake +++ b/rosidl_generator_rs/cmake/rosidl_generator_rs_generate_interfaces.cmake @@ -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 diff --git a/rosidl_generator_rs/resource/build.rs b/rosidl_generator_rs/resource/build.rs deleted file mode 100644 index da6c71e18..000000000 --- a/rosidl_generator_rs/resource/build.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::env; - -const ROS_DISTRO: &'static str = "ROS_DISTRO"; - -fn get_env_var_or_abort(env_var: &'static str) -> String { - if let Ok(value) = env::var(env_var) { - value - } else { - panic!( - "{} environment variable not set - please source ROS 2 installation first.", - env_var - ); - } -} - -fn main() { - let distro = get_env_var_or_abort(ROS_DISTRO); - println!("cargo:rustc-cfg=ros_distro=\"{}\"", distro); -} diff --git a/rosidl_generator_rs/resource/msg.rs.em b/rosidl_generator_rs/resource/msg.rs.em index 3a163ec1e..13010db94 100644 --- a/rosidl_generator_rs/resource/msg.rs.em +++ b/rosidl_generator_rs/resource/msg.rs.em @@ -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: @@ -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, out_seq: &mut rosidl_runtime_rs::Sequence) -> 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 } } diff --git a/rosidl_runtime_rs/build.rs b/rosidl_runtime_rs/build.rs index a33bc727e..fbc3c85cf 100644 --- a/rosidl_runtime_rs/build.rs +++ b/rosidl_runtime_rs/build.rs @@ -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) { @@ -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); } diff --git a/rosidl_runtime_rs/src/sequence.rs b/rosidl_runtime_rs/src/sequence.rs index a5dbca910..8aec2b492 100644 --- a/rosidl_runtime_rs/src/sequence.rs +++ b/rosidl_runtime_rs/src/sequence.rs @@ -265,19 +265,18 @@ where } impl Sequence { - /// 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::() * 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()); } @@ -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 { @@ -534,34 +528,27 @@ macro_rules! sequence_alloc_impl { unsafe { $fini_func(seq as *mut _) } } fn sequence_copy(in_seq: &Sequence, out_seq: &mut Sequence) -> 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::() * 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::() * 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 } } }; @@ -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, diff --git a/rosidl_runtime_rs/src/string.rs b/rosidl_runtime_rs/src/string.rs index f324504bf..4ccc1c616 100644 --- a/rosidl_runtime_rs/src/string.rs +++ b/rosidl_runtime_rs/src/string.rs @@ -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 { @@ -228,17 +223,9 @@ macro_rules! string_impl { unsafe { $sequence_fini(seq as *mut _) } } fn sequence_copy(in_seq: &Sequence, out_seq: &mut Sequence) -> 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 } } };