diff --git a/src/lib.rs b/src/lib.rs index 2f1ef27..eff935d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,7 +133,7 @@ fn convert_msg_code_to_type(code: u8) -> Result fn check_coord( coord: Option, - fields: &Vec, + fields: &[PointFieldMsg], xyz_field_type: &FieldDatatype, ) -> Result { match coord { @@ -144,9 +144,7 @@ fn check_coord( } Ok(field.clone()) } - None => { - return Err(ConversionError::NotEnoughFields); - } + None => Err(ConversionError::NotEnoughFields), } } @@ -237,7 +235,7 @@ where .get(idx) .ok_or(ConversionError::MetaIndexLengthMismatch)? .clone(), - value.datatype.clone(), + value.datatype, )); point_step_size += datatype_size(&value.datatype); } @@ -305,7 +303,7 @@ impl PointMeta { } fn new_from_buffer( - data: &Vec, + data: &[u8], offset: usize, datatype: &FieldDatatype, ) -> Result { @@ -319,7 +317,7 @@ impl PointMeta { } Ok(Self { bytes: bytes_array, - datatype: datatype.clone(), + datatype: *datatype, }) } @@ -371,7 +369,6 @@ where /// let convert: ConvertXYZ = ConvertXYZ::try_from(msg).unwrap(); // parse message /// ``` fn try_from(cloud: PointCloud2Msg) -> Result { - let cloud: PointCloud2Msg = cloud.into(); if cloud.fields.len() < DIM { return Err(ConversionError::NotEnoughFields); } @@ -411,7 +408,7 @@ where let meta_offsets: Vec = meta_with_offsets.iter().map(|x| x.2).collect(); let meta: Vec<(String, FieldDatatype)> = meta_with_offsets .iter() - .map(|x| (x.0.clone(), x.1.clone())) + .map(|x| (x.0.clone(), x.1)) .collect(); let x_field = check_coord(has_x, &cloud.fields, &xyz_field_type)?; @@ -530,13 +527,13 @@ where if DIM > 1 { fields.push(PointFieldMsg { name: "x".to_string(), - offset: 0 * SIZE as u32, + offset: 0, datatype, count: 1, }); fields.push(PointFieldMsg { name: "y".to_string(), - offset: 1 * SIZE as u32, + offset: SIZE as u32, datatype, count: 1, }); @@ -584,7 +581,7 @@ where for field in fields.iter() { let field_type = convert_msg_code_to_type(field.datatype)?; 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; @@ -624,6 +621,10 @@ where pub fn len(&self) -> usize { self.cloud_length } + + pub fn is_empty(&self) -> bool { + self.cloud_length == 0 + } } impl @@ -668,7 +669,7 @@ where self.iteration += 1; let conv = C::try_from((xyz, meta)); // try convert the point match conv { - Err(_) => return Err(ConversionError::PointConversionError), + Err(_) => Err(ConversionError::PointConversionError), Ok(tuple) => Ok(Some(tuple)), } } @@ -817,14 +818,14 @@ fn load_bytes(start_idx: usize, data: &[u8]) -> Option<[u8; S]> return None; } 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); match raw_byte { None => { return None; } Some(some_byte) => { - buff[byte] = some_byte.clone(); + *buff_val = *some_byte; } } } diff --git a/src/ros_types.rs b/src/ros_types.rs index 58e9d6f..9d729b6 100644 --- a/src/ros_types.rs +++ b/src/ros_types.rs @@ -1,38 +1,21 @@ #[cfg(not(feature = "rosrust_msg"))] -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct TimeMsg { pub sec: 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")] pub use rosrust::Time as TimeMsg; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct HeaderMsg { pub seq: u32, pub stamp: TimeMsg, pub frame_id: String, } -impl Default for HeaderMsg { - fn default() -> Self { - Self { - seq: 0, - stamp: TimeMsg::default(), - frame_id: String::new(), - } - } -} - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct PointFieldMsg { pub name: String, pub offset: u32, @@ -40,18 +23,7 @@ pub struct PointFieldMsg { pub count: u32, } -impl Default for PointFieldMsg { - fn default() -> Self { - Self { - name: String::new(), - offset: 0, - datatype: 0, - count: 0, - } - } -} - -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct PointCloud2Msg { pub header: HeaderMsg, pub height: u32, @@ -64,22 +36,6 @@ pub struct PointCloud2Msg { 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")] impl From for PointCloud2Msg { fn from(msg: r2r::sensor_msgs::msg::PointCloud2) -> Self { diff --git a/tests/e2e_test.rs b/tests/e2e_test.rs index f04c926..c8c2a90 100644 --- a/tests/e2e_test.rs +++ b/tests/e2e_test.rs @@ -40,9 +40,9 @@ fn custom_xyz_f32() { z: f32, } type MyConverter = Convert; - impl Into<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { - fn into(self) -> ([f32; DIM], [PointMeta; METADIM]) { - ([self.x, self.y, self.z], []) + impl From for ([f32; DIM], [PointMeta; METADIM]) { + fn from(point: CustomPoint) -> Self { + ([point.x, point.y, point.z], []) } } impl TryFrom<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { @@ -93,9 +93,9 @@ fn custom_xyzi_f32() { i: u8, } type MyConverter = Convert; - impl Into<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { - fn into(self) -> ([f32; DIM], [PointMeta; METADIM]) { - ([self.x, self.y, self.z], [PointMeta::new(self.i)]) + impl From for ([f32; DIM], [PointMeta; METADIM]) { + fn from(point: CustomPoint) -> Self { + ([point.x, point.y, point.z], [PointMeta::new(point.i)]) } } impl TryFrom<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { @@ -158,15 +158,15 @@ fn custom_rgba_f32() { a: u8, } type MyConverter = Convert; - impl Into<([f32; DIM], [PointMeta; METADIM])> for CustomPoint { - fn into(self) -> ([f32; DIM], [PointMeta; METADIM]) { + impl From for ([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(self.g), - PointMeta::new(self.b), - PointMeta::new(self.a), + PointMeta::new(point.r), + PointMeta::new(point.g), + PointMeta::new(point.b), + PointMeta::new(point.a), ], ) }