Skip to content

Commit a0cf860

Browse files
committed
refactor: value_to_string now returns a String, instead of Option<String>
After the refactor from the previous commit, it became obvious, that the function value_to_string() can never return a None value. Therefore, it's useless that it returns an Option<String>, so it was changed to return a String. This lead to a lot of code simplification.
1 parent 4faf211 commit a0cf860

File tree

1 file changed

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

1 file changed

+9
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ unsafe fn rstring_lossy(val: VALUE) -> String {
301301
String::from_utf8_lossy(slice).to_string()
302302
}
303303

304-
unsafe fn value_to_string(recorder: &Recorder, val: VALUE) -> Option<String> {
304+
unsafe fn value_to_string(recorder: &Recorder, val: VALUE) -> String {
305305
if RB_TYPE_P(val, rb_sys::ruby_value_type::RUBY_T_STRING) {
306-
Some(rstring_lossy(val))
306+
rstring_lossy(val)
307307
} else {
308-
Some(rstring_lossy(rb_funcall(val, recorder.id.to_s, 0)))
308+
rstring_lossy(rb_funcall(val, recorder.id.to_s, 0))
309309
}
310310
}
311311

@@ -477,7 +477,7 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
477477
if !RB_TYPE_P(members, rb_sys::ruby_value_type::RUBY_T_ARRAY)
478478
|| !RB_TYPE_P(values, rb_sys::ruby_value_type::RUBY_T_ARRAY)
479479
{
480-
let text = value_to_string(recorder, val).unwrap_or_default();
480+
let text = value_to_string(recorder, val);
481481
let type_id =
482482
TraceWriter::ensure_type_id(&mut *recorder.tracer, TypeKind::Raw, &class_name);
483483
return ValueRecord::Raw { r: text, type_id };
@@ -511,7 +511,7 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
511511
// generic object
512512
let ivars = rb_funcall(val, recorder.id.instance_variables, 0);
513513
if !RB_TYPE_P(ivars, rb_sys::ruby_value_type::RUBY_T_ARRAY) {
514-
let text = value_to_string(recorder, val).unwrap_or_default();
514+
let text = value_to_string(recorder, val);
515515
let type_id =
516516
TraceWriter::ensure_type_id(&mut *recorder.tracer, TypeKind::Raw, &class_name);
517517
return ValueRecord::Raw { r: text, type_id };
@@ -532,7 +532,7 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
532532
if !names.is_empty() {
533533
return struct_value(recorder, &class_name, &names, &vals, depth);
534534
}
535-
let text = value_to_string(recorder, val).unwrap_or_default();
535+
let text = value_to_string(recorder, val);
536536
let type_id = TraceWriter::ensure_type_id(&mut *recorder.tracer, TypeKind::Raw, &class_name);
537537
ValueRecord::Raw { r: text, type_id }
538538
}
@@ -751,7 +751,7 @@ unsafe extern "C" fn record_event_api(
751751
std::str::from_utf8(std::slice::from_raw_parts(ptr as *const u8, len)).unwrap_or("")
752752
};
753753
let line_num = rb_num2long(line) as i64;
754-
let content_str = value_to_string(recorder, content).unwrap_or_default();
754+
let content_str = value_to_string(recorder, content);
755755
record_event(&mut *recorder.tracer, path_slice, line_num, content_str);
756756
Qnil.into()
757757
}
@@ -864,9 +864,8 @@ unsafe extern "C" fn event_hook_raw(data: VALUE, arg: *mut rb_trace_arg_t) {
864864
TraceWriter::register_return(&mut *recorder.tracer, val_rec);
865865
} else if (ev & RUBY_EVENT_RAISE) != 0 {
866866
let exc = rb_tracearg_raised_exception(arg);
867-
if let Some(msg) = value_to_string(recorder, exc) {
868-
TraceWriter::register_special_event(&mut *recorder.tracer, EventLogKind::Error, &msg);
869-
}
867+
let msg = value_to_string(recorder, exc);
868+
TraceWriter::register_special_event(&mut *recorder.tracer, EventLogKind::Error, &msg);
870869
}
871870
}
872871

0 commit comments

Comments
 (0)