Skip to content

Commit 4641e4b

Browse files
committed
refactor: value_to_string_safe changed to return a String, instead of Option<String>
There is only one place, where value_to_string_safe() is called, and at that place, .unwrap_or_default() is used, rendering the Option<> effectively useless. This makes the code simpler, without functional changes.
1 parent a0cf860 commit 4641e4b

File tree

1 file changed

+12
-10
lines changed
  • gems/codetracer-ruby-recorder/ext/native_tracer/src

1 file changed

+12
-10
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,19 @@ unsafe extern "C" fn call_to_s(arg: VALUE) -> VALUE {
314314
rb_funcall(data.0, data.1, 0)
315315
}
316316

317-
unsafe fn value_to_string_safe(recorder: &Recorder, val: VALUE) -> Option<String> {
317+
unsafe fn value_to_string_safe(recorder: &Recorder, val: VALUE) -> String {
318318
if RB_TYPE_P(val, rb_sys::ruby_value_type::RUBY_T_STRING) {
319-
return Some(rstring_lossy(val));
320-
}
321-
let mut state: c_int = 0;
322-
let data = (val, recorder.id.to_s);
323-
let str_val = rb_protect(Some(call_to_s), &data as *const _ as VALUE, &mut state);
324-
if state != 0 {
325-
return None;
319+
rstring_lossy(val)
320+
} else {
321+
let mut state: c_int = 0;
322+
let data = (val, recorder.id.to_s);
323+
let str_val = rb_protect(Some(call_to_s), &data as *const _ as VALUE, &mut state);
324+
if state != 0 {
325+
String::default()
326+
} else {
327+
rstring_lossy(str_val)
328+
}
326329
}
327-
Some(rstring_lossy(str_val))
328330
}
329331

330332
unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRecord {
@@ -812,7 +814,7 @@ unsafe extern "C" fn event_hook_raw(data: VALUE, arg: *mut rb_trace_arg_t) {
812814

813815
let class_name =
814816
cstr_to_string(rb_obj_classname(self_val)).unwrap_or_else(|| "Object".to_string());
815-
let text = value_to_string_safe(recorder, self_val).unwrap_or_default();
817+
let text = value_to_string_safe(recorder, self_val);
816818
let self_type =
817819
TraceWriter::ensure_type_id(&mut *recorder.tracer, TypeKind::Raw, &class_name);
818820
let self_rec = ValueRecord::Raw {

0 commit comments

Comments
 (0)