fix clippy warnings

This commit is contained in:
stelzo 2023-07-25 21:16:22 +02:00
parent 8096edd6c7
commit 15665b4fc8
3 changed files with 33 additions and 76 deletions

View File

@ -133,7 +133,7 @@ fn convert_msg_code_to_type(code: u8) -> Result<FieldDatatype, ConversionError>
fn check_coord( fn check_coord(
coord: Option<usize>, coord: Option<usize>,
fields: &Vec<PointFieldMsg>, fields: &[PointFieldMsg],
xyz_field_type: &FieldDatatype, xyz_field_type: &FieldDatatype,
) -> Result<PointFieldMsg, ConversionError> { ) -> Result<PointFieldMsg, ConversionError> {
match coord { match coord {
@ -144,9 +144,7 @@ fn check_coord(
} }
Ok(field.clone()) Ok(field.clone())
} }
None => { None => Err(ConversionError::NotEnoughFields),
return Err(ConversionError::NotEnoughFields);
}
} }
} }
@ -237,7 +235,7 @@ where
.get(idx) .get(idx)
.ok_or(ConversionError::MetaIndexLengthMismatch)? .ok_or(ConversionError::MetaIndexLengthMismatch)?
.clone(), .clone(),
value.datatype.clone(), value.datatype,
)); ));
point_step_size += datatype_size(&value.datatype); point_step_size += datatype_size(&value.datatype);
} }
@ -305,7 +303,7 @@ impl PointMeta {
} }
fn new_from_buffer( fn new_from_buffer(
data: &Vec<u8>, data: &[u8],
offset: usize, offset: usize,
datatype: &FieldDatatype, datatype: &FieldDatatype,
) -> Result<Self, ConversionError> { ) -> Result<Self, ConversionError> {
@ -319,7 +317,7 @@ impl PointMeta {
} }
Ok(Self { Ok(Self {
bytes: bytes_array, bytes: bytes_array,
datatype: datatype.clone(), datatype: *datatype,
}) })
} }
@ -371,7 +369,6 @@ where
/// let convert: ConvertXYZ = ConvertXYZ::try_from(msg).unwrap(); // parse message /// let convert: ConvertXYZ = ConvertXYZ::try_from(msg).unwrap(); // parse message
/// ``` /// ```
fn try_from(cloud: PointCloud2Msg) -> Result<Self, Self::Error> { fn try_from(cloud: PointCloud2Msg) -> Result<Self, Self::Error> {
let cloud: PointCloud2Msg = cloud.into();
if cloud.fields.len() < DIM { if cloud.fields.len() < DIM {
return Err(ConversionError::NotEnoughFields); return Err(ConversionError::NotEnoughFields);
} }
@ -411,7 +408,7 @@ where
let meta_offsets: Vec<usize> = meta_with_offsets.iter().map(|x| x.2).collect(); let meta_offsets: Vec<usize> = meta_with_offsets.iter().map(|x| x.2).collect();
let meta: Vec<(String, FieldDatatype)> = meta_with_offsets let meta: Vec<(String, FieldDatatype)> = meta_with_offsets
.iter() .iter()
.map(|x| (x.0.clone(), x.1.clone())) .map(|x| (x.0.clone(), x.1))
.collect(); .collect();
let x_field = check_coord(has_x, &cloud.fields, &xyz_field_type)?; let x_field = check_coord(has_x, &cloud.fields, &xyz_field_type)?;
@ -530,13 +527,13 @@ where
if DIM > 1 { if DIM > 1 {
fields.push(PointFieldMsg { fields.push(PointFieldMsg {
name: "x".to_string(), name: "x".to_string(),
offset: 0 * SIZE as u32, offset: 0,
datatype, datatype,
count: 1, count: 1,
}); });
fields.push(PointFieldMsg { fields.push(PointFieldMsg {
name: "y".to_string(), name: "y".to_string(),
offset: 1 * SIZE as u32, offset: SIZE as u32,
datatype, datatype,
count: 1, count: 1,
}); });
@ -584,7 +581,7 @@ where
for field in fields.iter() { for field in fields.iter() {
let field_type = convert_msg_code_to_type(field.datatype)?; let field_type = convert_msg_code_to_type(field.datatype)?;
let field_size = datatype_size(&field_type); let field_size = datatype_size(&field_type);
step_size += field.count as u32 * field_size as u32; step_size += field.count * field_size as u32;
} }
cloud.fields = fields; cloud.fields = fields;
@ -624,6 +621,10 @@ where
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.cloud_length self.cloud_length
} }
pub fn is_empty(&self) -> bool {
self.cloud_length == 0
}
} }
impl<T, const SIZE: usize, const DIM: usize, const METADIM: usize, C> impl<T, const SIZE: usize, const DIM: usize, const METADIM: usize, C>
@ -668,7 +669,7 @@ where
self.iteration += 1; self.iteration += 1;
let conv = C::try_from((xyz, meta)); // try convert the point let conv = C::try_from((xyz, meta)); // try convert the point
match conv { match conv {
Err(_) => return Err(ConversionError::PointConversionError), Err(_) => Err(ConversionError::PointConversionError),
Ok(tuple) => Ok(Some(tuple)), Ok(tuple) => Ok(Some(tuple)),
} }
} }
@ -817,14 +818,14 @@ fn load_bytes<const S: usize>(start_idx: usize, data: &[u8]) -> Option<[u8; S]>
return None; return None;
} }
let mut buff: [u8; S] = [u8::zero(); S]; let mut buff: [u8; S] = [u8::zero(); S];
for byte in 0..S { for (byte, buff_val) in buff.iter_mut().enumerate().take(S) {
let raw_byte = data.get(start_idx + byte); let raw_byte = data.get(start_idx + byte);
match raw_byte { match raw_byte {
None => { None => {
return None; return None;
} }
Some(some_byte) => { Some(some_byte) => {
buff[byte] = some_byte.clone(); *buff_val = *some_byte;
} }
} }
} }

