Skip to content

Commit 2d54d63

Browse files
committed
Avoid allocations for parameters when not necessary
1 parent 430ebe2 commit 2d54d63

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/bbcode/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ pub struct BbcodeTag<'a> {
2323
name: &'a str,
2424

2525
/// A simple parameter for the tag, e.g. `value` for `[tag=value]something[/tag]`.
26-
simple_param: Option<String>,
26+
simple_param: Option<Cow<'a, str>>,
2727

2828
/// Complex parameters, e.g. the map `value1` -> `xxx`, `value2` -> `yyy` for `[tag value1=”xxx” value2=”yyy”]something[/tag]`.
29-
complex_params: HashMap<String, String>,
29+
complex_params: HashMap<Cow<'a, str>, Cow<'a, str>>,
3030

3131
/// The child nodes (or text) contained inside this node.
3232
children: Vec<Arc<BbcodeNode<'a>>>,
@@ -51,14 +51,14 @@ impl<'a> BbcodeTag<'a> {
5151
}
5252

5353
/// Add a simple parameter to the tag.
54-
pub fn add_simple_param<P: Into<String>>(&mut self, tag_param: P) -> &mut Self {
54+
pub fn add_simple_param<P: Into<Cow<'a, str>>>(&mut self, tag_param: P) -> &mut Self {
5555
self.simple_param = Some(tag_param.into());
5656
self
5757
}
5858

5959
/// Add a key/value parameter.
6060
#[cfg(test)]
61-
pub fn with_param<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
61+
pub fn with_param<K: Into<&'a str>, V: Into<Cow<'a, str>>>(mut self, key: K, value: V) -> Self {
6262
self.complex_params.insert(key.into(), value.into());
6363
self
6464
}
@@ -89,7 +89,7 @@ impl<'a> BbcodeTag<'a> {
8989
}
9090

9191
/// If it exists, the simple tag parameter of this tag.
92-
pub fn simple_param(&self) -> &Option<String> {
92+
pub fn simple_param(&self) -> &Option<Cow<'a, str>> {
9393
&self.simple_param
9494
}
9595
}

src/bevy/conversion.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl BbcodeContext {
4848
}
4949
} else {
5050
Self {
51-
color: color.clone().into(),
51+
color: color.to_string().into(),
5252
..self.clone()
5353
}
5454
}
@@ -60,7 +60,7 @@ impl BbcodeContext {
6060
"m" | "marker" => {
6161
if let Some(marker) = tag.simple_param() {
6262
let mut markers = self.markers.clone();
63-
markers.push(marker.clone());
63+
markers.push(marker.to_string());
6464

6565
Self {
6666
markers,
@@ -74,7 +74,7 @@ impl BbcodeContext {
7474
"font" => {
7575
if let Some(font_family) = tag.simple_param() {
7676
Self {
77-
font_family: font_family.clone(),
77+
font_family: font_family.to_string(),
7878
..self.clone()
7979
}
8080
} else {

0 commit comments

Comments
 (0)