Skip to content

Commit 4faf211

Browse files
committed
refactor: extracted some commonly repeated string conversion code to a function
The following code pattern: let ptr = RSTRING_PTR(val); let len = RSTRING_LEN(val) as usize; let slice = std::slice::from_raw_parts(ptr as *const u8, len); let result = String::from_utf8_lossy(slice).to_string(); Was repeated too often. This was extracted into a function rstring_lossy(). Note that there are other variations to this code pattern. For now, they haven't been converted to use the function. No functional changes.
1 parent 69000ed commit 4faf211

File tree

1 file changed

+14
-22
lines changed
  • gems/codetracer-ruby-recorder/ext/native_tracer/src

1 file changed

+14
-22
lines changed

gems/codetracer-ruby-recorder/ext/native_tracer/src/lib.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,19 @@ unsafe fn cstr_to_string(ptr: *const c_char) -> Option<String> {
294294
CStr::from_ptr(ptr).to_str().ok().map(|s| s.to_string())
295295
}
296296

297+
unsafe fn rstring_lossy(val: VALUE) -> String {
298+
let ptr = RSTRING_PTR(val);
299+
let len = RSTRING_LEN(val) as usize;
300+
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
301+
String::from_utf8_lossy(slice).to_string()
302+
}
303+
297304
unsafe fn value_to_string(recorder: &Recorder, val: VALUE) -> Option<String> {
298305
if RB_TYPE_P(val, rb_sys::ruby_value_type::RUBY_T_STRING) {
299-
let ptr = RSTRING_PTR(val);
300-
let len = RSTRING_LEN(val) as usize;
301-
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
302-
return Some(String::from_utf8_lossy(slice).to_string());
303-
}
304-
let str_val = rb_funcall(val, recorder.id.to_s, 0);
305-
let ptr = RSTRING_PTR(str_val);
306-
let len = RSTRING_LEN(str_val) as usize;
307-
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
308-
Some(String::from_utf8_lossy(slice).to_string())
306+
Some(rstring_lossy(val))
307+
} else {
308+
Some(rstring_lossy(rb_funcall(val, recorder.id.to_s, 0)))
309+
}
309310
}
310311

311312
unsafe extern "C" fn call_to_s(arg: VALUE) -> VALUE {
@@ -315,21 +316,15 @@ unsafe extern "C" fn call_to_s(arg: VALUE) -> VALUE {
315316

316317
unsafe fn value_to_string_safe(recorder: &Recorder, val: VALUE) -> Option<String> {
317318
if RB_TYPE_P(val, rb_sys::ruby_value_type::RUBY_T_STRING) {
318-
let ptr = RSTRING_PTR(val);
319-
let len = RSTRING_LEN(val) as usize;
320-
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
321-
return Some(String::from_utf8_lossy(slice).to_string());
319+
return Some(rstring_lossy(val));
322320
}
323321
let mut state: c_int = 0;
324322
let data = (val, recorder.id.to_s);
325323
let str_val = rb_protect(Some(call_to_s), &data as *const _ as VALUE, &mut state);
326324
if state != 0 {
327325
return None;
328326
}
329-
let ptr = RSTRING_PTR(str_val);
330-
let len = RSTRING_LEN(str_val) as usize;
331-
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
332-
Some(String::from_utf8_lossy(slice).to_string())
327+
Some(rstring_lossy(str_val))
333328
}
334329

335330
unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRecord {
@@ -376,11 +371,8 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
376371
};
377372
}
378373
if RB_TYPE_P(val, rb_sys::ruby_value_type::RUBY_T_STRING) {
379-
let ptr = RSTRING_PTR(val);
380-
let len = RSTRING_LEN(val) as usize;
381-
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
382374
return ValueRecord::String {
383-
text: String::from_utf8_lossy(slice).to_string(),
375+
text: rstring_lossy(val),
384376
type_id: recorder.string_type_id,
385377
};
386378
}

0 commit comments

Comments
 (0)