Skip to content

Commit d3585ca

Browse files
Minor cleanups, remove several unwraps
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent e34a4b9 commit d3585ca

File tree

4 files changed

+39
-52
lines changed

4 files changed

+39
-52
lines changed

rclrs/src/parameter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub(crate) struct ParameterMap {
492492
impl ParameterMap {
493493
/// Validates the requested parameter setting and returns an error if the requested value is
494494
/// not valid.
495-
pub(crate) fn validate_parameter_setting(
495+
fn validate_parameter_setting(
496496
&self,
497497
name: &str,
498498
value: RmwParameterValue,

rclrs/src/parameter/service.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ impl ParameterService {
4343
.names
4444
.into_iter()
4545
.map(|name| {
46-
// TODO(luca) Remove conversion to string and implement Borrow
47-
let storage = map.storage.get(name.to_cstr().to_str().unwrap())?;
46+
let name = name.to_cstr().to_str().ok()?;
47+
let storage = map.storage.get(name)?;
4848
let mut descriptor = match storage {
4949
ParameterStorage::Declared(storage) => {
5050
let (integer_range, floating_point_range) =
5151
storage.options.ranges.to_descriptor_ranges();
5252
ParameterDescriptor {
53-
name,
53+
name: name.into(),
5454
type_: Default::default(),
5555
description: storage.options.description.clone().into(),
5656
additional_constraints: storage
@@ -65,7 +65,7 @@ impl ParameterService {
6565
}
6666
}
6767
ParameterStorage::Undeclared(_) => ParameterDescriptor {
68-
name,
68+
name: name.into(),
6969
dynamic_typing: true,
7070
..Default::default()
7171
},
@@ -88,10 +88,8 @@ impl ParameterService {
8888
.names
8989
.into_iter()
9090
.map(|name| {
91-
// TODO(luca) Remove conversion to string and implement Borrow
92-
map.storage
93-
.get(name.to_cstr().to_str().unwrap())
94-
.map(|s| s.to_parameter_type())
91+
let name = name.to_cstr().to_str().ok()?;
92+
map.storage.get(name).map(|s| s.to_parameter_type())
9593
})
9694
.collect::<Option<_>>()
9795
.unwrap_or_default();
@@ -107,9 +105,8 @@ impl ParameterService {
107105
.names
108106
.into_iter()
109107
.map(|name| {
110-
// TODO(luca) Remove conversion to string and implement Borrow
111-
let storage = map.storage.get(name.to_cstr().to_str().unwrap())?;
112-
match storage {
108+
let name = name.to_cstr().to_str().ok()?;
109+
match map.storage.get(name)? {
113110
ParameterStorage::Declared(storage) => match &storage.value {
114111
DeclaredValue::Mandatory(v) => {
115112
Some(v.read().unwrap().clone().into())
@@ -160,7 +157,7 @@ impl ParameterService {
160157
let mut prefix = prefix.clone();
161158
prefix.extend(".".chars());
162159
if name.len() > prefix.len()
163-
&& name[0..prefix.len()] == prefix[0..]
160+
&& name.starts_with(&prefix)
164161
&& check_parameter_name_depth(&name[prefix.len()..])
165162
{
166163
return true;
@@ -176,12 +173,10 @@ impl ParameterService {
176173
let name = name.to_string();
177174
if let Some(pos) = name.rfind('.') {
178175
return Some(name[0..pos].into());
179-
//return Some(name[0..pos].iter().map(|c| *c as char).collect());
180176
}
181177
None
182178
})
183179
.collect();
184-
// TODO(luca) populate prefixes in result
185180
ListParameters_Response {
186181
result: ListParametersResult {
187182
names,
@@ -199,20 +194,25 @@ impl ParameterService {
199194
.parameters
200195
.into_iter()
201196
.map(|param| {
202-
let name = param.name.to_cstr().to_str().unwrap();
203-
let value = map.validate_parameter_setting(name, param.value)?;
204-
map.store_parameter(name.into(), value);
205-
Ok(())
206-
})
207-
.map(|res| match res {
208-
Ok(()) => SetParametersResult {
209-
successful: true,
210-
reason: Default::default(),
211-
},
212-
Err(e) => SetParametersResult {
213-
successful: false,
214-
reason: e,
215-
},
197+
let Ok(name) = param.name.to_cstr().to_str() else {
198+
return SetParametersResult {
199+
successful: false,
200+
reason: "Failed parsing into UTF-8".into(),
201+
};
202+
};
203+
match map.validate_parameter_setting(name, param.value) {
204+
Ok(value) => {
205+
map.store_parameter(name.into(), value);
206+
SetParametersResult {
207+
successful: true,
208+
reason: Default::default(),
209+
}
210+
}
211+
Err(e) => SetParametersResult {
212+
successful: false,
213+
reason: e.into(),
214+
},
215+
}
216216
})
217217
.collect();
218218
SetParameters_Response { results }
@@ -226,7 +226,9 @@ impl ParameterService {
226226
.parameters
227227
.into_iter()
228228
.map(|param| {
229-
let name = param.name.to_cstr().to_str().unwrap();
229+
let Ok(name) = param.name.to_cstr().to_str() else {
230+
return Err("Failed parsing into UTF-8".into());
231+
};
230232
let value = map.validate_parameter_setting(name, param.value)?;
231233
Ok((name.into(), value))
232234
})

rclrs/src/parameter/value.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ impl From<ParameterValue> for RmwParameterValue {
369369
},
370370
ParameterValue::StringArray(v) => RmwParameterValue {
371371
type_: ParameterType::PARAMETER_STRING_ARRAY,
372-
// TODO(luca) check if there is a more efficient way to perform this conversion
373372
string_array_value: v
374373
.iter()
375374
.map(|v| v.clone().into())

rosidl_runtime_rs/src/string.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use std::borrow::Borrow;
12
use std::cmp::Ordering;
23
use std::ffi::CStr;
34
use std::fmt::{self, Debug, Display};
45
use std::hash::{Hash, Hasher};
56
use std::ops::{Deref, DerefMut};
6-
use std::sync::Arc;
77

88
#[cfg(feature = "serde")]
99
mod serde;
@@ -302,31 +302,17 @@ string_impl!(
302302
rosidl_runtime_c__U16String__Sequence__copy
303303
);
304304

305-
impl From<&str> for String {
306-
fn from(s: &str) -> Self {
307-
let mut msg = Self {
308-
data: std::ptr::null_mut(),
309-
size: 0,
310-
capacity: 0,
311-
};
312-
// SAFETY: It's okay to pass a non-zero-terminated string here since assignn uses the
313-
// specified length and will append the 0 byte to the dest string itself.
314-
if !unsafe {
315-
rosidl_runtime_c__String__assignn(&mut msg as *mut _, s.as_ptr() as *const _, s.len())
316-
} {
317-
panic!("rosidl_runtime_c__String__assignn failed");
318-
}
319-
msg
320-
}
321-
}
322-
323-
impl From<Arc<str>> for String {
324-
fn from(s: Arc<str>) -> Self {
305+
impl<T> From<T> for String
306+
where
307+
T: Borrow<str>,
308+
{
309+
fn from(s: T) -> Self {
325310
let mut msg = Self {
326311
data: std::ptr::null_mut(),
327312
size: 0,
328313
capacity: 0,
329314
};
315+
let s = s.borrow();
330316
// SAFETY: It's okay to pass a non-zero-terminated string here since assignn uses the
331317
// specified length and will append the 0 byte to the dest string itself.
332318
if !unsafe {

0 commit comments

Comments
 (0)