Skip to content

Commit

Permalink
Enable direct creation of the _Options for all primitive types
Browse files Browse the repository at this point in the history
Signed-off-by: Michael X. Grey <greyxmike@gmail.com>
  • Loading branch information
mxgrey committed Nov 20, 2024
1 parent 6c61c9c commit da05361
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
21 changes: 14 additions & 7 deletions rclrs/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,25 @@ pub struct ClientOptions<'a> {
pub qos: QoSProfile,
}

impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for ClientOptions<'a> {
fn from(value: T) -> Self {
let options = value.into_primitive_options();
let mut qos = QoSProfile::services_default();
options.apply(&mut qos);
impl<'a> ClientOptions<'a> {
/// Initialize a new [`ClientOptions`] with default settings.
pub fn new(service_name: &'a str) -> Self {
Self {
service_name: options.name,
qos,
service_name,
qos: QoSProfile::services_default(),
}
}
}

impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for ClientOptions<'a> {
fn from(value: T) -> Self {
let primitive = value.into_primitive_options();
let mut options = Self::new(primitive.name);
primitive.apply(&mut options.qos);
options
}
}

impl<T> ClientBase for Client<T>
where
T: rosidl_runtime_rs::Service,
Expand Down
21 changes: 14 additions & 7 deletions rclrs/src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,25 @@ pub struct PublisherOptions<'a> {
pub qos: QoSProfile,
}

impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for PublisherOptions<'a> {
fn from(value: T) -> Self {
let options = value.into_primitive_options();
let mut qos = QoSProfile::topics_default();
options.apply(&mut qos);
impl<'a> PublisherOptions<'a> {
/// Initialize a new [`PublisherOptions`] with default settings.
pub fn new(topic: &'a str) -> Self {
Self {
topic: options.name,
qos,
topic,
qos: QoSProfile::topics_default(),
}
}
}

impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for PublisherOptions<'a> {
fn from(value: T) -> Self {
let primitive = value.into_primitive_options();
let mut options = Self::new(primitive.name);
primitive.apply(&mut options.qos);
options
}
}

/// Convenience trait for [`Publisher::publish`].
pub trait MessageCow<'a, T: Message> {
/// Wrap the owned or borrowed message in a `Cow`.
Expand Down
23 changes: 15 additions & 8 deletions rclrs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,29 @@ where
pub struct ServiceOptions<'a> {
/// The name for the service
pub name: &'a str,
/// The quality of service for the service.
/// The quality of service profile for the service.
pub qos: QoSProfile,
}

impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for ServiceOptions<'a> {
fn from(value: T) -> Self {
let options = value.into_primitive_options();
let mut qos = QoSProfile::services_default();
options.apply(&mut qos);
impl<'a> ServiceOptions<'a> {
/// Initialize a new [`ServiceOptions`] with default settings.
pub fn new(name: &'a str) -> Self {
Self {
name: options.name,
qos,
name,
qos: QoSProfile::services_default(),
}
}
}

impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for ServiceOptions<'a> {
fn from(value: T) -> Self {
let primitive = value.into_primitive_options();
let mut options = Self::new(primitive.name);
primitive.apply(&mut options.qos);
options
}
}

impl<T> ServiceBase for Service<T>
where
T: rosidl_runtime_rs::Service,
Expand Down
23 changes: 14 additions & 9 deletions rclrs/src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,25 @@ pub struct SubscriptionOptions<'a> {
pub qos: QoSProfile,
}

impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for SubscriptionOptions<'a> {
fn from(value: T) -> Self {
let options = value.into_primitive_options();
// Topics will use the QOS_PROFILE_DEFAULT by default, which is designed
// to roughly match the ROS 1 default topic behavior.
let mut qos = QoSProfile::topics_default();
options.apply(&mut qos);
impl<'a> SubscriptionOptions<'a> {
/// Initialize a new [`SubscriptionOptions`] with default settings.
pub fn new(topic: &'a str) -> Self {
Self {
topic: options.name,
qos,
topic,
qos: QoSProfile::topics_default(),
}
}
}

impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for SubscriptionOptions<'a> {
fn from(value: T) -> Self {
let primitive = value.into_primitive_options();
let mut options = Self::new(primitive.name);
primitive.apply(&mut options.qos);
options
}
}

impl<T> SubscriptionBase for Subscription<T>
where
T: Message,
Expand Down

0 comments on commit da05361

Please sign in to comment.