From a2f50942cd1dc9fe420d6c0c04e096a25fdf5530 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Mon, 20 May 2024 08:53:45 +0100 Subject: [PATCH] refactor: Prefer explicit Clone over `From<&Borrowed> for Owned` impls (#964) Extracted from #963 in case we want to land this separately. --- .../cargo/src/runtime/napi_interface/cst.rs | 8 ++++---- .../src/runtime/napi_interface/cursor.rs | 4 ++-- .../src/runtime/napi_interface/parse_error.rs | 2 +- .../src/runtime/napi_interface/text_index.rs | 16 +++++++-------- crates/metaslang/cst/src/text_index.rs | 20 +++---------------- .../src/generated/napi_interface/cst.rs | 8 ++++---- .../src/generated/napi_interface/cursor.rs | 4 ++-- .../generated/napi_interface/parse_error.rs | 2 +- .../generated/napi_interface/text_index.rs | 16 +++++++-------- .../src/generated/napi_interface/cst.rs | 8 ++++---- .../src/generated/napi_interface/cursor.rs | 4 ++-- .../generated/napi_interface/parse_error.rs | 2 +- .../generated/napi_interface/text_index.rs | 16 +++++++-------- 13 files changed, 48 insertions(+), 62 deletions(-) diff --git a/crates/codegen/runtime/cargo/src/runtime/napi_interface/cst.rs b/crates/codegen/runtime/cargo/src/runtime/napi_interface/cst.rs index d67077fe98..cf5edf9cb9 100644 --- a/crates/codegen/runtime/cargo/src/runtime/napi_interface/cst.rs +++ b/crates/codegen/runtime/cargo/src/runtime/napi_interface/cst.rs @@ -61,7 +61,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", catch_unwind)] @@ -79,7 +79,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() } @@ -149,7 +149,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)] @@ -171,7 +171,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() } } diff --git a/crates/codegen/runtime/cargo/src/runtime/napi_interface/cursor.rs b/crates/codegen/runtime/cargo/src/runtime/napi_interface/cursor.rs index d04b0759ad..6411848b2e 100644 --- a/crates/codegen/runtime/cargo/src/runtime/napi_interface/cursor.rs +++ b/crates/codegen/runtime/cargo/src/runtime/napi_interface/cursor.rs @@ -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 diff --git a/crates/codegen/runtime/cargo/src/runtime/napi_interface/parse_error.rs b/crates/codegen/runtime/cargo/src/runtime/napi_interface/parse_error.rs index 8a2a301fe2..219a85f70a 100644 --- a/crates/codegen/runtime/cargo/src/runtime/napi_interface/parse_error.rs +++ b/crates/codegen/runtime/cargo/src/runtime/napi_interface/parse_error.rs @@ -20,7 +20,7 @@ impl From 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)] diff --git a/crates/codegen/runtime/cargo/src/runtime/napi_interface/text_index.rs b/crates/codegen/runtime/cargo/src/runtime/napi_interface/text_index.rs index 5920bf093b..bfb24b804e 100644 --- a/crates/codegen/runtime/cargo/src/runtime/napi_interface/text_index.rs +++ b/crates/codegen/runtime/cargo/src/runtime/napi_interface/text_index.rs @@ -10,8 +10,8 @@ pub struct TextIndex { pub char: u32, } -impl From<&RustTextIndex> for TextIndex { - fn from(value: &RustTextIndex) -> Self { +impl From for TextIndex { + fn from(value: RustTextIndex) -> Self { // We only support 32-byte indices on TS side. #[allow(clippy::cast_possible_truncation)] Self { @@ -22,8 +22,8 @@ impl From<&RustTextIndex> for TextIndex { } } -impl From<&TextIndex> for RustTextIndex { - fn from(value: &TextIndex) -> Self { +impl From for RustTextIndex { + fn from(value: TextIndex) -> Self { Self { utf8: value.utf8 as usize, utf16: value.utf16 as usize, @@ -39,11 +39,11 @@ pub struct TextRange { pub end: TextIndex, } -impl From<&RustTextRange> for TextRange { - fn from(value: &RustTextRange) -> Self { +impl From for TextRange { + fn from(value: RustTextRange) -> Self { Self { - start: (&value.start).into(), - end: (&value.end).into(), + start: value.start.into(), + end: value.end.into(), } } } diff --git a/crates/metaslang/cst/src/text_index.rs b/crates/metaslang/cst/src/text_index.rs index 15ce1718f3..a4c9d2c521 100644 --- a/crates/metaslang/cst/src/text_index.rs +++ b/crates/metaslang/cst/src/text_index.rs @@ -37,26 +37,12 @@ impl Display for TextIndex { } } -impl From<&str> for TextIndex { - fn from(s: &str) -> Self { +impl> From 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; diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cst.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cst.rs index eff2d3bfe7..f8a79d15a4 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cst.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cst.rs @@ -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", catch_unwind)] @@ -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() } @@ -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)] @@ -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() } } diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs index 3fd9f64c17..64356c7921 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs @@ -65,12 +65,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 diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/parse_error.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/parse_error.rs index ed79439b9e..68bd760644 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/parse_error.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/parse_error.rs @@ -22,7 +22,7 @@ impl From 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)] diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/text_index.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/text_index.rs index 9cb6667c1a..970b0df5ce 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/text_index.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/text_index.rs @@ -12,8 +12,8 @@ pub struct TextIndex { pub char: u32, } -impl From<&RustTextIndex> for TextIndex { - fn from(value: &RustTextIndex) -> Self { +impl From for TextIndex { + fn from(value: RustTextIndex) -> Self { // We only support 32-byte indices on TS side. #[allow(clippy::cast_possible_truncation)] Self { @@ -24,8 +24,8 @@ impl From<&RustTextIndex> for TextIndex { } } -impl From<&TextIndex> for RustTextIndex { - fn from(value: &TextIndex) -> Self { +impl From for RustTextIndex { + fn from(value: TextIndex) -> Self { Self { utf8: value.utf8 as usize, utf16: value.utf16 as usize, @@ -41,11 +41,11 @@ pub struct TextRange { pub end: TextIndex, } -impl From<&RustTextRange> for TextRange { - fn from(value: &RustTextRange) -> Self { +impl From for TextRange { + fn from(value: RustTextRange) -> Self { Self { - start: (&value.start).into(), - end: (&value.end).into(), + start: value.start.into(), + end: value.end.into(), } } } diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cst.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cst.rs index eff2d3bfe7..f8a79d15a4 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cst.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cst.rs @@ -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", catch_unwind)] @@ -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() } @@ -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)] @@ -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() } } diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cursor.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cursor.rs index 3fd9f64c17..64356c7921 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cursor.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cursor.rs @@ -65,12 +65,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 diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/parse_error.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/parse_error.rs index ed79439b9e..68bd760644 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/parse_error.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/parse_error.rs @@ -22,7 +22,7 @@ impl From 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)] diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/text_index.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/text_index.rs index 9cb6667c1a..970b0df5ce 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/text_index.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/text_index.rs @@ -12,8 +12,8 @@ pub struct TextIndex { pub char: u32, } -impl From<&RustTextIndex> for TextIndex { - fn from(value: &RustTextIndex) -> Self { +impl From for TextIndex { + fn from(value: RustTextIndex) -> Self { // We only support 32-byte indices on TS side. #[allow(clippy::cast_possible_truncation)] Self { @@ -24,8 +24,8 @@ impl From<&RustTextIndex> for TextIndex { } } -impl From<&TextIndex> for RustTextIndex { - fn from(value: &TextIndex) -> Self { +impl From for RustTextIndex { + fn from(value: TextIndex) -> Self { Self { utf8: value.utf8 as usize, utf16: value.utf16 as usize, @@ -41,11 +41,11 @@ pub struct TextRange { pub end: TextIndex, } -impl From<&RustTextRange> for TextRange { - fn from(value: &RustTextRange) -> Self { +impl From for TextRange { + fn from(value: RustTextRange) -> Self { Self { - start: (&value.start).into(), - end: (&value.end).into(), + start: value.start.into(), + end: value.end.into(), } } }