Skip to content

Commit 7dc0b17

Browse files
committed
Convert some Cow<'src, str> usages into &'src str
1 parent be4c041 commit 7dc0b17

File tree

4 files changed

+31
-39
lines changed

4 files changed

+31
-39
lines changed

librubyfmt/src/format_prism.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ fn format_case_node<'src>(ps: &mut ParserState<'src>, case_node: prism::CaseNode
629629
pub fn format_program<'src>(
630630
ps: &mut ParserState<'src>,
631631
program_node: prism::ProgramNode<'src>,
632-
data_loc: Option<prism::Location>,
632+
data_loc: Option<prism::Location<'src>>,
633633
) {
634634
ps.with_start_of_line(true, |ps| {
635635
format_statements(ps, program_node.statements());
@@ -1395,7 +1395,7 @@ fn format_def_body<'src>(ps: &mut ParserState<'src>, def_node: prism::DefNode<'s
13951395
});
13961396
} else {
13971397
ps.emit_space();
1398-
ps.emit_op(Cow::Borrowed("="));
1398+
ps.emit_op("=");
13991399
ps.emit_space();
14001400

14011401
ps.with_start_of_line(
@@ -2485,7 +2485,7 @@ fn format_call_and_write_node<'src>(
24852485
}
24862486

24872487
ps.emit_space();
2488-
ps.emit_op(Cow::Borrowed("&&="));
2488+
ps.emit_op("&&=");
24892489
ps.emit_space();
24902490

24912491
ps.with_start_of_line(false, |ps| format_node(ps, call_and_write_node.value()));
@@ -2533,7 +2533,7 @@ fn format_call_or_write_node<'src>(
25332533
}
25342534

25352535
ps.emit_space();
2536-
ps.emit_op(Cow::Borrowed("||="));
2536+
ps.emit_op("||=");
25372537
ps.emit_space();
25382538

25392539
ps.with_start_of_line(false, |ps| format_node(ps, call_or_write_node.value()));
@@ -3932,7 +3932,7 @@ fn format_index_and_write_node<'src>(
39323932
}
39333933

39343934
ps.emit_space();
3935-
ps.emit_op(Cow::Borrowed("&&="));
3935+
ps.emit_op("&&=");
39363936
ps.emit_space();
39373937

39383938
ps.with_start_of_line(false, |ps| format_node(ps, index_and_write_node.value()));
@@ -3976,7 +3976,7 @@ fn format_index_or_write_node<'src>(
39763976
}
39773977

39783978
ps.emit_space();
3979-
ps.emit_op(Cow::Borrowed("||="));
3979+
ps.emit_op("||=");
39803980
ps.emit_space();
39813981

