@@ -8,36 +8,14 @@ use std::ffi::CString;
8
8
use std:: marker:: PhantomData ;
9
9
use std:: sync:: Arc ;
10
10
11
- use parking_lot:: { Mutex , MutexGuard } ;
11
+ use parking_lot:: Mutex ;
12
12
13
13
use rosidl_runtime_rs:: { Message , RmwMessage } ;
14
14
15
15
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
16
16
// they are running in. Therefore, this type can be safely sent to another thread.
17
17
unsafe impl Send for rcl_publisher_t { }
18
18
19
- pub ( crate ) struct PublisherHandle {
20
- rcl_publisher_mtx : Mutex < rcl_publisher_t > ,
21
- rcl_node_mtx : Arc < Mutex < rcl_node_t > > ,
22
- }
23
-
24
- impl PublisherHandle {
25
- fn lock ( & self ) -> MutexGuard < rcl_publisher_t > {
26
- self . rcl_publisher_mtx . lock ( )
27
- }
28
- }
29
-
30
- impl Drop for PublisherHandle {
31
- fn drop ( & mut self ) {
32
- let rcl_publisher = self . rcl_publisher_mtx . get_mut ( ) ;
33
- let rcl_node = & mut * self . rcl_node_mtx . lock ( ) ;
34
- // SAFETY: No preconditions for this function (besides the arguments being valid).
35
- unsafe {
36
- rcl_publisher_fini ( rcl_publisher as * mut _ , rcl_node as * mut _ ) ;
37
- }
38
- }
39
- }
40
-
41
19
/// Struct for sending messages of type `T`.
42
20
///
43
21
/// Multiple publishers can be created for the same topic, in different nodes or the same node.
@@ -52,10 +30,26 @@ pub struct Publisher<T>
52
30
where
53
31
T : Message ,
54
32
{
55
- pub ( crate ) handle : Arc < PublisherHandle > ,
33
+ rcl_publisher_mtx : Mutex < rcl_publisher_t > ,
34
+ rcl_node_mtx : Arc < Mutex < rcl_node_t > > ,
56
35
message : PhantomData < T > ,
57
36
}
58
37
38
+ impl < T > Drop for Publisher < T >
39
+ where
40
+ T : Message ,
41
+ {
42
+ fn drop ( & mut self ) {
43
+ unsafe {
44
+ // SAFETY: No preconditions for this function (besides the arguments being valid).
45
+ rcl_publisher_fini (
46
+ self . rcl_publisher_mtx . get_mut ( ) ,
47
+ & mut * self . rcl_node_mtx . lock ( ) ,
48
+ ) ;
49
+ }
50
+ }
51
+ }
52
+
59
53
impl < T > Publisher < T >
60
54
where
61
55
T : Message ,
96
90
. ok ( ) ?;
97
91
}
98
92
99
- let handle = Arc :: new ( PublisherHandle {
100
- rcl_publisher_mtx : Mutex :: new ( rcl_publisher) ,
101
- rcl_node_mtx : node. rcl_node_mtx . clone ( ) ,
102
- } ) ;
103
-
104
93
Ok ( Self {
105
- handle,
94
+ rcl_publisher_mtx : Mutex :: new ( rcl_publisher) ,
95
+ rcl_node_mtx : Arc :: clone ( & node. rcl_node_mtx ) ,
106
96
message : PhantomData ,
107
97
} )
108
98
}
@@ -125,7 +115,7 @@ where
125
115
/// [1]: https://github.com/ros2/ros2/issues/255
126
116
pub fn publish < ' a , M : MessageCow < ' a , T > > ( & self , message : M ) -> Result < ( ) , RclrsError > {
127
117
let rmw_message = T :: into_rmw_message ( message. into_cow ( ) ) ;
128
- let rcl_publisher = & mut * self . handle . lock ( ) ;
118
+ let rcl_publisher = & mut * self . rcl_publisher_mtx . lock ( ) ;
129
119
let ret = unsafe {
130
120
// SAFETY: The message type is guaranteed to match the publisher type by the type system.
131
121
// The message does not need to be valid beyond the duration of this function call.
0 commit comments