Skip to content

Commit

Permalink
Clean up a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
nnmm committed Mar 15, 2022
1 parent 3aecf1f commit 8da496d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
12 changes: 6 additions & 6 deletions rclrs/src/node/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> {
Expand All @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion rclrs_examples/src/message_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
6 changes: 3 additions & 3 deletions rosidl_generator_rs/resource/msg.rs.em
Original file line number Diff line number Diff line change
Expand Up @@ -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]@
Expand All @@ -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(),
Expand All @@ -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 ==
Expand Down
13 changes: 8 additions & 5 deletions rosidl_runtime_rs/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 8da496d

Please sign in to comment.