View File

@ -1,38 +1,21 @@
#[cfg(not(feature = "rosrust_msg"))] #[cfg(not(feature = "rosrust_msg"))]
#[derive(Clone, Debug)] #[derive(Clone, Debug, Default)]
pub struct TimeMsg { pub struct TimeMsg {
pub sec: u32, pub sec: u32,
pub nsec: u32, pub nsec: u32,
} }
#[cfg(not(feature = "rosrust_msg"))]
impl Default for TimeMsg {
fn default() -> Self {
Self { sec: 0, nsec: 0 }
}
}
#[cfg(feature = "rosrust_msg")] #[cfg(feature = "rosrust_msg")]
pub use rosrust::Time as TimeMsg; pub use rosrust::Time as TimeMsg;
#[derive(Clone, Debug)] #[derive(Clone, Debug, Default)]
pub struct HeaderMsg { pub struct HeaderMsg {
pub seq: u32, pub seq: u32,
pub stamp: TimeMsg, pub stamp: TimeMsg,
pub frame_id: String, pub frame_id: String,
} }
impl Default for HeaderMsg { #[derive(Clone, Debug, Default)]
fn default() -> Self {
Self {
seq: 0,
stamp: TimeMsg::default(),
frame_id: String::new(),
}
}
}
#[derive(Clone, Debug)]
pub struct PointFieldMsg { pub struct PointFieldMsg {
pub name: String, pub name: String,
pub offset: u32, pub offset: u32,
@ -40,18 +23,7 @@ pub struct PointFieldMsg {
pub count: u32, pub count: u32,
} }
impl Default for PointFieldMsg { #[derive(Clone, Debug, Default)]
fn default() -> Self {
Self {
name: String::new(),
offset: 0,
datatype: 0,
count: 0,
}
}
}
#[derive(Clone, Debug)]
pub struct PointCloud2Msg { pub struct PointCloud2Msg {
pub header: HeaderMsg, pub header: HeaderMsg,
pub height: u32, pub height: u32,
@ -64,22 +36,6 @@ pub struct PointCloud2Msg {
pub is_dense: bool, pub is_dense: bool,
} }
impl Default for PointCloud2Msg {
fn default() -> Self {
Self {
header: HeaderMsg::default(),
height: 0,
width: 0,
fields: Vec::new(),
is_bigendian: false,
point_step: 0,
row_step: 0,
data: Vec::new(),
is_dense: false,
}
}
}
#[cfg(feature = "r2r_msg")] #[cfg(feature = "r2r_msg")]
impl From<r2r::sensor_msgs::msg::PointCloud2> for PointCloud2Msg { impl From<r2r::sensor_msgs::msg::PointCloud2> for PointCloud2Msg {
fn from(msg: r2r::sensor_msgs::msg::PointCloud2) -> Self { fn from(msg: r2r::sensor_msgs::msg::PointCloud2) -> Self {

View File

@ -40,9 +40,9 @@ fn custom_xyz_f32() {
z: f32, z: f32,
} }
type MyConverter = Convert<f32, { size_of!(f32) }, DIM, METADIM, CustomPoint>; type MyConverter = Convert<f32, { size_of!(f32) }, DIM, METADIM, CustomPoint>;
impl Into<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { impl From<CustomPoint> for ([f32; DIM], [PointMeta; METADIM]) {
fn into(self) -> ([f32; DIM], [PointMeta; METADIM]) { fn from(point: CustomPoint) -> Self {
([self.x, self.y, self.z], []) ([point.x, point.y, point.z], [])
} }
} }
impl TryFrom<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { impl TryFrom<([f32; DIM], [PointMeta; METADIM])> for CustomPoint {
@ -93,9 +93,9 @@ fn custom_xyzi_f32() {
i: u8, i: u8,
} }
type MyConverter = Convert<f32, { size_of!(f32) }, DIM, METADIM, CustomPoint>; type MyConverter = Convert<f32, { size_of!(f32) }, DIM, METADIM, CustomPoint>;
impl Into<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { impl From<CustomPoint> for ([f32; DIM], [PointMeta; METADIM]) {
fn into(self) -> ([f32; DIM], [PointMeta; METADIM]) { fn from(point: CustomPoint) -> Self {
([self.x, self.y, self.z], [PointMeta::new(self.i)]) ([point.x, point.y, point.z], [PointMeta::new(point.i)])
} }
} }
impl TryFrom<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { impl TryFrom<([f32; DIM], [PointMeta; METADIM])> for CustomPoint {
@ -158,15 +158,15 @@ fn custom_rgba_f32() {
a: u8, a: u8,
} }
type MyConverter = Convert<f32, { size_of!(f32) }, DIM, METADIM, CustomPoint>; type MyConverter = Convert<f32, { size_of!(f32) }, DIM, METADIM, CustomPoint>;
impl Into<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { impl From<CustomPoint> for ([f32; DIM], [PointMeta; METADIM]) {
fn into(self) -> ([f32; DIM], [PointMeta; METADIM]) { fn from(point: CustomPoint) -> Self {
( (
[self.x, self.y, self.z], [point.x, point.y, point.z],
[ [
PointMeta::new(self.r), PointMeta::new(point.r),
PointMeta::new(self.g), PointMeta::new(point.g),
PointMeta::new(self.b), PointMeta::new(point.b),
PointMeta::new(self.a), PointMeta::new(point.a),
], ],
) )
} }