39823982
ps.with_start_of_line(false, |ps| format_node(ps, index_or_write_node.value()));
@@ -4476,7 +4476,7 @@ fn format_optional_keyword_parameter_node<'src>(
44764476
let name = const_to_str(optional_keyword_parameter_node.name());
44774477
ps.bind_variable(name);
44784478
ps.emit_ident(name);
4479-
ps.emit_op(Cow::Borrowed(":"));
4479+
ps.emit_op(":");
44804480
ps.emit_space();
44814481
ps.with_start_of_line(false, |ps| {
44824482
format_node(ps, optional_keyword_parameter_node.value());
@@ -4491,7 +4491,7 @@ fn format_optional_parameter_node<'src>(
44914491
ps.bind_variable(name);
44924492
ps.emit_ident(name);
44934493
ps.emit_space();
4494-
ps.emit_op(Cow::Borrowed("="));
4494+
ps.emit_op("=");
44954495
ps.emit_space();
44964496
format_node(ps, optional_parameter_node.value());
44974497
}
@@ -4653,7 +4653,7 @@ fn format_rescue_node<'src>(ps: &mut ParserState<'src>, rescue_node: prism::Resc
46534653

46544654
if let Some(reference) = rescue_node.reference() {
46554655
ps.emit_space();
4656-
ps.emit_op(Cow::Borrowed("=>"));
4656+
ps.emit_op("=>");
46574657
ps.emit_space();
46584658
ps.with_start_of_line(false, |ps| {
46594659
format_node(ps, reference);

librubyfmt/src/intermediary.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ impl<'src> Intermediary<'src> {
131131
}
132132
}
133133
ConcreteLineToken::MethodName { name } => {
134-
if name == "require" && self.tokens.last().map(|t| t.is_indent()).unwrap_or(false) {
134+
if *name == "require" && self.tokens.last().map(|t| t.is_indent()).unwrap_or(false)
135+
{
135136
self.current_line_metadata.set_has_require();
136137
}
137138
}

librubyfmt/src/line_tokens.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub enum ConcreteLineToken<'src> {
4646
part: Cow<'src, str>,
4747
},
4848
MethodName {
49-
name: Cow<'src, str>,
49+
name: &'src str,
5050
},
5151
CommaSpace,
5252
Comma,
@@ -62,7 +62,7 @@ pub enum ConcreteLineToken<'src> {
6262
CloseParen,
6363
ParenExprClose,
6464
Op {
65-
op: Cow<'src, str>,
65+
op: &'src str,
6666
},
6767
DoubleQuote,
6868
LTStringContent {
@@ -86,7 +86,7 @@ pub enum ConcreteLineToken<'src> {
8686
EndCallChainIndent,
8787
HeredocStart {
8888
kind: HeredocKind,
89-
symbol: Cow<'src, str>,
89+
symbol: &'src str,
9090
},
9191
}
9292

@@ -102,7 +102,7 @@ impl<'src> ConcreteLineToken<'src> {
102102
Self::DefKeyword => Cow::Borrowed("def"),
103103
Self::ModuleKeyword => Cow::Borrowed("module"),
104104
Self::DirectPart { part } => part,
105-
Self::MethodName { name } => name,
105+
Self::MethodName { name } => Cow::Borrowed(name),
106106
Self::CommaSpace => Cow::Borrowed(", "),
107107
Self::Comma => Cow::Borrowed(","),
108108
Self::Space => Cow::Borrowed(" "),
@@ -115,15 +115,15 @@ impl<'src> ConcreteLineToken<'src> {
115115
Self::CloseCurlyBracket => Cow::Borrowed("}"),
116116
Self::OpenParen => Cow::Borrowed("("),
117117
Self::CloseParen | Self::ParenExprClose => Cow::Borrowed(")"),
118-
Self::Op { op } => op,
118+
Self::Op { op } => Cow::Borrowed(op),
119119
Self::DoubleQuote => Cow::Borrowed("\""),
120120
Self::LTStringContent { content } => content,
121121
Self::SingleSlash => Cow::Borrowed("\\"),
122122
Self::Comment { contents } => Cow::Owned(contents),
123123
Self::Delim { contents } => Cow::Borrowed(contents),
124124
Self::End => Cow::Borrowed("end"),
125125
Self::HeredocClose { symbol } => Cow::Owned(symbol),
126-
Self::HeredocStart { symbol, .. } => symbol,
126+
Self::HeredocStart { symbol, .. } => Cow::Borrowed(symbol),
127127
// no-op, this is purely semantic information
128128
// for the render queue
129129
Self::AfterCallChain | Self::BeginCallChainIndent | Self::EndCallChainIndent => {
@@ -144,10 +144,8 @@ impl<'src> ConcreteLineToken<'src> {
144144
Delim { contents } => contents.len(),
145145
Indent { depth } => *depth as usize,
146146
Keyword { keyword: contents } | ConditionalKeyword { contents } => contents.len(),
147-
Op { op: contents }
148-
| DirectPart { part: contents }
149-
| MethodName { name: contents }
150-
| LTStringContent { content: contents } => contents.len(),
147+
Op { op } | MethodName { name: op } => op.len(),
148+
DirectPart { part: contents } | LTStringContent { content: contents } => contents.len(),
151149
Comment { contents } | HeredocClose { symbol: contents } => contents.len(),
152150
HardNewLine | Comma | Space | Dot | OpenSquareBracket | CloseSquareBracket
153151
| OpenCurlyBracket | CloseCurlyBracket | OpenParen | CloseParen | ParenExprClose
@@ -162,7 +160,7 @@ impl<'src> ConcreteLineToken<'src> {
162160
fn is_block_closing_token(&self) -> bool {
163161
match self {
164162
Self::End | Self::ParenExprClose => true,
165-
Self::DirectPart { part } => part == "}" || part == "]" || part == ")",
163+
Self::DirectPart { part } => *part == "}" || *part == "]" || *part == ")",
166164
Self::Delim { contents } => *contents == "}" || *contents == "]" || *contents == ")",
167165
_ => false,
168166
}
@@ -172,7 +170,7 @@ impl<'src> ConcreteLineToken<'src> {
172170
match self {
173171
Self::ConditionalKeyword { contents } => !(*contents == "else" || *contents == "elsif"),
174172
Self::Dot | Self::LonelyOperator => false,
175-
Self::DirectPart { part } => part != "&.",
173+
Self::DirectPart { part } => *part != "&.",
176174
_ => true,
177175
}
178176
}
@@ -189,7 +187,7 @@ impl<'src> ConcreteLineToken<'src> {
189187
match self {
190188
Self::HardNewLine => true,
191189
Self::DirectPart { part } => {
192-
if part == "\n" {
190+
if *part == "\n" {
193191
panic!("shouldn't ever have a single newline direct part");
194192
} else {
195193
false

librubyfmt/src/parser_state.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,8 @@ impl<'src> ParserState<'src> {
123123
));
124124
}
125125

126-
pub(crate) fn emit_heredoc_start(
127-
&mut self,
128-
symbol: impl Into<Cow<'src, str>>,
129-
kind: HeredocKind,
130-
) {
131-
self.push_concrete_token(ConcreteLineToken::HeredocStart {
132-
kind,
133-
symbol: symbol.into(),
134-
});
126+
pub(crate) fn emit_heredoc_start(&mut self, symbol: &'src str, kind: HeredocKind) {
127+
self.push_concrete_token(ConcreteLineToken::HeredocStart { kind, symbol });
135128
}
136129

137130
pub(crate) fn emit_heredoc_close(&mut self, symbol: String) {
@@ -416,8 +409,8 @@ impl<'src> ParserState<'src> {
416409
.merge(comments.apply_spaces(self.spaces_after_last_newline));
417410
}
418411

419-
pub(crate) fn emit_op(&mut self, op: impl Into<Cow<'src, str>>) {
420-
self.push_concrete_token(ConcreteLineToken::Op { op: op.into() });
412+
pub(crate) fn emit_op(&mut self, op: &'src str) {
413+
self.push_concrete_token(ConcreteLineToken::Op { op });
421414
}
422415

423416
pub(crate) fn emit_double_quote(&mut self) {
@@ -435,12 +428,12 @@ impl<'src> ParserState<'src> {
435428
self.push_concrete_token(ConcreteLineToken::LTStringContent { content });
436429
}
437430

438-
pub(crate) fn emit_ident(&mut self, ident: impl Into<Cow<'src, str>>) {
431+
pub(crate) fn emit_ident(&mut self, ident: &'src str) {
439432
self.push_concrete_token(ConcreteLineToken::DirectPart { part: ident.into() });
440433
}
441434

442-
pub(crate) fn emit_method_name(&mut self, name: impl Into<Cow<'src, str>>) {
443-
self.push_concrete_token(ConcreteLineToken::MethodName { name: name.into() });
435+
pub(crate) fn emit_method_name(&mut self, name: &'src str) {
436+
self.push_concrete_token(ConcreteLineToken::MethodName { name });
444437
}
445438

446439
pub(crate) fn emit_newline(&mut self) {
@@ -627,9 +620,9 @@ impl<'src> ParserState<'src> {
627620
self.emit_conditional_keyword("else");
628621
}
629622

630-
pub(crate) fn emit_data(&mut self, data: &str) {
623+
pub(crate) fn emit_data(&mut self, data: &'src str) {
631624
self.push_concrete_token(ConcreteLineToken::DirectPart {
632-
part: Cow::Owned(data.to_string()),
625+
part: Cow::Borrowed(data),
633626
})
634627
}
635628

0 commit comments

Comments
 (0)