Skip to content

Commit

Permalink
[codegen] Derive Copy for simple records
Browse files Browse the repository at this point in the history
That is, if a record has a fixed size we will derive the Copy trait.
This will be necessary if we want to use bytemuck, and should let us
simplify some other things.

- See #659 for previous
  discussion
  • Loading branch information
cmyr committed Mar 5, 2024
1 parent 3d0d10e commit 95ac9bf
Show file tree
Hide file tree
Showing 23 changed files with 62 additions and 69 deletions.
13 changes: 3 additions & 10 deletions font-codegen/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub(crate) fn generate(item: &Record, all_items: &Items) -> syn::Result<TokenStr
#[repr(packed)]
}
});
let maybe_copy = is_zerocopy.then(|| quote!(Copy,));

let maybe_impl_fixed_size = is_zerocopy.then(|| {
let inner_types = item.fields.iter().map(|fld| fld.typ.cooked_type_tokens());
Expand All @@ -52,20 +53,12 @@ pub(crate) fn generate(item: &Record, all_items: &Items) -> syn::Result<TokenStr
}
});
let maybe_impl_read_with_args = (has_read_args).then(|| generate_read_with_args(item));
let mut maybe_extra_traits = item
let maybe_extra_traits = item
.gets_extra_traits(all_items)
.then(|| quote!(PartialEq, Eq, PartialOrd, Ord, Hash));
// Just make all records Copy?
// <https://github.com/googlefonts/fontations/issues/659>
if item.name == "SbitLineMetrics" {
use quote::TokenStreamExt;
let mut copy_trait = quote!(Copy,);
copy_trait.append_all(maybe_extra_traits.unwrap_or_default());
maybe_extra_traits = Some(copy_trait);
}
Ok(quote! {
#( #docs )*
#[derive(Clone, Debug, #maybe_extra_traits)]
#[derive(Clone, Debug, #maybe_copy #maybe_extra_traits)]
#repr_packed
pub struct #name #lifetime {
#( #field_docs pub #field_names: #field_types, )*
Expand Down
2 changes: 1 addition & 1 deletion read-fonts/generated/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> std::fmt::Debug for TableDirectory<'a> {
}

/// Record for a table in a font.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct TableRecord {
Expand Down
2 changes: 1 addition & 1 deletion read-fonts/generated/generated_avar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<'a> SomeRecord<'a> for SegmentMaps<'a> {
}

/// [AxisValueMap](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct AxisValueMap {
Expand Down
6 changes: 3 additions & 3 deletions read-fonts/generated/generated_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl<'a> std::fmt::Debug for BaseScriptList<'a> {
}

/// [BaseScriptRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescriptrecord)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct BaseScriptRecord {
Expand Down Expand Up @@ -550,7 +550,7 @@ impl<'a> std::fmt::Debug for BaseScript<'a> {
}

/// [BaseLangSysRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#baselangsysrecord)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct BaseLangSysRecord {
Expand Down Expand Up @@ -839,7 +839,7 @@ impl<'a> std::fmt::Debug for MinMax<'a> {
}

/// [FeatMinMaxRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#baselangsysrecord)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct FeatMinMaxRecord {
Expand Down
10 changes: 5 additions & 5 deletions read-fonts/generated/generated_bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::codegen_prelude::*;

/// [BitmapSize](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#bitmapsize-record) record.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct BitmapSize {
Expand Down Expand Up @@ -583,7 +583,7 @@ impl<'a> From<BitmapFlags> for FieldType<'a> {
}

/// [BigGlyphMetrics](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#bigglyphmetrics) record.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct BigGlyphMetrics {
Expand Down Expand Up @@ -687,7 +687,7 @@ impl<'a> SomeRecord<'a> for BigGlyphMetrics {
}

/// [SmallGlyphMetrics](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#smallglyphmetrics) record.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct SmallGlyphMetrics {
Expand Down Expand Up @@ -1354,7 +1354,7 @@ impl<'a> std::fmt::Debug for IndexSubtable4<'a> {
}

/// [GlyphIdOffsetPair](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#glyphidoffsetpair-record) record.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct GlyphIdOffsetPair {
Expand Down Expand Up @@ -1545,7 +1545,7 @@ impl<'a> std::fmt::Debug for IndexSubtable5<'a> {
}

/// [EbdtComponent](https://learn.microsoft.com/en-us/typography/opentype/spec/ebdt#ebdtcomponent-record) record.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct BdtComponent {
Expand Down
14 changes: 7 additions & 7 deletions read-fonts/generated/generated_cmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<'a> std::fmt::Debug for Cmap<'a> {
}

/// [Encoding Record](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#encoding-records-and-encodings)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct EncodingRecord {
Expand Down Expand Up @@ -498,7 +498,7 @@ impl<'a> std::fmt::Debug for Cmap2<'a> {
}

/// Part of [Cmap2]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct SubHeader {
Expand Down Expand Up @@ -1038,7 +1038,7 @@ impl<'a> std::fmt::Debug for Cmap8<'a> {
}

/// Used in [Cmap8] and [Cmap12]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct SequentialMapGroup {
Expand Down Expand Up @@ -1468,7 +1468,7 @@ impl<'a> std::fmt::Debug for Cmap13<'a> {
}

/// Part of [Cmap13]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct ConstantMapGroup {
Expand Down Expand Up @@ -1634,7 +1634,7 @@ impl<'a> std::fmt::Debug for Cmap14<'a> {
}

/// Part of [Cmap14]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct VariationSelector {
Expand Down Expand Up @@ -1872,7 +1872,7 @@ impl<'a> std::fmt::Debug for NonDefaultUvs<'a> {
}

