Skip to content

Commit c5001db

Browse files
committed
Revert changes. Declare waitables new function with pub(crate) visibility
1 parent ed3a391 commit c5001db

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

rclrs/src/node.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,17 @@ impl Node {
184184
///
185185
/// [1]: crate::Client
186186
// TODO: make client's lifetime depend on node's lifetime
187-
pub fn create_client<T>(&mut self, topic: &str) -> Result<Arc<Client<T>>, RclrsError>
187+
pub fn create_client<T>(
188+
&mut self,
189+
topic: &str,
190+
) -> Result<Arc<crate::node::client::Client<T>>, RclrsError>
188191
where
189192
T: rosidl_runtime_rs::Service,
190193
{
191-
Client::<T>::new(self, topic)
194+
let client = Arc::new(crate::node::client::Client::<T>::new(self, topic)?);
195+
self.clients
196+
.push(Arc::downgrade(&client) as Weak<dyn ClientBase>);
197+
Ok(client)
192198
}
193199

194200
/// Creates a [`Publisher`][1].
@@ -214,12 +220,17 @@ impl Node {
214220
&mut self,
215221
topic: &str,
216222
callback: F,
217-
) -> Result<Arc<Service<T>>, RclrsError>
223+
) -> Result<Arc<crate::node::service::Service<T>>, RclrsError>
218224
where
219225
T: rosidl_runtime_rs::Service,
220226
F: Fn(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,
221227
{
222-
Service::<T>::new(self, topic, callback)
228+
let service = Arc::new(crate::node::service::Service::<T>::new(
229+
self, topic, callback,
230+
)?);
231+
self.services
232+
.push(Arc::downgrade(&service) as Weak<dyn ServiceBase>);
233+
Ok(service)
223234
}
224235

225236
/// Creates a [`Subscription`][1].
@@ -236,7 +247,10 @@ impl Node {
236247
T: Message,
237248
F: FnMut(T) + 'static + Send,
238249
{
239-
Subscription::new(self, topic, qos, callback)
250+
let subscription = Arc::new(Subscription::<T>::new(self, topic, qos, callback)?);
251+
self.subscriptions
252+
.push(Arc::downgrade(&subscription) as Weak<dyn SubscriptionBase>);
253+
Ok(subscription)
240254
}
241255

242256
/// Returns the subscriptions that have not been dropped yet.

rclrs/src/node/client.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where
7070
T: rosidl_runtime_rs::Service,
7171
{
7272
/// Creates a new client.
73-
pub fn new(node: &mut Node, topic: &str) -> Result<Arc<Self>, RclrsError>
73+
pub(crate) fn new(node: &Node, topic: &str) -> Result<Self, RclrsError>
7474
where
7575
T: rosidl_runtime_rs::Service,
7676
{
@@ -107,15 +107,14 @@ where
107107
rcl_node_mtx: node.rcl_node_mtx.clone(),
108108
in_use_by_wait_set: Arc::new(AtomicBool::new(false)),
109109
});
110-
let client = Arc::new(Self {
110+
111+
Ok(Self {
111112
handle,
112113
requests: Mutex::new(HashMap::new()),
113114
futures: Arc::new(Mutex::new(
114115
HashMap::<RequestId, oneshot::Sender<T::Response>>::new(),
115116
)),
116-
});
117-
node.clients.push(Arc::downgrade(&client) as _);
118-
Ok(client)
117+
})
119118
}
120119

121120
/// Sends a request with a callback to be called with the response.

rclrs/src/node/service.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ where
6969
T: rosidl_runtime_rs::Service,
7070
{
7171
/// Creates a new service.
72-
pub fn new<F>(node: &mut Node, topic: &str, callback: F) -> Result<Arc<Self>, RclrsError>
72+
pub(crate) fn new<F>(node: &Node, topic: &str, callback: F) -> Result<Self, RclrsError>
7373
where
7474
T: rosidl_runtime_rs::Service,
7575
F: Fn(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,
@@ -107,13 +107,11 @@ where
107107
node_handle: node.rcl_node_mtx.clone(),
108108
in_use_by_wait_set: Arc::new(AtomicBool::new(false)),
109109
});
110-
let service = Arc::new(Self {
110+
111+
Ok(Self {
111112
handle,
112113
callback: Mutex::new(Box::new(callback)),
113-
});
114-
node.services.push(Arc::downgrade(&service) as _);
115-
116-
Ok(service)
114+
})
117115
}
118116

119117
/// Fetches a new request.

rclrs/src/node/subscription.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ where
7676
T: Message,
7777
{
7878
/// Creates a new subscription.
79-
pub fn new<F>(
80-
node: &mut Node,
79+
pub(crate) fn new<F>(
80+
node: &Node,
8181
topic: &str,
8282
qos: QoSProfile,
8383
callback: F,
84-
) -> Result<Arc<Self>, RclrsError>
84+
) -> Result<Self, RclrsError>
8585
where
8686
T: Message,
8787
F: FnMut(T) + 'static + Send,
@@ -120,13 +120,12 @@ where
120120
rcl_node_mtx: node.rcl_node_mtx.clone(),
121121
in_use_by_wait_set: Arc::new(AtomicBool::new(false)),
122122
});
123-
let subscription = Arc::new(Self {
123+
124+
Ok(Self {
124125
handle,
125126
callback: Mutex::new(Box::new(callback)),
126127
message: PhantomData,
127-
});
128-
node.subscriptions.push(Arc::downgrade(&subscription) as _);
129-
Ok(subscription)
128+
})
130129
}
131130

132131
/// Returns the topic name of the subscription.
@@ -220,9 +219,8 @@ mod tests {
220219
fn test_instantiate_subscriber() -> Result<(), RclrsError> {
221220
let context =
222221
Context::new(vec![]).expect("Context instantiation is expected to be a success");
223-
let mut node = create_node(&context, "test_new_subscriber")?;
224-
let _subscriber = Subscription::<std_msgs::msg::String>::new(
225-
&mut node,
222+
let node = create_node(&context, "test_new_subscriber")?;
223+
let _subscriber = node.create_subscription::<std_msgs::msg::String, _>(
226224
"test",
227225
QOS_PROFILE_DEFAULT,
228226
move |_: std_msgs::msg::String| {},

0 commit comments

Comments
 (0)