Skip to content

Commit 4d1c454

Browse files
committed
Update SymbolNode definition
1 parent 1b0f7fb commit 4d1c454

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

rust/ruby-rbs/src/node/mod.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl std::fmt::Display for RBSString<'_> {
275275

276276
impl SymbolNode<'_> {
277277
#[must_use]
278-
pub fn name(&self) -> &[u8] {
278+
pub fn as_bytes(&self) -> &[u8] {
279279
unsafe {
280280
let constant_ptr = rbs_constant_pool_id_to_constant(
281281
&(*self.parser.as_ptr()).constant_pool,
@@ -289,6 +289,17 @@ impl SymbolNode<'_> {
289289
std::slice::from_raw_parts(constant.start, constant.length)
290290
}
291291
}
292+
293+
#[must_use]
294+
pub fn as_str(&self) -> &str {
295+
unsafe { std::str::from_utf8_unchecked(self.as_bytes()) }
296+
}
297+
}
298+
299+
impl std::fmt::Display for SymbolNode<'_> {
300+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
301+
f.write_str(self.as_str())
302+
}
292303
}
293304

294305
#[cfg(test)]
@@ -359,10 +370,10 @@ mod tests {
359370
panic!("Expected ClassInstanceType");
360371
};
361372

362-
let key_name = String::from_utf8(sym.name().to_vec()).unwrap();
373+
let key_name = sym.to_string();
363374
let type_name_node = class_type.name();
364375
let type_name_sym = type_name_node.name();
365-
let type_name = String::from_utf8(type_name_sym.name().to_vec()).unwrap();
376+
let type_name = type_name_sym.to_string();
366377
field_types.push((key_name, type_name));
367378
}
368379

@@ -393,28 +404,19 @@ mod tests {
393404
}
394405

395406
fn visit_class_node(&mut self, node: &ClassNode) {
396-
self.visited.push(format!(
397-
"class:{}",
398-
String::from_utf8(node.name().name().name().to_vec()).unwrap()
399-
));
407+
self.visited.push(format!("class:{}", node.name().name()));
400408

401409
crate::node::visit_class_node(self, node);
402410
}
403411

404412
fn visit_class_instance_type_node(&mut self, node: &ClassInstanceTypeNode) {
405-
self.visited.push(format!(
406-
"type:{}",
407-
String::from_utf8(node.name().name().name().to_vec()).unwrap()
408-
));
413+
self.visited.push(format!("type:{}", node.name().name()));
409414

410415
crate::node::visit_class_instance_type_node(self, node);
411416
}
412417

413418
fn visit_class_super_node(&mut self, node: &ClassSuperNode) {
414-
self.visited.push(format!(
415-
"super:{}",
416-
String::from_utf8(node.name().name().name().to_vec()).unwrap()
417-
));
419+
self.visited.push(format!("super:{}", node.name().name()));
418420

419421
crate::node::visit_class_super_node(self, node);
420422
}
@@ -428,10 +430,7 @@ mod tests {
428430
}
429431

430432
fn visit_method_definition_node(&mut self, node: &MethodDefinitionNode) {
431-
self.visited.push(format!(
432-
"method:{}",
433-
String::from_utf8(node.name().name().to_vec()).unwrap()
434-
));
433+
self.visited.push(format!("method:{}", node.name()));
435434

436435
crate::node::visit_method_definition_node(self, node);
437436
}
@@ -443,10 +442,7 @@ mod tests {
443442
}
444443

445444
fn visit_symbol_node(&mut self, node: &SymbolNode) {
446-
self.visited.push(format!(
447-
"symbol:{}",
448-
String::from_utf8(node.name().to_vec()).unwrap()
449-
));
445+
self.visited.push(format!("symbol:{node}"));
450446

451447
crate::node::visit_symbol_node(self, node);
452448
}

0 commit comments

Comments
 (0)