Skip to content

Commit

Permalink
save: Remove unnecessary Cow<Operation>.
Browse files Browse the repository at this point in the history
`Operation` is designed to be cheap to clone (at most a few refcount
changes).
  • Loading branch information
kpreid committed Nov 2, 2024
1 parent a4eb798 commit 6dff69d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
16 changes: 7 additions & 9 deletions all-is-cubes/src/save/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mod block {
}
}

impl<'a> From<&'a Primitive> for schema::PrimitiveSer<'a> {
impl<'a> From<&'a Primitive> for schema::PrimitiveSer {
fn from(value: &'a Primitive) -> Self {
match value {
Primitive::Indirect(definition) => schema::PrimitiveSer::IndirectV1 {
Expand Down Expand Up @@ -160,9 +160,7 @@ mod block {
}
}

fn primitive_from_schema(
value: schema::PrimitiveSer<'_>,
) -> (Primitive, Option<BlockAttributes>) {
fn primitive_from_schema(value: schema::PrimitiveSer) -> (Primitive, Option<BlockAttributes>) {
match value {
schema::PrimitiveSer::IndirectV1 { definition } => {
(Primitive::Indirect(definition), None)
Expand Down Expand Up @@ -204,7 +202,7 @@ mod block {
}
}

impl<'a> From<&'a BlockAttributes> for schema::BlockAttributesV1Ser<'a> {
impl<'a> From<&'a BlockAttributes> for schema::BlockAttributesV1Ser {
fn from(value: &'a BlockAttributes) -> Self {
let &BlockAttributes {
ref display_name,
Expand All @@ -225,7 +223,7 @@ mod block {
ref operation,
schedule,
}| schema::TickActionSer {
operation: Cow::Borrowed(operation),
operation: operation.clone(),
schedule,
},
),
Expand All @@ -235,8 +233,8 @@ mod block {
}
}

impl<'a> From<schema::BlockAttributesV1Ser<'a>> for BlockAttributes {
fn from(value: schema::BlockAttributesV1Ser<'a>) -> Self {
impl From<schema::BlockAttributesV1Ser> for BlockAttributes {
fn from(value: schema::BlockAttributesV1Ser) -> Self {
let schema::BlockAttributesV1Ser {
display_name,
selectable,
Expand All @@ -257,7 +255,7 @@ mod block {
schedule,
}| {
TickAction {
operation: operation.into_owned(),
operation,
schedule,
}
},
Expand Down
20 changes: 10 additions & 10 deletions all-is-cubes/src/save/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,30 @@ pub(crate) enum BehaviorV1Ser {
#[serde(tag = "type")]
pub(crate) enum BlockSer<'a> {
BlockV1 {
primitive: PrimitiveSer<'a>,
primitive: PrimitiveSer,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
modifiers: Vec<ModifierSer<'a>>,
},
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
pub(crate) enum PrimitiveSer<'a> {
pub(crate) enum PrimitiveSer {
AirV1,
AtomV1 {
color: RgbaSer,
#[serde(default, skip_serializing_if = "is_default")]
light_emission: RgbSer,
/// Note: Attributes stored on the primitive are no longer used, and supported only for deserialization.
#[serde(flatten)]
attributes: BlockAttributesV1Ser<'a>,
attributes: BlockAttributesV1Ser,
#[serde(default, skip_serializing_if = "is_default")]
collision: BlockCollisionSer,
},
RecurV1 {
/// Note: Attributes stored on the primitive are no longer used, and supported only for deserialization.
#[serde(flatten)]
attributes: BlockAttributesV1Ser<'a>,
attributes: BlockAttributesV1Ser,
space: Handle<space::Space>,
#[serde(default, skip_serializing_if = "is_default")]
offset: [i32; 3],
Expand All @@ -105,7 +105,7 @@ pub(crate) enum PrimitiveSer<'a> {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub(crate) struct BlockAttributesV1Ser<'a> {
pub(crate) struct BlockAttributesV1Ser {
#[serde(default, skip_serializing_if = "str::is_empty")]
pub display_name: ArcStr,
#[serde(default = "return_true", skip_serializing_if = "is_true")]
Expand All @@ -115,9 +115,9 @@ pub(crate) struct BlockAttributesV1Ser<'a> {
#[serde(default, skip_serializing_if = "is_default")]
pub rotation_rule: RotationPlacementRuleSer,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tick_action: Option<TickActionSer<'a>>,
pub tick_action: Option<TickActionSer>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub activation_action: Option<op::Operation>, // no Cow because Operation is cheap to clone
pub activation_action: Option<op::Operation>,
#[serde(default, skip_serializing_if = "is_default")]
pub animation_hint: AnimationHintSer,
}
Expand Down Expand Up @@ -151,8 +151,8 @@ pub(crate) enum RotationPlacementRuleSer {

/// Unversioned because it's versioned by the parent struct
#[derive(Clone, Debug, Deserialize, Serialize)]
pub(crate) struct TickActionSer<'a> {
pub operation: Cow<'a, op::Operation>,
pub(crate) struct TickActionSer {
pub operation: op::Operation,
pub schedule: Schedule,
}
#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Serialize)]
Expand Down Expand Up @@ -184,7 +184,7 @@ pub(crate) enum AnimationChangeV1Ser {
pub(crate) enum ModifierSer<'a> {
AttributesV1 {
#[serde(flatten)]
attributes: BlockAttributesV1Ser<'a>,
attributes: BlockAttributesV1Ser,
},
QuoteV1 {
suppress_ambient: bool,
Expand Down

0 comments on commit 6dff69d

Please sign in to comment.