Skip to content

Commit cf4c4ab

Browse files
authored
refactor: consistently use crate::helper::copy_str_to_c_char_array when such situation (#41)
1 parent 77d5f3c commit cf4c4ab

1 file changed

Lines changed: 3 additions & 10 deletions

File tree

src/types/visualization.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use crate::bindgen::{
77
mjNGROUP, mjNVISFLAG, mjNRNDFLAG, mjMAXLIGHT, mjMAXLINE, mjMAXLINEPNT,
88
};
99

10-
use crate::{mjtObj, obj, ObjectId, SegmentationId};
10+
use crate::{helper, mjtObj, obj, ObjectId, SegmentationId};
1111

1212
pub use crate::bindgen::mjvPerturb;
1313
impl Default for mjvPerturb {
@@ -453,11 +453,7 @@ macro_rules! chars {
453453
#[doc = "set "]
454454
#[doc = $description]
455455
pub fn $set_name(&mut self, $name: &str) -> &mut Self {
456-
let bytes = $name.as_bytes();
457-
let (len, limit) = (bytes.len(), self.$name.len() - 1);
458-
assert!(len <= limit, "{}: `{len}` is too long, max {limit} bytes", stringify!($name));
459-
bytes.iter().enumerate().for_each(|(i, &b)| self.$name[i] = b as std::ffi::c_char);
460-
self.$name[bytes.len()] = 0; // null-terminate
456+
helper::copy_str_to_c_chararray($name, &mut self.$name);
461457
self
462458
}
463459
)*
@@ -479,10 +475,7 @@ impl mjvFigure {
479475
/// set line name for legend of index `index` \in `0..mjMAXLINE`
480476
pub fn set_linename(&mut self, index: usize, name: &str) -> &mut Self {
481477
assert!(index < mjMAXLINE, "`set_linename`: too large index `{index}`, must be index < {mjMAXLINE}");
482-
let (len, limit) = (name.as_bytes().len(), self.linename[index].len() - 1);
483-
assert!(len <= limit, "Line name `{name}` is too long, max {limit} bytes");
484-
name.as_bytes().iter().enumerate().for_each(|(i, &b)| self.linename[index][i] = b as std::ffi::c_char);
485-
self.linename[index][len] = 0; // null-terminate
478+
helper::copy_str_to_c_chararray(name, &mut self.linename[index]);
486479
self
487480
}
488481

0 commit comments

Comments
 (0)