Skip to content

Commit 1439045

Browse files
Minor cleanups
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent 9ab8dc5 commit 1439045

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

rclrs/src/dynamic_message.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ pub struct DynamicMessageMetadata {
7070
introspection_type_support_library: Arc<libloading::Library>,
7171
type_support_ptr: *const rosidl_message_type_support_t,
7272
structure: MessageStructure,
73-
fini_function: unsafe extern "C" fn(*mut std::os::raw::c_void),
73+
// The message content can be moved out into another message,
74+
// in which case the drop impl is not responsible for calling fini anymore
75+
fini_function: Option<unsafe extern "C" fn(*mut std::os::raw::c_void)>,
7476
}
7577

7678
/// A message whose type is not known at compile-time.
@@ -85,9 +87,6 @@ pub struct DynamicMessage {
8587
// This is aligned to the maximum possible alignment of a message (8)
8688
// by the use of a special allocation function.
8789
storage: Box<[u8]>,
88-
// This type allows moving the message contents out into another message,
89-
// in which case the drop impl is not responsible for calling fini anymore
90-
needs_fini: bool,
9190
}
9291

9392
// ========================= impl for DynamicMessagePackage =========================
@@ -201,16 +200,14 @@ impl DynamicMessagePackage {
201200
unsafe { &*(type_support.data as *const rosidl_message_members_t) };
202201
// SAFETY: The message members coming from a type support library will always be valid.
203202
let structure = unsafe { MessageStructure::from_rosidl_message_members(message_members) };
204-
// The fini function will always exist.
205-
let fini_function = message_members.fini_function.unwrap();
206203
let metadata = DynamicMessageMetadata {
207204
message_type,
208205
introspection_type_support_library: Arc::clone(
209206
&self.introspection_type_support_library,
210207
),
211208
type_support_ptr,
212209
structure,
213-
fini_function,
210+
fini_function: message_members.fini_function,
214211
};
215212
Ok(metadata)
216213
}
@@ -314,7 +311,6 @@ impl DynamicMessageMetadata {
314311
let dyn_msg = DynamicMessage {
315312
metadata: self.clone(),
316313
storage,
317-
needs_fini: true,
318314
};
319315
Ok(dyn_msg)
320316
}
@@ -336,9 +332,9 @@ impl Deref for DynamicMessage {
336332

337333
impl Drop for DynamicMessage {
338334
fn drop(&mut self) {
339-
if self.needs_fini {
335+
if let Some(fini_function) = self.metadata.fini_function {
340336
// SAFETY: The fini_function expects to be passed a pointer to the message
341-
unsafe { (self.metadata.fini_function)(self.storage.as_mut_ptr() as _) }
337+
unsafe { (fini_function)(self.storage.as_mut_ptr() as _) }
342338
}
343339
}
344340
}
@@ -475,7 +471,7 @@ impl DynamicMessage {
475471
dest_slice.copy_from_slice(&self.storage);
476472
// Don't run the fini function on the src data anymore, because the inner parts would be
477473
// double-freed by dst and src.
478-
self.needs_fini = false;
474+
self.metadata.fini_function = None;
479475
Ok(dest)
480476
} else {
481477
Err(self)

rclrs/src/dynamic_message/dynamic_subscription.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::any::Any;
22
use std::boxed::Box;
3-
use std::ffi::{CStr, CString};
3+
use std::ffi::CString;
44
use std::ops::{Deref, DerefMut};
55
use std::sync::{Arc, Mutex};
66

@@ -102,7 +102,7 @@ impl<Payload> DerefMut for WorkerDynamicSubscriptionCallback<Payload> {
102102
}
103103
}
104104

105-
pub enum DynamicSubscriptionCallback<Payload> {
105+
pub(crate) enum DynamicSubscriptionCallback<Payload> {
106106
/// A callback with the message and the message info as arguments.
107107
Node(NodeAsyncDynamicSubscriptionCallback),
108108
/// A callback with the payload, message, and the message info as arguments.

0 commit comments

Comments
 (0)