From 8da496de6b08ac1b1afd679c5a25b286111ec846 Mon Sep 17 00:00:00 2001 From: nnmm Date: Tue, 15 Mar 2022 20:32:58 +0100 Subject: [PATCH] Clean up a little bit --- rclrs/src/node/publisher.rs | 12 ++++++------ rclrs_examples/src/message_demo.rs | 6 +++++- rosidl_generator_rs/resource/msg.rs.em | 6 +++--- rosidl_runtime_rs/src/traits.rs | 13 ++++++++----- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/rclrs/src/node/publisher.rs b/rclrs/src/node/publisher.rs index a74c3cd7e..37b911ddb 100644 --- a/rclrs/src/node/publisher.rs +++ b/rclrs/src/node/publisher.rs @@ -97,7 +97,7 @@ where /// Publishes a message. /// - /// The [`OwnedOrBorrowedMessage`] trait is implemented by any + /// The [`MessageCow`] trait is implemented by any /// [`Message`] as well as any reference to a `Message`. /// /// The reason for allowing owned messages is that publishing owned messages can be more @@ -107,7 +107,7 @@ where /// /// Hence, when a message will not be needed anymore after publishing, pass it by value. /// When a message will be needed again after publishing, pass it by reference, instead of cloning and passing by value. - pub fn publish<'a, M: OwnedOrBorrowedMessage<'a, T>>( + pub fn publish<'a, M: MessageCow<'a, T>>( &self, message: M, ) -> Result<(), RclReturnCode> { @@ -124,18 +124,18 @@ where } } -/// Convenience trait for ['Publisher::publish`]. -pub trait OwnedOrBorrowedMessage<'a, T: Message> { +/// Convenience trait for [`Publisher::publish`]. +pub trait MessageCow<'a, T: Message> { fn into_cow(self) -> Cow<'a, T>; } -impl<'a, T: Message> OwnedOrBorrowedMessage<'a, T> for T { +impl<'a, T: Message> MessageCow<'a, T> for T { fn into_cow(self) -> Cow<'a, T> { Cow::Owned(self) } } -impl<'a, T: Message> OwnedOrBorrowedMessage<'a, T> for &'a T { +impl<'a, T: Message> MessageCow<'a, T> for &'a T { fn into_cow(self) -> Cow<'a, T> { Cow::Borrowed(self) } diff --git a/rclrs_examples/src/message_demo.rs b/rclrs_examples/src/message_demo.rs index dd71b518e..705c5d1f1 100644 --- a/rclrs_examples/src/message_demo.rs +++ b/rclrs_examples/src/message_demo.rs @@ -58,8 +58,12 @@ fn check_default_values() { assert_eq!(msg.nested_seq_unbounded, seq![]); // The default instance for the idiomatic type also has the defaults set + let idiomatic_msg = rclrs_example_msgs::msg::VariousTypes::default(); assert_eq!( - rclrs_example_msgs::msg::VariousTypes::default().into_owned_rmw_message(), + rclrs_example_msgs::msg::VariousTypes::into_rmw_message(std::borrow::Cow::Owned( + idiomatic_msg + )) + .into_owned(), msg ); } diff --git a/rosidl_generator_rs/resource/msg.rs.em b/rosidl_generator_rs/resource/msg.rs.em index 13010db94..94efcc340 100644 --- a/rosidl_generator_rs/resource/msg.rs.em +++ b/rosidl_generator_rs/resource/msg.rs.em @@ -122,7 +122,7 @@ impl rosidl_runtime_rs::Message for @(type_name) { .map(|elem| elem.as_str().into()), @[ elif isinstance(member.type.value_type, NamedType) or isinstance(member.type.value_type, NamespacedType)]@ @(get_rs_name(member.name)): msg.@(get_rs_name(member.name)) - .map(|elem| elem.into_owned_rmw_message()), + .map(|elem| @(get_idiomatic_rs_type(member.type.value_type))::into_rmw_message(std::borrow::Cow::Owned(elem)).into_owned()), @[ else]@ @(get_rs_name(member.name)): msg.@(get_rs_name(member.name)).clone(), @[ end if]@ @@ -143,7 +143,7 @@ impl rosidl_runtime_rs::Message for @(type_name) { @[ elif isinstance(member.type.value_type, NamedType) or isinstance(member.type.value_type, NamespacedType)]@ @(get_rs_name(member.name)): msg.@(get_rs_name(member.name)) .into_iter() - .map(|elem| elem.into_owned_rmw_message()) + .map(|elem| @(get_idiomatic_rs_type(member.type.value_type))::into_rmw_message(std::borrow::Cow::Owned(elem)).into_owned()) .collect(), @[ else]@ @(get_rs_name(member.name)): msg.@(get_rs_name(member.name)).into(), @@ -152,7 +152,7 @@ impl rosidl_runtime_rs::Message for @(type_name) { @# @# == NamedType + NamespacedType == @[ elif isinstance(member.type, NamedType) or isinstance(member.type, NamespacedType)]@ - @(get_rs_name(member.name)): msg.@(get_rs_name(member.name)).into_owned_rmw_message(), + @(get_rs_name(member.name)): @(get_idiomatic_rs_type(member.type))::into_rmw_message(std::borrow::Cow::Owned(msg.@(get_rs_name(member.name)))).into_owned(), @# @# @# == Bounded and basic types == diff --git a/rosidl_runtime_rs/src/traits.rs b/rosidl_runtime_rs/src/traits.rs index d9a52b828..8c1f21d9a 100644 --- a/rosidl_runtime_rs/src/traits.rs +++ b/rosidl_runtime_rs/src/traits.rs @@ -126,13 +126,16 @@ pub trait Message: Clone + Debug + Default + 'static { /// The corresponding RMW-compatible message type. type RmwMsg: RmwMessage; - /// Converts the idiomatic message into a RMW-compatible message. + /// Converts the idiomatic message into an RMW-compatible message. + /// + /// If the idiomatic message is owned, a slightly more efficient conversion is possible. + /// This is why the function takes a `Cow`. + /// + /// If this function receives a borrowed message that is already RMW-compatible, it should + /// directly return that borrowed message. + /// This is why the return type is also `Cow`. fn into_rmw_message<'a>(msg_cow: Cow<'a, Self>) -> Cow<'a, Self::RmwMsg>; /// Converts the RMW-compatible message into an idiomatic message. fn from_rmw_message(msg: Self::RmwMsg) -> Self; - - fn into_owned_rmw_message(self) -> Self::RmwMsg { - Self::into_rmw_message(Cow::Owned(self)).into_owned() - } }