Skip to content

Commit

Permalink
packed -> C,packed
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jun 13, 2024
1 parent 275f3f7 commit b163822
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion components/calendar/src/provider/chinese_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'data> ChineseBasedCacheV1<'data> {
derive(databake::Bake),
databake(path = icu_calendar::provider),
)]
#[repr(packed)]
#[repr(C, packed)]
pub struct PackedChineseBasedYearInfo(pub u8, pub u8, pub u8);

impl PackedChineseBasedYearInfo {
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/provider/islamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'data> IslamicCacheV1<'data> {
databake(path = icu_calendar::provider),
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[repr(packed)]
#[repr(C, packed)]
pub struct PackedIslamicYearInfo(pub u8, pub u8);

impl fmt::Debug for PackedIslamicYearInfo {
Expand Down
2 changes: 1 addition & 1 deletion components/casemap/src/provider/exceptions_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl ExceptionHeader {
/// In this struct the RESERVED bit is still allowed to be set, and it will produce a different
/// exception header, but it will not have any other effects.
#[derive(Copy, Clone, PartialEq, Eq, ULE)]
#[repr(packed)]
#[repr(C, packed)]
pub struct ExceptionHeaderULE {
slot_presence: SlotPresence,
bits: ExceptionBitsULE,
Expand Down
4 changes: 2 additions & 2 deletions utils/zerovec/derive/src/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use syn::spanned::Spanned;
use syn::{Data, DeriveInput, Error};

pub fn derive_impl(input: &DeriveInput) -> TokenStream2 {
if !utils::has_valid_repr(&input.attrs, |r| r == "packed" || r == "transparent") {
if !utils::has_transparent_or_cpacked_repr(&input.attrs) {
return Error::new(
input.span(),
"derive(ULE) must be applied to a #[repr(packed)] or #[repr(transparent)] type",
"derive(ULE) must be applied to a #[repr(C, packed)] or #[repr(transparent)] type",
)
.to_compile_error();
}
Expand Down
22 changes: 21 additions & 1 deletion utils/zerovec/derive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ pub fn has_valid_repr(attrs: &[Attribute], predicate: impl Fn(&Ident) -> bool +
})
}

pub fn has_transparent_or_cpacked_repr(attrs: &[Attribute]) -> bool {
let mut found_transparent = false;
let mut found_packed = false;
let mut found_c = false;
for attr in attrs.iter().filter(|a| a.path().is_ident("repr")) {
if let Some(pieces) = attr.parse_args::<IdentListAttribute>().ok() {
for piece in pieces.idents.iter() {
if piece == "C" || piece == "c" {
found_c = true;
} else if piece == "transparent" {
found_transparent = true;
} else if piece == "packed" {
found_packed = true;
}
}
}
}
(found_c && found_packed) || found_transparent
}

// An attribute that is a list of idents
struct IdentListAttribute {
idents: Punctuated<Ident, Token![,]>,
Expand Down Expand Up @@ -60,7 +80,7 @@ pub fn repr_for(f: &Fields) -> TokenStream2 {
if f.len() == 1 {
quote!(transparent)
} else {
quote!(packed)
quote!(C, packed)
}
}

Expand Down
4 changes: 2 additions & 2 deletions utils/zerovec/derive/src/varule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub fn derive_impl(
input: &DeriveInput,
custom_varule_validator: Option<TokenStream2>,
) -> TokenStream2 {
if !utils::has_valid_repr(&input.attrs, |r| r == "packed" || r == "transparent") {
if !utils::has_transparent_or_cpacked_repr(&input.attrs) {
return Error::new(
input.span(),
"derive(VarULE) must be applied to a #[repr(packed)] or #[repr(transparent)] type",
"derive(VarULE) must be applied to a #[repr(C, packed)] or #[repr(transparent)] type",
)
.to_compile_error();
}
Expand Down

0 comments on commit b163822

Please sign in to comment.