Skip to content

Commit 73022f7

Browse files
API symmetry with normal pub/sub
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent 8112a77 commit 73022f7

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

rclrs/src/dynamic_message/dynamic_publisher.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::error::{RclrsError, ToResult};
99
use crate::rcl_bindings::*;
1010
use crate::{NodeHandle, PublisherHandle, PublisherOptions, ENTITY_LIFECYCLE_MUTEX};
1111

12+
pub type DynamicPublisher = Arc<DynamicPublisherState>;
13+
1214
/// Struct for sending messages of type `T`.
1315
///
1416
/// Multiple publishers can be created for the same topic, in different nodes or the same node.
@@ -19,23 +21,23 @@ use crate::{NodeHandle, PublisherHandle, PublisherOptions, ENTITY_LIFECYCLE_MUTE
1921
/// Sending messages does not require calling [`spin`][1] on the publisher's node.
2022
///
2123
/// [1]: crate::spin
22-
pub struct DynamicPublisher {
24+
pub struct DynamicPublisherState {
2325
handle: PublisherHandle,
2426
metadata: DynamicMessageMetadata,
2527
// This is the regular type support library, not the introspection one.
2628
#[allow(dead_code)]
2729
type_support_library: Arc<libloading::Library>,
2830
}
2931

30-
impl DynamicPublisher {
31-
/// Creates a new `DynamicPublisher`.
32+
impl DynamicPublisherState {
33+
/// Creates a new `DynamicPublisherState`.
3234
///
3335
/// Node and namespace changes are always applied _before_ topic remapping.
34-
pub(crate) fn new<'a>(
36+
pub(crate) fn create<'a>(
3537
topic_type: MessageTypeName,
3638
options: impl Into<PublisherOptions<'a>>,
3739
node_handle: Arc<NodeHandle>,
38-
) -> Result<Self, RclrsError> {
40+
) -> Result<Arc<Self>, RclrsError> {
3941
// This loads the introspection type support library.
4042
let metadata = DynamicMessageMetadata::new(topic_type)?;
4143
let PublisherOptions { topic, qos } = options.into();
@@ -86,14 +88,14 @@ impl DynamicPublisher {
8688
}
8789
}
8890

89-
Ok(Self {
91+
Ok(Arc::new(Self {
9092
handle: PublisherHandle {
9193
rcl_publisher: Mutex::new(rcl_publisher),
9294
node_handle,
9395
},
9496
metadata,
9597
type_support_library,
96-
})
98+
}))
9799
}
98100

99101
/// Returns the topic name of the publisher.

rclrs/src/dynamic_message/dynamic_subscription.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ use super::{
1212
};
1313
use crate::rcl_bindings::*;
1414
use crate::{
15-
MessageInfo, NodeHandle, RclPrimitive, RclPrimitiveHandle, RclPrimitiveKind, RclrsError,
15+
MessageInfo, Node, NodeHandle, RclPrimitive, RclPrimitiveHandle, RclPrimitiveKind, RclrsError,
1616
SubscriptionHandle, SubscriptionOptions, ToResult, Waitable, WaitableLifecycle, WorkScope,
1717
WorkerCommands, ENTITY_LIFECYCLE_MUTEX,
1818
};
1919

20+
pub type DynamicSubscription = Arc<DynamicSubscriptionState<Node>>;
21+
2022
struct DynamicSubscriptionExecutable<Payload> {
2123
handle: Arc<SubscriptionHandle>,
2224
callback: Arc<Mutex<DynamicSubscriptionCallback<Payload>>>,
@@ -153,7 +155,7 @@ impl<Payload: 'static> RclPrimitive for DynamicSubscriptionExecutable<Payload> {
153155
}
154156

155157
/// Struct for receiving messages whose type is only known at runtime.
156-
pub struct DynamicSubscription<Scope>
158+
pub struct DynamicSubscriptionState<Scope>
157159
where
158160
Scope: WorkScope,
159161
{
@@ -174,14 +176,14 @@ where
174176
type_support_library: Arc<libloading::Library>,
175177
}
176178

177-
impl<Scope> DynamicSubscription<Scope>
179+
impl<Scope> DynamicSubscriptionState<Scope>
178180
where
179181
Scope: WorkScope,
180182
{
181183
/// Creates a new dynamic subscription.
182184
///
183185
/// This is not a public function, by the same rationale as `Subscription::new()`.
184-
pub(crate) fn new<'a>(
186+
pub(crate) fn create<'a>(
185187
topic_type: MessageTypeName,
186188
options: impl Into<SubscriptionOptions<'a>>,
187189
callback: impl Into<DynamicSubscriptionCallback<Scope::Payload>>,
@@ -336,11 +338,9 @@ mod tests {
336338
fn assert_send<T: Send>() {}
337339
fn assert_sync<T: Sync>() {}
338340

339-
/*
340341
#[test]
341342
fn dynamic_subscription_is_sync_and_send() {
342343
assert_send::<DynamicSubscription>();
343344
assert_sync::<DynamicSubscription>();
344345
}
345-
*/
346346
}

rclrs/src/node.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ pub use primitive_options::*;
66

77
mod graph;
88
#[cfg(feature = "dyn_msg")]
9-
use crate::dynamic_message::{DynamicMessage, DynamicPublisher, DynamicSubscription};
9+
use crate::dynamic_message::{
10+
DynamicMessage, DynamicPublisher, DynamicPublisherState, DynamicSubscription,
11+
DynamicSubscriptionState, MessageTypeName, NodeDynamicSubscriptionCallback,
12+
};
1013

1114
pub use graph::*;
1215

@@ -33,9 +36,7 @@ use async_std::future::timeout;
3336
use rosidl_runtime_rs::Message;
3437

3538
use crate::{
36-
dynamic_message::{MessageTypeName, NodeDynamicSubscriptionCallback},
37-
rcl_bindings::*,
38-
Client, ClientOptions, ClientState, Clock, ContextHandle, ExecutorCommands,
39+
rcl_bindings::*, Client, ClientOptions, ClientState, Clock, ContextHandle, ExecutorCommands,
3940
IntoAsyncServiceCallback, IntoAsyncSubscriptionCallback, IntoNodeServiceCallback,
4041
IntoNodeSubscriptionCallback, LogParams, Logger, MessageInfo, ParameterBuilder,
4142
ParameterInterface, ParameterVariant, Parameters, Promise, Publisher, PublisherOptions,
@@ -407,7 +408,7 @@ impl NodeState {
407408
topic_type: MessageTypeName,
408409
options: impl Into<PublisherOptions<'a>>,
409410
) -> Result<DynamicPublisher, RclrsError> {
410-
DynamicPublisher::new(topic_type, options, Arc::clone(&self.handle))
411+
DynamicPublisherState::create(topic_type, options, Arc::clone(&self.handle))
411412
}
412413

413414
/// Creates a [`Service`] with an ordinary callback.
@@ -807,18 +808,17 @@ impl NodeState {
807808
topic_type: MessageTypeName,
808809
options: impl Into<SubscriptionOptions<'a>>,
809810
callback: F,
810-
) -> Result<Arc<DynamicSubscription<Node>>, RclrsError>
811+
) -> Result<DynamicSubscription, RclrsError>
811812
where
812813
F: FnMut(DynamicMessage, MessageInfo) -> BoxFuture<'static, ()> + Send + 'static,
813814
{
814-
let subscription = DynamicSubscription::new(
815+
let subscription = DynamicSubscriptionState::<Node>::create(
815816
topic_type,
816817
options,
817818
NodeDynamicSubscriptionCallback(Box::new(callback)),
818819
&self.handle,
819820
self.commands.async_worker_commands(),
820821
)?;
821-
// TODO(luca) similar API to above?
822822
Ok(subscription)
823823
}
824824

0 commit comments

Comments
 (0)