@@ -28,18 +28,40 @@ struct DynamicSubscriptionExecutable<Payload> {
28
28
metadata : Arc < DynamicMessageMetadata > ,
29
29
}
30
30
31
- // TODO(luca) consider making these enums if we want different callback types
32
- // TODO(luca) make fields private
33
- pub struct NodeDynamicSubscriptionCallback (
34
- pub Box < dyn Fn ( DynamicMessage , MessageInfo ) + Send + Sync > ,
31
+ pub ( crate ) struct NodeDynamicSubscriptionCallback (
32
+ Box < dyn Fn ( DynamicMessage , MessageInfo ) + Send + Sync > ,
35
33
) ;
36
- pub struct NodeAsyncDynamicSubscriptionCallback (
37
- pub Box < dyn FnMut ( DynamicMessage , MessageInfo ) -> BoxFuture < ' static , ( ) > + Send + Sync > ,
34
+
35
+ impl NodeDynamicSubscriptionCallback {
36
+ pub ( crate ) fn new ( f : impl Fn ( DynamicMessage , MessageInfo ) + Send + Sync + ' static ) -> Self {
37
+ NodeDynamicSubscriptionCallback ( Box :: new ( f) )
38
+ }
39
+ }
40
+
41
+ pub ( crate ) struct NodeAsyncDynamicSubscriptionCallback (
42
+ Box < dyn FnMut ( DynamicMessage , MessageInfo ) -> BoxFuture < ' static , ( ) > + Send + Sync > ,
38
43
) ;
39
- pub struct WorkerDynamicSubscriptionCallback < Payload > (
40
- pub Box < dyn FnMut ( & mut Payload , DynamicMessage , MessageInfo ) + Send + Sync > ,
44
+
45
+ impl NodeAsyncDynamicSubscriptionCallback {
46
+ pub ( crate ) fn new (
47
+ f : impl FnMut ( DynamicMessage , MessageInfo ) -> BoxFuture < ' static , ( ) > + Send + Sync + ' static ,
48
+ ) -> Self {
49
+ NodeAsyncDynamicSubscriptionCallback ( Box :: new ( f) )
50
+ }
51
+ }
52
+
53
+ pub ( crate ) struct WorkerDynamicSubscriptionCallback < Payload > (
54
+ Box < dyn FnMut ( & mut Payload , DynamicMessage , MessageInfo ) + Send + Sync > ,
41
55
) ;
42
56
57
+ impl < Payload > WorkerDynamicSubscriptionCallback < Payload > {
58
+ pub ( crate ) fn new (
59
+ f : impl FnMut ( & mut Payload , DynamicMessage , MessageInfo ) + Send + Sync + ' static ,
60
+ ) -> Self {
61
+ WorkerDynamicSubscriptionCallback ( Box :: new ( f) )
62
+ }
63
+ }
64
+
43
65
impl Deref for NodeDynamicSubscriptionCallback {
44
66
type Target = Box < dyn Fn ( DynamicMessage , MessageInfo ) + ' static + Send + Sync > ;
45
67
fn deref ( & self ) -> & Self :: Target {
@@ -304,61 +326,13 @@ where
304
326
/// This returns the topic name after remapping, so it is not necessarily the
305
327
/// topic name which was used when creating the subscription.
306
328
pub fn topic_name ( & self ) -> String {
307
- // SAFETY: No preconditions for the function used
308
- // The unsafe variables get converted to safe types before being returned
309
- unsafe {
310
- let raw_topic_pointer = rcl_subscription_get_topic_name ( & * self . handle . lock ( ) ) ;
311
- CStr :: from_ptr ( raw_topic_pointer)
312
- . to_string_lossy ( )
313
- . into_owned ( )
314
- }
329
+ self . handle . topic_name ( )
315
330
}
316
331
317
332
/// Returns a description of the message structure.
318
333
pub fn structure ( & self ) -> & MessageStructure {
319
334
& self . metadata . structure
320
335
}
321
-
322
- /// Fetches a new message.
323
- ///
324
- /// When there is no new message, this will return a
325
- /// [`SubscriptionTakeFailed`][1].
326
- ///
327
- /// [1]: crate::RclrsError
328
- //
329
- // ```text
330
- // +-------------+
331
- // | rclrs::take |
332
- // +------+------+
333
- // |
334
- // |
335
- // +------v------+
336
- // | rcl_take |
337
- // +------+------+
338
- // |
339
- // |
340
- // +------v------+
341
- // | rmw_take |
342
- // +-------------+
343
- // ```
344
- pub fn take ( & self ) -> Result < DynamicMessage , RclrsError > {
345
- let mut dynamic_message = self . metadata . create ( ) ?;
346
- let rmw_message = dynamic_message. storage . as_mut_ptr ( ) ;
347
- let rcl_subscription = & mut * self . handle . lock ( ) ;
348
- unsafe {
349
- // SAFETY: The first two pointers are valid/initialized, and do not need to be valid
350
- // beyond the function call.
351
- // The latter two pointers are explicitly allowed to be NULL.
352
- rcl_take (
353
- rcl_subscription,
354
- rmw_message as * mut _ ,
355
- std:: ptr:: null_mut ( ) ,
356
- std:: ptr:: null_mut ( ) ,
357
- )
358
- . ok ( ) ?
359
- } ;
360
- Ok ( dynamic_message)
361
- }
362
336
}
363
337
364
338
#[ cfg( test) ]
0 commit comments