Skip to content

Commit

Permalink
[rs] Box big variants in enums to reduce their size
Browse files Browse the repository at this point in the history
  • Loading branch information
moulins committed May 7, 2022
1 parent 4dbd314 commit e6e6605
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- **[Breaking change]** Use bitflags instead of separate `bool` fields in `Bevel`, `ButtonCond`, `ButtonRecord`,
`ClipEventFlags`, `DefineDynamicText`, `DefineFont`, `DefineFontInfo`, `FileAttributes`, `Glow`, `GradientBevel`,
`GradientGlow`, `LineStyle` and `MorphLineStyle`.
- **[Breaking change]** Box big variants to reduce size of enums `FillStyle`, `MorphFillStyle`, `MorphShapeRecord`,
`ShapeRecord` and `Tag`.

# 0.14.0 (2022-05-07)

Expand Down
7 changes: 7 additions & 0 deletions rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ path = "src/lib.rs"
bitflags = "1.3.2"
hex = { version = "0.4.3", optional = true }
serde = { version = "1.0.137", optional = true, features = ["derive"] }
static_assertions = "1.1.0"
swf-fixed = "0.1.5"

[features]
Expand Down
19 changes: 11 additions & 8 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "serde")]
use ::serde::{Deserialize, Serialize};
use static_assertions::const_assert;
pub use ::swf_fixed as fixed;

pub use crate::basic_types::ColorTransform;
Expand Down Expand Up @@ -141,22 +142,22 @@ pub enum Tag {
DefineBitmap(tags::DefineBitmap),
DefineButton(tags::DefineButton),
DefineButtonColorTransform(tags::DefineButtonColorTransform),
DefineButtonSound(tags::DefineButtonSound),
DefineButtonSound(Box<tags::DefineButtonSound>),
DefineCffFont(tags::DefineCffFont),
DefineDynamicText(tags::DefineDynamicText),
DefineFont(tags::DefineFont),
DefineDynamicText(Box<tags::DefineDynamicText>),
DefineFont(Box<tags::DefineFont>),
DefineFontAlignZones(tags::DefineFontAlignZones),
DefineFontInfo(tags::DefineFontInfo),
DefineFontName(tags::DefineFontName),
DefineGlyphFont(tags::DefineGlyphFont),
DefineJpegTables(tags::DefineJpegTables),
DefineMorphShape(tags::DefineMorphShape),
DefineMorphShape(Box<tags::DefineMorphShape>),
DefineScalingGrid(tags::DefineScalingGrid),
DefineSceneAndFrameLabelData(tags::DefineSceneAndFrameLabelData),
DefineShape(tags::DefineShape),
DefineShape(Box<tags::DefineShape>),
DefineSound(tags::DefineSound),
DefineSprite(tags::DefineSprite),
DefineText(tags::DefineText),
DefineText(Box<tags::DefineText>),
DefineVideoStream(tags::DefineVideoStream),
EnablePostscript,
DoAbc(tags::DoAbc),
Expand All @@ -168,7 +169,7 @@ pub enum Tag {
FrameLabel(tags::FrameLabel),
ImportAssets(tags::ImportAssets),
Metadata(tags::Metadata),
PlaceObject(tags::PlaceObject),
PlaceObject(Box<tags::PlaceObject>),
Protect(tags::Protect),
Raw(tags::Raw),
RawBody(tags::RawBody),
Expand All @@ -180,12 +181,14 @@ pub enum Tag {
SoundStreamBlock(tags::SoundStreamBlock),
SoundStreamHead(tags::SoundStreamHead),
StartSound(tags::StartSound),
StartSound2(tags::StartSound2),
StartSound2(Box<tags::StartSound2>),
SymbolClass(tags::SymbolClass),
Telemetry(tags::Telemetry),
VideoFrame(tags::VideoFrame),
}

const_assert!(std::mem::size_of::<Tag>() <= 64);

#[cfg(all(test, feature = "serde"))]
mod tests {
use std::path::Path;
Expand Down
29 changes: 19 additions & 10 deletions rs/src/shape.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "serde")]
use ::serde::{Deserialize, Serialize};
use static_assertions::const_assert;

use crate::fill_styles;
use crate::join_styles;
Expand Down Expand Up @@ -77,27 +78,31 @@ serde_bitflags! {
)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FillStyle {
Bitmap(fill_styles::Bitmap),
FocalGradient(fill_styles::FocalGradient),
LinearGradient(fill_styles::LinearGradient),
RadialGradient(fill_styles::RadialGradient),
Bitmap(Box<fill_styles::Bitmap>),
FocalGradient(Box<fill_styles::FocalGradient>),
LinearGradient(Box<fill_styles::LinearGradient>),
RadialGradient(Box<fill_styles::RadialGradient>),
Solid(fill_styles::Solid),
}

const_assert!(std::mem::size_of::<FillStyle>() <= 16);

#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(tag = "type", rename_all = "PascalCase")
)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MorphFillStyle {
Bitmap(fill_styles::MorphBitmap),
FocalGradient(fill_styles::MorphFocalGradient),
LinearGradient(fill_styles::MorphLinearGradient),
RadialGradient(fill_styles::MorphRadialGradient),
Bitmap(Box<fill_styles::MorphBitmap>),
FocalGradient(Box<fill_styles::MorphFocalGradient>),
LinearGradient(Box<fill_styles::MorphLinearGradient>),
RadialGradient(Box<fill_styles::MorphRadialGradient>),
Solid(fill_styles::MorphSolid),
}

const_assert!(std::mem::size_of::<MorphFillStyle>() <= 16);

#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down Expand Up @@ -176,9 +181,11 @@ pub struct MorphShape {
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ShapeRecord {
Edge(shape_records::Edge),
StyleChange(shape_records::StyleChange),
StyleChange(Box<shape_records::StyleChange>),
}

const_assert!(std::mem::size_of::<ShapeRecord>() <= 24);

#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand All @@ -187,9 +194,11 @@ pub enum ShapeRecord {
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MorphShapeRecord {
Edge(shape_records::MorphEdge),
StyleChange(shape_records::MorphStyleChange),
StyleChange(Box<shape_records::MorphStyleChange>),
}

const_assert!(std::mem::size_of::<MorphShapeRecord>() <= 48);

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ShapeStyles {
Expand Down

0 comments on commit e6e6605

Please sign in to comment.