1
1
mod builder;
2
2
mod graph;
3
+ mod primitive_options;
3
4
use std:: {
4
5
cmp:: PartialEq ,
5
6
ffi:: CStr ,
@@ -11,12 +12,12 @@ use std::{
11
12
12
13
use rosidl_runtime_rs:: Message ;
13
14
14
- pub use self :: { builder:: * , graph:: * } ;
15
+ pub use self :: { builder:: * , graph:: * , primitive_options :: * } ;
15
16
use crate :: {
16
17
rcl_bindings:: * , Client , ClientBase , Clock , Context , ContextHandle , GuardCondition ,
17
18
ParameterBuilder , ParameterInterface , ParameterVariant , Parameters , Publisher , QoSProfile ,
18
19
RclrsError , Service , ServiceBase , Subscription , SubscriptionBase , SubscriptionCallback ,
19
- TimeSource , ENTITY_LIFECYCLE_MUTEX ,
20
+ SubscriptionOptions , TimeSource , ENTITY_LIFECYCLE_MUTEX ,
20
21
} ;
21
22
22
23
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
@@ -292,21 +293,56 @@ impl Node {
292
293
293
294
/// Creates a [`Subscription`][1].
294
295
///
295
- /// [1]: crate::Subscription
296
- // TODO: make subscription's lifetime depend on node's lifetime
297
- pub fn create_subscription < T , Args > (
296
+ ///
297
+ /// Pass in only the topic name for the `options` argument to use all default subscription options:
298
+ /// ```
299
+ /// # use rclrs::*;
300
+ /// # let context = Context::new([]).unwrap();
301
+ /// # let node = create_node(&context, "my_node").unwrap();
302
+ /// let subscription = node.create_subscription(
303
+ /// "my_subscription",
304
+ /// |_msg: test_msgs::msg::Empty| {
305
+ /// println!("Received message!");
306
+ /// },
307
+ /// );
308
+ /// ```
309
+ ///
310
+ /// Take advantage of [`IntoPrimitiveOptions`] to easily build up the
311
+ /// subscription options:
312
+ ///
313
+ /// ```
314
+ /// # use rclrs::*;
315
+ /// # let context = Context::new([]).unwrap();
316
+ /// # let node = create_node(&context, "my_node").unwrap();
317
+ /// let subscription = node.create_subscription(
318
+ /// "my_subscription"
319
+ /// .keep_last(100)
320
+ /// .transient_local(),
321
+ /// |_msg: test_msgs::msg::Empty| {
322
+ /// println!("Received message!");
323
+ /// },
324
+ /// );
325
+ ///
326
+ /// let reliable_subscription = node.create_subscription(
327
+ /// "my_reliable_subscription"
328
+ /// .reliable(),
329
+ /// |_msg: test_msgs::msg::Empty| {
330
+ /// println!("Received message!");
331
+ /// },
332
+ /// );
333
+ /// ```
334
+ ///
335
+ pub fn create_subscription < ' a , T , Args > (
298
336
& self ,
299
- topic : & str ,
300
- qos : QoSProfile ,
337
+ options : impl Into < SubscriptionOptions < ' a > > ,
301
338
callback : impl SubscriptionCallback < T , Args > ,
302
339
) -> Result < Arc < Subscription < T > > , RclrsError >
303
340
where
304
341
T : Message ,
305
342
{
306
343
let subscription = Arc :: new ( Subscription :: < T > :: new (
307
344
Arc :: clone ( & self . handle ) ,
308
- topic,
309
- qos,
345
+ options,
310
346
callback,
311
347
) ?) ;
312
348
{ self . subscriptions_mtx . lock ( ) }
@@ -461,8 +497,7 @@ pub(crate) unsafe fn call_string_getter_with_rcl_node(
461
497
462
498
#[ cfg( test) ]
463
499
mod tests {
464
- use super :: * ;
465
- use crate :: test_helpers:: * ;
500
+ use crate :: { test_helpers:: * , * } ;
466
501
467
502
#[ test]
468
503
fn traits ( ) {
@@ -472,25 +507,20 @@ mod tests {
472
507
473
508
#[ test]
474
509
fn test_topic_names_and_types ( ) -> Result < ( ) , RclrsError > {
475
- use crate :: QOS_PROFILE_SYSTEM_DEFAULT ;
476
510
use test_msgs:: msg;
477
511
478
512
let graph = construct_test_graph ( "test_topics_graph" ) ?;
479
513
480
514
let _node_1_defaults_subscription = graph. node1 . create_subscription :: < msg:: Defaults , _ > (
481
515
"graph_test_topic_3" ,
482
- QOS_PROFILE_SYSTEM_DEFAULT ,
483
516
|_msg : msg:: Defaults | { } ,
484
517
) ?;
485
- let _node_2_empty_subscription = graph. node2 . create_subscription :: < msg:: Empty , _ > (
486
- "graph_test_topic_1" ,
487
- QOS_PROFILE_SYSTEM_DEFAULT ,
488
- |_msg : msg:: Empty | { } ,
489
- ) ?;
518
+ let _node_2_empty_subscription = graph
519
+ . node2
520
+ . create_subscription :: < msg:: Empty , _ > ( "graph_test_topic_1" , |_msg : msg:: Empty | { } ) ?;
490
521
let _node_2_basic_types_subscription =
491
522
graph. node2 . create_subscription :: < msg:: BasicTypes , _ > (
492
523
"graph_test_topic_2" ,
493
- QOS_PROFILE_SYSTEM_DEFAULT ,
494
524
|_msg : msg:: BasicTypes | { } ,
495
525
) ?;
496
526
0 commit comments