Skip to content

Commit

Permalink
Introduce ServiceWaitable/ClientWaitable/SubscriptionWaitable
Browse files Browse the repository at this point in the history
  • Loading branch information
nnmm committed Jul 8, 2022
1 parent 00f0d5b commit d3497a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
20 changes: 10 additions & 10 deletions rclrs/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use self::service::*;
pub use self::subscription::*;

use crate::rcl_bindings::*;
use crate::{Context, ParameterOverrideMap, QoSProfile, RclrsError, ToResult, Waitable};
use crate::{Context, ParameterOverrideMap, QoSProfile, RclrsError, ToResult};

use std::cmp::PartialEq;
use std::ffi::CStr;
Expand Down Expand Up @@ -72,9 +72,9 @@ unsafe impl Send for rcl_node_t {}
pub struct Node {
rcl_node_mtx: Arc<Mutex<rcl_node_t>>,
pub(crate) rcl_context_mtx: Arc<Mutex<rcl_context_t>>,
pub(crate) clients: Vec<Weak<dyn Waitable>>,
pub(crate) services: Vec<Weak<dyn Waitable>>,
pub(crate) subscriptions: Vec<Weak<dyn Waitable>>,
pub(crate) clients: Vec<Weak<dyn ClientWaitable>>,
pub(crate) services: Vec<Weak<dyn ServiceWaitable>>,
pub(crate) subscriptions: Vec<Weak<dyn SubscriptionWaitable>>,
_parameter_map: ParameterOverrideMap,
}

Expand Down Expand Up @@ -193,7 +193,7 @@ impl Node {
{
let client = Arc::new(crate::node::client::Client::<T>::new(self, topic)?);
self.clients
.push(Arc::downgrade(&client) as Weak<dyn Waitable>);
.push(Arc::downgrade(&client) as Weak<dyn ClientWaitable>);
Ok(client)
}

Expand Down Expand Up @@ -229,7 +229,7 @@ impl Node {
self, topic, callback,
)?);
self.services
.push(Arc::downgrade(&service) as Weak<dyn Waitable>);
.push(Arc::downgrade(&service) as Weak<dyn ServiceWaitable>);
Ok(service)
}

Expand All @@ -249,23 +249,23 @@ impl Node {
{
let subscription = Arc::new(Subscription::<T>::new(self, topic, qos, callback)?);
self.subscriptions
.push(Arc::downgrade(&subscription) as Weak<dyn Waitable>);
.push(Arc::downgrade(&subscription) as Weak<dyn SubscriptionWaitable>);
Ok(subscription)
}

/// Returns the subscriptions that have not been dropped yet.
pub(crate) fn live_subscriptions(&self) -> Vec<Arc<dyn Waitable>> {
pub(crate) fn live_subscriptions(&self) -> Vec<Arc<dyn SubscriptionWaitable>> {
self.subscriptions
.iter()
.filter_map(Weak::upgrade)
.collect()
}

pub(crate) fn live_clients(&self) -> Vec<Arc<dyn Waitable>> {
pub(crate) fn live_clients(&self) -> Vec<Arc<dyn ClientWaitable>> {
self.clients.iter().filter_map(Weak::upgrade).collect()
}

pub(crate) fn live_services(&self) -> Vec<Arc<dyn Waitable>> {
pub(crate) fn live_services(&self) -> Vec<Arc<dyn ServiceWaitable>> {
self.services.iter().filter_map(Weak::upgrade).collect()
}

Expand Down
5 changes: 5 additions & 0 deletions rclrs/src/node/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ where
}
}

/// A marker trait to distinguish `Client` waitables from other [`Waitable`]s.
pub(crate) trait ClientWaitable: Waitable {}

impl<T> ClientWaitable for Client<T> where T: rosidl_runtime_rs::Service {}

impl<T> Client<T>
where
T: rosidl_runtime_rs::Service,
Expand Down
5 changes: 5 additions & 0 deletions rclrs/src/node/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ where
}
}

/// A marker trait to distinguish `Service` waitables from other [`Waitable`]s.
pub(crate) trait ServiceWaitable: Waitable {}

impl<T> ServiceWaitable for Service<T> where T: rosidl_runtime_rs::Service {}

impl<T> Service<T>
where
T: rosidl_runtime_rs::Service,
Expand Down
5 changes: 5 additions & 0 deletions rclrs/src/node/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ where
}
}

/// A marker trait to distinguish `Subscription` waitables from other [`Waitable`]s.
pub(crate) trait SubscriptionWaitable: Waitable {}

impl<T> SubscriptionWaitable for Subscription<T> where T: Message {}

impl<T> Subscription<T>
where
T: Message,
Expand Down

0 comments on commit d3497a3

Please sign in to comment.