Skip to content

Commit 1b0f7fb

Browse files
committed
Update RBSString
1 parent 2074023 commit 1b0f7fb

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ impl<'a> RBSString<'a> {
260260
std::slice::from_raw_parts(s.start as *const u8, s.end.offset_from(s.start) as usize)
261261
}
262262
}
263+
264+
#[must_use]
265+
pub fn as_str(&self) -> &str {
266+
unsafe { std::str::from_utf8_unchecked(self.as_bytes()) }
267+
}
268+
}
269+
270+
impl std::fmt::Display for RBSString<'_> {
271+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
272+
f.write_str(self.as_str())
273+
}
263274
}
264275

265276
impl SymbolNode<'_> {
@@ -314,10 +325,7 @@ mod tests {
314325
&& let Node::LiteralType(literal) = node.type_()
315326
&& let Node::Integer(integer) = literal.literal()
316327
{
317-
assert_eq!(
318-
"1".to_string(),
319-
String::from_utf8(integer.string_representation().as_bytes().to_vec()).unwrap()
320-
);
328+
assert_eq!(integer.string_representation().as_str(), "1");
321329
} else {
322330
panic!("No literal type node found");
323331
}

0 commit comments

Comments
 (0)