Skip to content

Commit

Permalink
Avoid allocations for parameters when not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Nov 30, 2024
1 parent 430ebe2 commit 2d54d63
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/bbcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub struct BbcodeTag<'a> {
name: &'a str,

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

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

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

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

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

/// If it exists, the simple tag parameter of this tag.
pub fn simple_param(&self) -> &Option<String> {
pub fn simple_param(&self) -> &Option<Cow<'a, str>> {
&self.simple_param
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/bevy/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl BbcodeContext {
}
} else {
Self {
color: color.clone().into(),
color: color.to_string().into(),
..self.clone()
}
}
Expand All @@ -60,7 +60,7 @@ impl BbcodeContext {
"m" | "marker" => {
if let Some(marker) = tag.simple_param() {
let mut markers = self.markers.clone();
markers.push(marker.clone());
markers.push(marker.to_string());

Self {
markers,
Expand All @@ -74,7 +74,7 @@ impl BbcodeContext {
"font" => {
if let Some(font_family) = tag.simple_param() {
Self {
font_family: font_family.clone(),
font_family: font_family.to_string(),
..self.clone()
}
} else {
Expand Down

0 comments on commit 2d54d63

Please sign in to comment.