Skip to content

Commit

Permalink
refactor: Prefer explicit Clone over From<&Borrowed> for Owned impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed May 15, 2024
1 parent 874039b commit 69fdb85
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl RuleNode {
catch_unwind
)]
pub fn text_len(&self) -> TextIndex {
(&self.0.text_len).into()
self.0.text_len.into()
}

#[napi(ts_return_type = "Array<cst.Node>", catch_unwind)]
Expand All @@ -81,7 +81,7 @@ impl RuleNode {
#[napi(ts_arg_type = "text_index.TextIndex")] text_offset: TextIndex,
) -> Cursor {
RustNode::Rule(Rc::clone(&self.0))
.cursor_with_offset((&text_offset).into())
.cursor_with_offset(text_offset.into())
.into()
}

Expand Down Expand Up @@ -151,7 +151,7 @@ impl TokenNode {
)]
pub fn text_len(&self) -> TextIndex {
let text_len: RustTextIndex = (&self.0.text).into();
(&text_len).into()
text_len.into()
}

#[napi(getter, catch_unwind)]
Expand All @@ -173,7 +173,7 @@ impl TokenNode {
#[napi(ts_arg_type = "text_index.TextIndex")] text_offset: TextIndex,
) -> Cursor {
RustNode::Token(Rc::clone(&self.0))
.cursor_with_offset((&text_offset).into())
.cursor_with_offset(text_offset.into())
.into()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ impl Cursor {

#[napi(getter, ts_return_type = "text_index.TextIndex", catch_unwind)]
pub fn text_offset(&self) -> TextIndex {
(&self.0.text_offset()).into()
self.0.text_offset().into()
}

#[napi(getter, ts_return_type = "text_index.TextRange", catch_unwind)]
pub fn text_range(&self) -> TextRange {
(&self.0.text_range()).into()
self.0.text_range().into()
}

#[allow(clippy::cast_possible_truncation)] // Cursor depth can't reasonably be larger than u32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl From<RustParseError> for ParseError {
impl ParseError {
#[napi(getter, ts_return_type = "text_index.TextRange", catch_unwind)]
pub fn text_range(&self) -> TextRange {
self.0.text_range().into()
self.0.text_range().clone().into()
}

#[napi(catch_unwind)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct TextIndex {
pub char: u32,
}

impl From<&RustTextIndex> for TextIndex {
fn from(value: &RustTextIndex) -> Self {
impl From<RustTextIndex> for TextIndex {
fn from(value: RustTextIndex) -> Self {
// We only support 32-byte indices on TS side.
#[allow(clippy::cast_possible_truncation)]
Self {
Expand All @@ -22,8 +22,8 @@ impl From<&RustTextIndex> for TextIndex {
}
}

impl From<&TextIndex> for RustTextIndex {
fn from(value: &TextIndex) -> Self {
impl From<TextIndex> for RustTextIndex {
fn from(value: TextIndex) -> Self {
Self {
utf8: value.utf8 as usize,
utf16: value.utf16 as usize,
Expand All @@ -39,11 +39,11 @@ pub struct TextRange {
pub end: TextIndex,
}

impl From<&RustTextRange> for TextRange {
fn from(value: &RustTextRange) -> Self {
impl From<RustTextRange> for TextRange {
fn from(value: RustTextRange) -> Self {
Self {
start: (&value.start).into(),
end: (&value.end).into(),
start: value.start.into(),
end: value.end.into(),
}
}
}
20 changes: 3 additions & 17 deletions crates/codegen/runtime/cargo/src/runtime/text_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,12 @@ impl Display for TextIndex {
}
}

impl From<&str> for TextIndex {
fn from(s: &str) -> Self {
impl<T: AsRef<str>> From<T> for TextIndex {
fn from(s: T) -> Self {
let mut utf8 = 0;
let mut utf16 = 0;
let mut char = 0;
for c in s.chars() {
utf8 += c.len_utf8();
utf16 += c.len_utf16();
char += 1;
}
Self { utf8, utf16, char }
}
}

impl From<&String> for TextIndex {
fn from(s: &String) -> Self {
let mut utf8 = 0;
let mut utf16 = 0;
let mut char = 0;
for c in s.chars() {
for c in s.as_ref().chars() {
utf8 += c.len_utf8();
utf16 += c.len_utf16();
char += 1;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 69fdb85

Please sign in to comment.