Skip to content

Commit

Permalink
Avoid cloning display_name in serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Oct 7, 2023
1 parent 6609d74 commit 3bd99d5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 7 additions & 3 deletions all-is-cubes/src/save/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::string::ToString;
use alloc::sync::Arc;
use alloc::vec::Vec;

Expand Down Expand Up @@ -189,7 +188,7 @@ mod block {
animation_hint,
} = value;
schema::BlockAttributesV1Ser {
display_name: display_name.to_string(),
display_name: Cow::Borrowed(&**display_name),
selectable,
rotation_rule: rotation_rule.into(),
tick_action: tick_action.as_ref().map(
Expand Down Expand Up @@ -217,7 +216,12 @@ mod block {
animation_hint,
} = value;
Self {
display_name: display_name.into(),
// convert Cow<'a> to Cow<'static>
display_name: if display_name == "" {
Cow::Borrowed("")
} else {
Cow::Owned(display_name.into())
},
selectable,
rotation_rule: rotation_rule.into(),
tick_action: tick_action.map(|schema::TickActionSer { operation, period }| {
Expand Down
5 changes: 2 additions & 3 deletions all-is-cubes/src/save/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
//! * [`Cow`] is sometimes used to avoid unnecessary clones during serialization.
use alloc::borrow::Cow;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::num::NonZeroU16;
Expand Down Expand Up @@ -99,8 +98,8 @@ pub(crate) enum PrimitiveSer<'a> {

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct BlockAttributesV1Ser<'a> {
#[serde(default, skip_serializing_if = "String::is_empty")]
pub display_name: String, // TODO: Cow
#[serde(default, skip_serializing_if = "str::is_empty")]
pub display_name: Cow<'a, str>,
#[serde(default = "return_true", skip_serializing_if = "is_true")]
pub selectable: bool,
#[serde(default, skip_serializing_if = "is_default")]
Expand Down

0 comments on commit 3bd99d5

Please sign in to comment.