Skip to content

Commit f12f874

Browse files
committed
Migrate to PublisherOptions
Signed-off-by: Michael X. Grey <[email protected]>
1 parent 433a348 commit f12f874

File tree

7 files changed

+72
-35
lines changed

7 files changed

+72
-35
lines changed

examples/message_demo/src/message_demo.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,8 @@ fn demonstrate_pubsub() -> Result<(), Error> {
141141
let context = rclrs::Context::new(env::args())?;
142142
let node = rclrs::create_node(&context, "message_demo")?;
143143

144-
let idiomatic_publisher = node.create_publisher::<rclrs_example_msgs::msg::VariousTypes>(
145-
"topic",
146-
rclrs::QOS_PROFILE_DEFAULT,
147-
)?;
148-
let direct_publisher = node.create_publisher::<rclrs_example_msgs::msg::rmw::VariousTypes>(
149-
"topic",
150-
rclrs::QOS_PROFILE_DEFAULT,
151-
)?;
144+
let idiomatic_publisher = node.create_publisher::<rclrs_example_msgs::msg::VariousTypes>("topic")?;
145+
let direct_publisher = node.create_publisher::<rclrs_example_msgs::msg::rmw::VariousTypes>("topic")?;
152146

153147
let _idiomatic_subscription = node
154148
.create_subscription::<rclrs_example_msgs::msg::VariousTypes, _>(

examples/minimal_pub_sub/src/minimal_publisher.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ fn main() -> Result<(), Error> {
77

88
let node = rclrs::create_node(&context, "minimal_publisher")?;
99

10-
let publisher =
11-
node.create_publisher::<std_msgs::msg::String>("topic", rclrs::QOS_PROFILE_DEFAULT)?;
10+
let publisher = node.create_publisher::<std_msgs::msg::String>("topic")?;
1211

1312
let mut message = std_msgs::msg::String::default();
1413

examples/minimal_pub_sub/src/minimal_two_nodes.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ fn main() -> Result<(), Error> {
5555
let subscriber_node_one = MinimalSubscriber::new("minimal_subscriber_one", "topic")?;
5656
let subscriber_node_two = MinimalSubscriber::new("minimal_subscriber_two", "topic")?;
5757

58-
let publisher = publisher_node
59-
.create_publisher::<std_msgs::msg::String>("topic", rclrs::QOS_PROFILE_DEFAULT)?;
58+
let publisher = publisher_node.create_publisher::<std_msgs::msg::String>("topic")?;
6059

6160
std::thread::spawn(move || -> Result<(), rclrs::RclrsError> {
6261
let mut message = std_msgs::msg::String::default();

examples/minimal_pub_sub/src/zero_copy_publisher.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ fn main() -> Result<(), Error> {
77

88
let node = rclrs::create_node(&context, "minimal_publisher")?;
99

10-
let publisher =
11-
node.create_publisher::<std_msgs::msg::rmw::UInt32>("topic", rclrs::QOS_PROFILE_DEFAULT)?;
10+
let publisher = node.create_publisher::<std_msgs::msg::rmw::UInt32>("topic")?;
1211

1312
let mut publish_count: u32 = 1;
1413

examples/rust_pubsub/src/simple_publisher.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rclrs::{create_node, Context, Node, Publisher, RclrsError, QOS_PROFILE_DEFAULT};
1+
use rclrs::{create_node, Context, Node, Publisher, RclrsError};
22
use std::{sync::Arc, thread, time::Duration};
33
use std_msgs::msg::String as StringMsg;
44
struct SimplePublisherNode {
@@ -8,9 +8,7 @@ struct SimplePublisherNode {
88
impl SimplePublisherNode {
99
fn new(context: &Context) -> Result<Self, RclrsError> {
1010
let node = create_node(context, "simple_publisher").unwrap();
11-
let _publisher = node
12-
.create_publisher("publish_hello", QOS_PROFILE_DEFAULT)
13-
.unwrap();
11+
let _publisher = node.create_publisher("publish_hello").unwrap();
1412
Ok(Self { node, _publisher })
1513
}
1614

rclrs/src/node.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rosidl_runtime_rs::Message;
1515
pub use self::{builder::*, graph::*, primitive_options::*};
1616
use crate::{
1717
rcl_bindings::*, Client, ClientBase, Clock, Context, ContextHandle, GuardCondition,
18-
ParameterBuilder, ParameterInterface, ParameterVariant, Parameters, Publisher, QoSProfile,
18+
ParameterBuilder, ParameterInterface, ParameterVariant, Parameters, Publisher, PublisherOptions,
1919
RclrsError, Service, ServiceBase, Subscription, SubscriptionBase, SubscriptionCallback,
2020
SubscriptionOptions, TimeSource, ENTITY_LIFECYCLE_MUTEX,
2121
};
@@ -252,19 +252,46 @@ impl Node {
252252
guard_condition
253253
}
254254

255-
/// Creates a [`Publisher`][1].
255+
/// Pass in only the topic name for the `options` argument to use all default publisher options:
256+
/// ```
257+
/// # use rclrs::*;
258+
/// # let context = Context::new([]).unwrap();
259+
/// # let node = create_node(&context, "my_node").unwrap();
260+
/// let publisher = node.create_publisher::<test_msgs::msg::Empty>(
261+
/// "my_topic"
262+
/// )
263+
/// .unwrap();
264+
/// ```
265+
///
266+
/// Take advantage of the [`IntoPrimitiveOptions`] API to easily build up the
267+
/// publisher options:
256268
///
257-
/// [1]: crate::Publisher
258-
// TODO: make publisher's lifetime depend on node's lifetime
259-
pub fn create_publisher<T>(
269+
/// ```
270+
/// # use rclrs::*;
271+
/// # let context = Context::new([]).unwrap();
272+
/// # let node = create_node(&context, "my_node").unwrap();
273+
/// let publisher = node.create_publisher::<test_msgs::msg::Empty>(
274+
/// "my_topic"
275+
/// .keep_last(100)
276+
/// .transient_local()
277+
/// )
278+
/// .unwrap();
279+
///
280+
/// let reliable_publisher = node.create_publisher::<test_msgs::msg::Empty>(
281+
/// "my_topic"
282+
/// .reliable()
283+
/// )
284+
/// .unwrap();
285+
/// ```
286+
///
287+
pub fn create_publisher<'a, T>(
260288
&self,
261-
topic: &str,
262-
qos: QoSProfile,
289+
options: impl Into<PublisherOptions<'a>>,
263290
) -> Result<Arc<Publisher<T>>, RclrsError>
264291
where
265292
T: Message,
266293
{
267-
let publisher = Arc::new(Publisher::<T>::new(Arc::clone(&self.handle), topic, qos)?);
294+
let publisher = Arc::new(Publisher::<T>::new(Arc::clone(&self.handle), options)?);
268295
Ok(publisher)
269296
}
270297

rclrs/src/publisher.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
error::{RclrsError, ToResult},
1212
qos::QoSProfile,
1313
rcl_bindings::*,
14-
NodeHandle, ENTITY_LIFECYCLE_MUTEX,
14+
NodeHandle, ENTITY_LIFECYCLE_MUTEX, IntoPrimitiveOptions,
1515
};
1616

1717
mod loaned_message;
@@ -78,14 +78,14 @@ where
7878
/// Creates a new `Publisher`.
7979
///
8080
/// Node and namespace changes are always applied _before_ topic remapping.
81-
pub(crate) fn new(
81+
pub(crate) fn new<'a>(
8282
node_handle: Arc<NodeHandle>,
83-
topic: &str,
84-
qos: QoSProfile,
83+
options: impl Into<PublisherOptions<'a>>,
8584
) -> Result<Self, RclrsError>
8685
where
8786
T: Message,
8887
{
88+
let PublisherOptions { topic, qos } = options.into();
8989
// SAFETY: Getting a zero-initialized value is always safe.
9090
let mut rcl_publisher = unsafe { rcl_get_zero_initialized_publisher() };
9191
let type_support_ptr =
@@ -231,6 +231,28 @@ where
231231
}
232232
}
233233

234+
/// `PublisherOptions` are used by [`Node::create_publisher`][1] to initialize
235+
/// a [`Publisher`].
236+
///
237+
/// [1]: crate::NodeState::create_publisher
238+
#[derive(Debug, Clone)]
239+
#[non_exhaustive]
240+
pub struct PublisherOptions<'a> {
241+
/// The topic name for the publisher.
242+
pub topic: &'a str,
243+
/// The quality of service settings for the publisher.
244+
pub qos: QoSProfile,
245+
}
246+
247+
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for PublisherOptions<'a> {
248+
fn from(value: T) -> Self {
249+
let options = value.into_primitive_options();
250+
let mut qos = QoSProfile::topics_default();
251+
options.apply(&mut qos);
252+
Self { topic: options.name, qos }
253+
}
254+
}
255+
234256
/// Convenience trait for [`Publisher::publish`].
235257
pub trait MessageCow<'a, T: Message> {
236258
/// Wrap the owned or borrowed message in a `Cow`.
@@ -262,24 +284,23 @@ mod tests {
262284

263285
#[test]
264286
fn test_publishers() -> Result<(), RclrsError> {
265-
use crate::{TopicEndpointInfo, QOS_PROFILE_SYSTEM_DEFAULT};
287+
use crate::TopicEndpointInfo;
266288
use test_msgs::msg;
267289

268290
let namespace = "/test_publishers_graph";
269291
let graph = construct_test_graph(namespace)?;
270292

271293
let node_1_empty_publisher = graph
272294
.node1
273-
.create_publisher::<msg::Empty>("graph_test_topic_1", QOS_PROFILE_SYSTEM_DEFAULT)?;
295+
.create_publisher::<msg::Empty>("graph_test_topic_1")?;
274296
let topic1 = node_1_empty_publisher.topic_name();
275297
let node_1_basic_types_publisher = graph.node1.create_publisher::<msg::BasicTypes>(
276-
"graph_test_topic_2",
277-
QOS_PROFILE_SYSTEM_DEFAULT,
298+
"graph_test_topic_2"
278299
)?;
279300
let topic2 = node_1_basic_types_publisher.topic_name();
280301
let node_2_default_publisher = graph
281302
.node2
282-
.create_publisher::<msg::Defaults>("graph_test_topic_3", QOS_PROFILE_SYSTEM_DEFAULT)?;
303+
.create_publisher::<msg::Defaults>("graph_test_topic_3")?;
283304
let topic3 = node_2_default_publisher.topic_name();
284305

285306
std::thread::sleep(std::time::Duration::from_millis(100));

0 commit comments

Comments
 (0)