Skip to content

Commit 0c11ea0

Browse files
Minor cleanups and refactors
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent d1c264a commit 0c11ea0

File tree

4 files changed

+46
-68
lines changed

4 files changed

+46
-68
lines changed

rclrs/src/dynamic_message/dynamic_subscription.rs

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,40 @@ struct DynamicSubscriptionExecutable<Payload> {
2828
metadata: Arc<DynamicMessageMetadata>,
2929
}
3030

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>,
3533
);
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>,
3843
);
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>,
4155
);
4256

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+
4365
impl Deref for NodeDynamicSubscriptionCallback {
4466
type Target = Box<dyn Fn(DynamicMessage, MessageInfo) + 'static + Send + Sync>;
4567
fn deref(&self) -> &Self::Target {
@@ -304,61 +326,13 @@ where
304326
/// This returns the topic name after remapping, so it is not necessarily the
305327
/// topic name which was used when creating the subscription.
306328
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()
315330
}
316331

317332
/// Returns a description of the message structure.
318333
pub fn structure(&self) -> &MessageStructure {
319334
&self.metadata.structure
320335
}
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-
}
362336
}
363337

364338
#[cfg(test)]

rclrs/src/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ impl NodeState {
816816
DynamicSubscriptionState::<Node>::create(
817817
topic_type,
818818
options,
819-
NodeDynamicSubscriptionCallback(Box::new(callback)),
819+
NodeDynamicSubscriptionCallback::new(callback),
820820
&self.handle,
821821
self.commands.async_worker_commands(),
822822
)
@@ -835,7 +835,7 @@ impl NodeState {
835835
DynamicSubscriptionState::<Node>::create(
836836
topic_type,
837837
options,
838-
NodeAsyncDynamicSubscriptionCallback(Box::new(callback)),
838+
NodeAsyncDynamicSubscriptionCallback::new(callback),
839839
&self.handle,
840840
self.commands.async_worker_commands(),
841841
)

rclrs/src/subscription.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,7 @@ where
100100
/// This returns the topic name after remapping, so it is not necessarily the
101101
/// topic name which was used when creating the subscription.
102102
pub fn topic_name(&self) -> String {
103-
// SAFETY: The subscription handle is valid because its lifecycle is managed by an Arc.
104-
// The unsafe variables get converted to safe types before being returned
105-
unsafe {
106-
let raw_topic_pointer = rcl_subscription_get_topic_name(&*self.handle.lock());
107-
CStr::from_ptr(raw_topic_pointer)
108-
}
109-
.to_string_lossy()
110-
.into_owned()
103+
self.handle.topic_name()
111104
}
112105

113106
/// Used by [`Node`][crate::Node] to create a new subscription.
@@ -333,6 +326,17 @@ impl SubscriptionHandle {
333326
self.rcl_subscription.lock().unwrap()
334327
}
335328

329+
pub(crate) fn topic_name(&self) -> String {
330+
// SAFETY: The subscription handle is valid because its lifecycle is managed by an Arc.
331+
// The unsafe variables get converted to safe types before being returned
332+
unsafe {
333+
let raw_topic_pointer = rcl_subscription_get_topic_name(&*self.lock());
334+
CStr::from_ptr(raw_topic_pointer)
335+
}
336+
.to_string_lossy()
337+
.into_owned()
338+
}
339+
336340
/// Fetches a new message.
337341
///
338342
/// When there is no new message, this will return a

rclrs/src/worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl<Payload: 'static + Send + Sync> WorkerState<Payload> {
282282
DynamicSubscriptionState::<Worker<Payload>>::create(
283283
topic_type,
284284
options,
285-
WorkerDynamicSubscriptionCallback(Box::new(callback)),
285+
WorkerDynamicSubscriptionCallback::new(callback),
286286
self.node.handle(),
287287
&self.commands,
288288
)

0 commit comments

Comments
 (0)