@@ -70,9 +70,7 @@ 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- // 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 ) > ,
73+ fini_function : unsafe extern "C" fn ( * mut std:: os:: raw:: c_void ) ,
7674}
7775
7876/// A message whose type is not known at compile-time.
@@ -87,6 +85,9 @@ pub struct DynamicMessage {
8785 // This is aligned to the maximum possible alignment of a message (8)
8886 // by the use of a special allocation function.
8987 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 ,
9091}
9192
9293// ========================= impl for DynamicMessagePackage =========================
@@ -200,14 +201,16 @@ impl DynamicMessagePackage {
200201 unsafe { & * ( type_support. data as * const rosidl_message_members_t ) } ;
201202 // SAFETY: The message members coming from a type support library will always be valid.
202203 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 ( ) ;
203206 let metadata = DynamicMessageMetadata {
204207 message_type,
205208 introspection_type_support_library : Arc :: clone (
206209 & self . introspection_type_support_library ,
207210 ) ,
208211 type_support_ptr,
209212 structure,
210- fini_function : message_members . fini_function ,
213+ fini_function,
211214 } ;
212215 Ok ( metadata)
213216 }
@@ -311,6 +314,7 @@ impl DynamicMessageMetadata {
311314 let dyn_msg = DynamicMessage {
312315 metadata : self . clone ( ) ,
313316 storage,
317+ needs_fini : true ,
314318 } ;
315319 Ok ( dyn_msg)
316320 }
@@ -332,9 +336,9 @@ impl Deref for DynamicMessage {
332336
333337impl Drop for DynamicMessage {
334338 fn drop ( & mut self ) {
335- if let Some ( fini_function ) = self . metadata . fini_function {
339+ if self . needs_fini {
336340 // SAFETY: The fini_function expects to be passed a pointer to the message
337- unsafe { ( fini_function) ( self . storage . as_mut_ptr ( ) as _ ) }
341+ unsafe { ( self . metadata . fini_function ) ( self . storage . as_mut_ptr ( ) as _ ) }
338342 }
339343 }
340344}
@@ -471,7 +475,7 @@ impl DynamicMessage {
471475 dest_slice. copy_from_slice ( & self . storage ) ;
472476 // Don't run the fini function on the src data anymore, because the inner parts would be
473477 // double-freed by dst and src.
474- self . metadata . fini_function = None ;
478+ self . needs_fini = false ;
475479 Ok ( dest)
476480 } else {
477481 Err ( self )
0 commit comments