Skip to content

Commit d93cee4

Browse files
committed
refactor: more use of cstr_to_string() + other simplifications
Don't expect a &[&str] parameter in the struct_value() function. In many cases, it's not practical to require from the caller to build a vector of references. It only works in very limited cases, in other cases, the caller has to jump through hoops. On the other hand, requiring a &[String] is also limiting in many cases, and breaks other callers. And you can't really do both, because Rust doesn't have function overloading. That's why we now take a generic &[T] parameter, where <T: AsRef<str> + ToString>.
1 parent b783f49 commit d93cee4

File tree

1 file changed

+7
-13
lines changed
  • gems/codetracer-ruby-recorder/ext/native_tracer/src

1 file changed

+7
-13
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ fn value_type_id(val: &ValueRecord) -> runtime_tracing::TypeId {
127127
}
128128
}
129129

130-
unsafe fn struct_value(
130+
unsafe fn struct_value<T: AsRef<str> + ToString>(
131131
recorder: &mut Recorder,
132132
class_name: &str,
133-
field_names: &[&str],
133+
field_names: &[T],
134134
field_values: &[VALUE],
135135
depth: usize,
136136
) -> ValueRecord {
@@ -471,14 +471,11 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
471471
let len = RARRAY_LEN(values) as usize;
472472
let mem_ptr = RARRAY_CONST_PTR(members);
473473
let val_ptr = RARRAY_CONST_PTR(values);
474-
let mut names: Vec<&str> = Vec::with_capacity(len);
475-
let mut vals: Vec<VALUE> = Vec::with_capacity(len);
474+
let mut names = Vec::with_capacity(len);
475+
let mut vals = Vec::with_capacity(len);
476476
for i in 0..len {
477477
let sym = *mem_ptr.add(i);
478-
let id = rb_sym2id(sym);
479-
let cstr = rb_id2name(id);
480-
let name = CStr::from_ptr(cstr).to_str().unwrap_or("?");
481-
names.push(name);
478+
names.push(cstr_to_string(rb_id2name(rb_sym2id(sym))).unwrap_or("?".to_string()));
482479
vals.push(*val_ptr.add(i));
483480
}
484481
return struct_value(recorder, &class_name, &names, &vals, depth);
@@ -504,14 +501,11 @@ unsafe fn to_value(recorder: &mut Recorder, val: VALUE, depth: usize) -> ValueRe
504501
}
505502
let len = RARRAY_LEN(ivars) as usize;
506503
let ptr = RARRAY_CONST_PTR(ivars);
507-
let mut names: Vec<&str> = Vec::with_capacity(len);
504+
let mut names = Vec::with_capacity(len);
508505
let mut vals: Vec<VALUE> = Vec::with_capacity(len);
509506
for i in 0..len {
510507
let sym = *ptr.add(i);
511-
let id = rb_sym2id(sym);
512-
let cstr = rb_id2name(id);
513-
let name = CStr::from_ptr(cstr).to_str().unwrap_or("?");
514-
names.push(name);
508+
names.push(cstr_to_string(rb_id2name(rb_sym2id(sym))).unwrap_or("?".to_string()));
515509
let value = rb_funcall(val, recorder.id.instance_variable_get, 1, sym);
516510
vals.push(value);
517511
}

0 commit comments

Comments
 (0)