Skip to content

Commit cc59f0a

Browse files
Cleanup remaining warnings / clippy
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent debaac9 commit cc59f0a

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

rclrs/src/clock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::rcl_bindings::*;
22
use crate::{error::ToResult, time::Time, to_rclrs_result, RclrsError};
3-
use std::sync::{Arc, Mutex};
3+
use std::sync::Mutex;
44

55
/// Enum to describe clock type. Redefined for readability and to eliminate the uninitialized case
66
/// from the `rcl_clock_type_t` enum in the binding.

rclrs/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,11 @@ pub fn spin(node: Arc<Node>) -> Result<(), RclrsError> {
116116
/// ```
117117
pub fn create_node(context: &Context, node_name: &str) -> Result<Arc<Node>, RclrsError> {
118118
let node = Arc::new(Node::builder(context, node_name).build()?);
119-
*node._time_source.lock().unwrap() =
120-
Some(TimeSourceBuilder::new(node.clone(), node.get_clock()).build());
119+
*node._time_source.lock().unwrap() = Some(
120+
TimeSourceBuilder::new(node.clone(), node.get_clock())
121+
.build()
122+
.unwrap(),
123+
);
121124
Ok(node)
122125
}
123126

rclrs/src/node.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct Node {
7373
pub(crate) subscriptions_mtx: Mutex<Vec<Weak<dyn SubscriptionBase>>>,
7474
_clock: Arc<Clock>,
7575
// TODO(luca) set to private
76-
pub _time_source: Arc<Mutex<Option<TimeSource>>>,
76+
pub(crate) _time_source: Arc<Mutex<Option<TimeSource>>>,
7777
_parameter_map: ParameterOverrideMap,
7878
}
7979

@@ -363,8 +363,8 @@ impl Node {
363363
domain_id
364364
}
365365

366-
// TODO(luca) There should really be parameter callbacks, this is only for testing
367-
// temporarily
366+
/// Gets a parameter given the name.
367+
/// Returns None if no parameter with the requested name was found.
368368
pub fn get_parameter(&self, name: &str) -> Option<ParameterValue> {
369369
self._parameter_map.get(name).cloned()
370370
}

rclrs/src/time_source.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
clock::{Clock, ClockType},
3-
RclrsError,
4-
};
1+
use crate::clock::{Clock, ClockType};
52
use crate::{Node, ParameterValue, QoSProfile, Subscription, QOS_PROFILE_CLOCK};
63
use rosgraph_msgs::msg::Clock as ClockMsg;
74
use std::fmt;
@@ -72,7 +69,7 @@ impl TimeSourceBuilder {
7269
}
7370

7471
/// Builds the `TimeSource` and attaches the provided `Node` and `Clock`.
75-
pub fn build(self) -> TimeSource {
72+
pub fn build(self) -> Result<TimeSource, ClockMismatchError> {
7673
let mut source = TimeSource {
7774
_node: self.node.clone(),
7875
_clocks: Arc::new(Mutex::new(vec![])),
@@ -82,12 +79,15 @@ impl TimeSourceBuilder {
8279
_clock_subscription: None,
8380
_last_time_msg: Arc::new(Mutex::new(None)),
8481
};
85-
source.attach_clock(self.clock);
82+
source.attach_clock(self.clock)?;
8683
source.attach_node(self.node);
87-
source
84+
Ok(source)
8885
}
8986
}
9087

88+
/// Error created when a non RosTime clock is attached but the `use_sim_time` parameter is set.
89+
/// This case could behave in unexpected ways since the /clock topic would be ignored even though
90+
/// the user explicitly requested to use it through a command line argument.
9191
#[derive(Debug)]
9292
pub struct ClockMismatchError(ClockType);
9393

@@ -104,7 +104,7 @@ impl fmt::Display for ClockMismatchError {
104104
impl TimeSource {
105105
/// Creates a new time source with default parameters.
106106
pub fn new(node: Arc<Node>, clock: Arc<Clock>) -> Self {
107-
TimeSourceBuilder::new(node, clock).build()
107+
TimeSourceBuilder::new(node, clock).build().unwrap()
108108
}
109109

110110
/// Attaches the given clock to the `TimeSource`, enabling the `TimeSource` to control it.
@@ -138,23 +138,22 @@ impl TimeSource {
138138

139139
/// Attaches the given node to to the `TimeSource`, using its interface to read the
140140
/// `use_sim_time` parameter and create the clock subscription.
141-
pub fn attach_node(&mut self, node: Arc<Node>) -> Result<(), RclrsError> {
141+
pub fn attach_node(&mut self, node: Arc<Node>) {
142142
self._node = node;
143143

144144
// TODO(luca) register a parameter callback
145145
if let Some(sim_param) = self._node.get_parameter("use_sim_time") {
146146
match sim_param {
147147
ParameterValue::Bool(val) => {
148-
self.set_ros_time(val)?;
148+
self.set_ros_time(val);
149149
}
150150
// TODO(luca) more graceful error handling?
151151
_ => panic!("use_sim_time parameter must be boolean"),
152152
}
153153
}
154-
Ok(())
155154
}
156155

157-
fn set_ros_time(&mut self, enable: bool) -> Result<(), RclrsError> {
156+
fn set_ros_time(&mut self, enable: bool) {
158157
let updated = self
159158
._ros_time_active
160159
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |prev| {
@@ -169,13 +168,8 @@ impl TimeSource {
169168
for clock in self._clocks.lock().unwrap().iter() {
170169
clock.set_ros_time(enable);
171170
}
172-
self._clock_subscription = if enable {
173-
Some(self.create_clock_sub()?)
174-
} else {
175-
None
176-
};
171+
self._clock_subscription = enable.then(|| self.create_clock_sub());
177172
}
178-
Ok(())
179173
}
180174

181175
fn update_clock(clock: &Arc<Clock>, nanoseconds: i64) {
@@ -189,22 +183,21 @@ impl TimeSource {
189183
}
190184
}
191185

192-
fn create_clock_sub(&self) -> Result<Arc<Subscription<ClockMsg>>, RclrsError> {
186+
fn create_clock_sub(&self) -> Arc<Subscription<ClockMsg>> {
193187
let ros_time_active = self._ros_time_active.clone();
194188
let clocks = self._clocks.clone();
195189
let last_time_msg = self._last_time_msg.clone();
196-
self._node.create_subscription::<ClockMsg, _>(
197-
"/clock",
198-
self._clock_qos,
199-
move |msg: ClockMsg| {
190+
// Safe to unwrap since the function will only fail if invalid arguments are provided
191+
self._node
192+
.create_subscription::<ClockMsg, _>("/clock", self._clock_qos, move |msg: ClockMsg| {
200193
if ros_time_active.load(Ordering::Relaxed) {
201194
let nanoseconds: i64 =
202195
(msg.clock.sec as i64 * 1_000_000_000) + msg.clock.nanosec as i64;
203196
*last_time_msg.lock().unwrap() = Some(msg);
204197
Self::update_all_clocks(&clocks, nanoseconds);
205198
}
206-
},
207-
)
199+
})
200+
.unwrap()
208201
}
209202
}
210203

0 commit comments

Comments
 (0)