/// Part of [Cmap14]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct UvsMapping {
Expand Down Expand Up @@ -1921,7 +1921,7 @@ impl<'a> SomeRecord<'a> for UvsMapping {
}

/// Part of [Cmap14]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct UnicodeRange {
Expand Down
16 changes: 8 additions & 8 deletions read-fonts/generated/generated_colr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl<'a> std::fmt::Debug for Colr<'a> {
}

/// [BaseGlyph](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#baseglyph-and-layer-records) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct BaseGlyph {
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<'a> SomeRecord<'a> for BaseGlyph {
}

/// [Layer](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#baseglyph-and-layer-records) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct Layer {
Expand Down Expand Up @@ -478,7 +478,7 @@ impl<'a> std::fmt::Debug for BaseGlyphList<'a> {
}

/// [BaseGlyphPaint](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#baseglyphlist-layerlist-and-cliplist) record
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct BaseGlyphPaint {
Expand Down Expand Up @@ -710,7 +710,7 @@ impl<'a> std::fmt::Debug for ClipList<'a> {
}

/// [Clip](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#baseglyphlist-layerlist-and-cliplist) record
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct Clip {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ impl<'a> std::fmt::Debug for ClipBoxFormat2<'a> {
}

/// [ColorIndex](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#color-references-colorstop-and-colorline) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct ColorIndex {
Expand Down Expand Up @@ -1133,7 +1133,7 @@ impl<'a> SomeRecord<'a> for ColorIndex {
}

/// [VarColorIndex](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#color-references-colorstop-and-colorline) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct VarColorIndex {
Expand Down Expand Up @@ -1190,7 +1190,7 @@ impl<'a> SomeRecord<'a> for VarColorIndex {
}

/// [ColorStop](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#color-references-colorstop-and-colorline) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct ColorStop {
Expand Down Expand Up @@ -1247,7 +1247,7 @@ impl<'a> SomeRecord<'a> for ColorStop {
}

/// [VarColorStop](https://learn.microsoft.com/en-us/typography/opentype/spec/colr#color-references-colorstop-and-colorline) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct VarColorStop {
Expand Down
2 changes: 1 addition & 1 deletion read-fonts/generated/generated_cpal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ impl<'a> From<PaletteType> for FieldType<'a> {
}

/// [CPAL (Color Record)](https://learn.microsoft.com/en-us/typography/opentype/spec/cpal#palette-entries-and-color-records) record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct ColorRecord {
Expand Down
2 changes: 1 addition & 1 deletion read-fonts/generated/generated_fvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl<'a> std::fmt::Debug for AxisInstanceArrays<'a> {
}

/// The [VariationAxisRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#variationaxisrecord)
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct VariationAxisRecord {
Expand Down
4 changes: 2 additions & 2 deletions read-fonts/generated/generated_gpos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ impl<'a> std::fmt::Debug for MarkArray<'a> {
}

/// Part of [MarkArray]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct MarkRecord {
Expand Down Expand Up @@ -2331,7 +2331,7 @@ impl<'a> std::fmt::Debug for CursivePosFormat1<'a> {
}

/// Part of [CursivePosFormat1]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct EntryExitRecord {
Expand Down
2 changes: 1 addition & 1 deletion read-fonts/generated/generated_hmtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'a> std::fmt::Debug for Hmtx<'a> {
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct LongMetric {
Expand Down
16 changes: 8 additions & 8 deletions read-fonts/generated/generated_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<'a> std::fmt::Debug for ScriptList<'a> {
}

/// [Script Record](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#script-list-table-and-script-record)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct ScriptRecord {
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<'a> std::fmt::Debug for Script<'a> {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct LangSysRecord {
Expand Down Expand Up @@ -467,7 +467,7 @@ impl<'a> std::fmt::Debug for FeatureList<'a> {
}

/// Part of [FeatureList]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct FeatureRecord {
Expand Down Expand Up @@ -1119,7 +1119,7 @@ impl<'a> std::fmt::Debug for CoverageFormat2<'a> {
}

/// Used in [CoverageFormat2]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct RangeRecord {
Expand Down Expand Up @@ -1422,7 +1422,7 @@ impl<'a> std::fmt::Debug for ClassDefFormat2<'a> {
}

/// Used in [ClassDefFormat2]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct ClassRangeRecord {
Expand Down Expand Up @@ -1534,7 +1534,7 @@ impl<'a> SomeTable<'a> for ClassDef<'a> {
}

/// [Sequence Lookup Record](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#sequence-lookup-record)
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct SequenceLookupRecord {
Expand Down Expand Up @@ -3979,7 +3979,7 @@ impl<'a> std::fmt::Debug for FeatureVariations<'a> {
}

/// Part of [FeatureVariations]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct FeatureVariationRecord {
Expand Down Expand Up @@ -4342,7 +4342,7 @@ impl<'a> std::fmt::Debug for FeatureTableSubstitution<'a> {
}

/// Used in [FeatureTableSubstitution]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct FeatureTableSubstitutionRecord {
Expand Down
2 changes: 1 addition & 1 deletion read-fonts/generated/generated_mvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<'a> std::fmt::Debug for Mvar<'a> {
}

/// [ValueRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/mvar#table-formats) metrics variation record
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
#[repr(packed)]
pub struct ValueRecord {
Expand Down
4 changes: 2 additions & 2 deletions read-fonts/generated/generated_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<'a> std::fmt::Debug for Name<'a> {
}

/// Part of [Name]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct LangTagRecord {
Expand Down Expand Up @@ -218,7 +218,7 @@ impl<'a> SomeRecord<'a> for LangTagRecord {
}

///[Name Records](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Copy)]
#[repr(C)]
#[repr(packed)]
pub struct NameRecord {
Expand Down
Loading

0 comments on commit 95ac9bf

Please sign in to comment.