Skip to content

Commit 6b2658d

Browse files
authored
KCL: write_to method on Name to avoid allocating a string (#7997)
1 parent 5c471da commit 6b2658d

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

rust/kcl-lib/src/parsing/ast/types/mod.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,26 @@ impl Name {
23962396
}
23972397
}
23982398

2399+
impl Name {
2400+
/// Write the full name to the given string.
2401+
pub fn write_to<W: std::fmt::Write>(&self, buf: &mut W) -> std::fmt::Result {
2402+
if self.abs_path {
2403+
buf.write_str("::")?;
2404+
};
2405+
for p in &self.path {
2406+
buf.write_str(&p.name)?;
2407+
buf.write_str("::")?;
2408+
}
2409+
buf.write_str(&self.name.name)
2410+
}
2411+
}
2412+
2413+
impl fmt::Display for Name {
2414+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2415+
self.write_to(f)
2416+
}
2417+
}
2418+
23992419
impl From<Node<Identifier>> for Node<Name> {
24002420
fn from(value: Node<Identifier>) -> Self {
24012421
let start = value.start;
@@ -2416,19 +2436,6 @@ impl From<Node<Identifier>> for Node<Name> {
24162436
}
24172437
}
24182438

2419-
impl fmt::Display for Name {
2420-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2421-
if self.abs_path {
2422-
f.write_str("::")?;
2423-
}
2424-
for p in &self.path {
2425-
f.write_str(&p.name)?;
2426-
f.write_str("::")?;
2427-
}
2428-
f.write_str(&self.name.name)
2429-
}
2430-
}
2431-
24322439
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Eq)]
24332440
#[ts(export)]
24342441
#[serde(tag = "type")]

rust/kcl-lib/src/unparser.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl BinaryPart {
349349
}
350350
BinaryPart::Name(name) => match deprecation(&name.inner.name.inner.name, DeprecationKind::Const) {
351351
Some(suggestion) => write!(buf, "{suggestion}").no_fail(),
352-
None => write!(buf, "{name}").no_fail(),
352+
None => name.write_to(buf).no_fail(),
353353
},
354354
BinaryPart::BinaryExpression(binary_expression) => {
355355
binary_expression.recast(buf, options, indentation_level, ctxt)
@@ -423,7 +423,7 @@ impl CallExpressionKw {
423423
options.get_indentation(indentation_level)
424424
};
425425
options.write_indentation(buf, smart_indent_level);
426-
write!(buf, "{name}").no_fail();
426+
name.write_to(buf).no_fail();
427427
buf.push('(');
428428
buf.push('\n');
429429
write!(buf, "{inner_indentation}").no_fail();
@@ -433,7 +433,7 @@ impl CallExpressionKw {
433433
buf.push(')');
434434
} else {
435435
options.write_indentation(buf, smart_indent_level);
436-
write!(buf, "{name}").no_fail();
436+
name.write_to(buf).no_fail();
437437
buf.push('(');
438438
write!(buf, "{args}").no_fail();
439439
buf.push(')');
@@ -546,7 +546,8 @@ impl Literal {
546546
impl TagDeclarator {
547547
pub fn recast(&self, buf: &mut String) {
548548
// TagDeclarators are always prefixed with a dollar sign.
549-
write!(buf, "${}", self.name).no_fail()
549+
buf.push('$');
550+
buf.push_str(&self.name);
550551
}
551552
}
552553

0 commit comments

Comments
 (0)