Skip to content

Commit

Permalink
Add support for data-carrying enums
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Jul 22, 2024
1 parent 38bebd8 commit 36912cc
Show file tree
Hide file tree
Showing 14 changed files with 2,138 additions and 435 deletions.
16 changes: 8 additions & 8 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"rust-analyzer.cargo.cfgs": {
// This adds an extra cfg to rust-analyzer so we can prevent it from
// analyzing tests with a lot of generated code. These can cause it to
// hang or run slowly in general, which is bad for workflows.
"rust_analyzer": null,
}
}
{
"rust-analyzer.cargo.cfgs": {
// This adds an extra cfg to rust-analyzer so we can prevent it from
// analyzing tests with a lot of generated code. These can cause it to
// hang or run slowly in general, which is bad for workflows.
"rust_analyzer": null,
}
}
16 changes: 12 additions & 4 deletions src/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ macro_rules! align_of {
#[doc(hidden)] // `#[macro_export]` bypasses this module's `#[doc(hidden)]`.
#[macro_export]
macro_rules! struct_has_padding {
($t:ty, $($ts:ty),*) => {
($t:ty, [$($ts:ty),*]) => {
::zerocopy::macro_util::core_reexport::mem::size_of::<$t>() > 0 $(+ ::zerocopy::macro_util::core_reexport::mem::size_of::<$ts>())*
};
}
Expand All @@ -294,11 +294,19 @@ macro_rules! struct_has_padding {
#[doc(hidden)] // `#[macro_export]` bypasses this module's `#[doc(hidden)]`.
#[macro_export]
macro_rules! union_has_padding {
($t:ty, $($ts:ty),*) => {
($t:ty, [$($ts:ty),*]) => {
false $(|| ::zerocopy::macro_util::core_reexport::mem::size_of::<$t>() != ::zerocopy::macro_util::core_reexport::mem::size_of::<$ts>())*
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! enum_has_padding {
($t:ty, $disc:ty, $([$($ts:ty),*]),*) => {
false $(|| ::zerocopy::macro_util::core_reexport::mem::size_of::<$t>() != (::zerocopy::macro_util::core_reexport::mem::size_of::<$disc>() $(+ ::zerocopy::macro_util::core_reexport::mem::size_of::<$ts>())*))*
}
}

/// Does `t` have alignment greater than or equal to `u`? If not, this macro
/// produces a compile error. It must be invoked in a dead codepath. This is
/// used in `transmute_ref!` and `transmute_mut!`.
Expand Down Expand Up @@ -728,7 +736,7 @@ mod tests {
(#[$cfg:meta] ($($ts:ty),*) => $expect:expr) => {{
#[$cfg]
struct Test($(#[allow(dead_code)] $ts),*);
assert_eq!(struct_has_padding!(Test, $($ts),*), $expect);
assert_eq!(struct_has_padding!(Test, [$($ts),*]), $expect);
}};
(#[$cfg:meta] $(#[$cfgs:meta])* ($($ts:ty),*) => $expect:expr) => {
test!(#[$cfg] ($($ts),*) => $expect);
Expand Down Expand Up @@ -759,7 +767,7 @@ mod tests {
#[$cfg]
#[allow(unused)] // fields are never read
union Test{ $($fs: $ts),* }
assert_eq!(union_has_padding!(Test, $($ts),*), $expect);
assert_eq!(union_has_padding!(Test, [$($ts),*]), $expect);
}};
(#[$cfg:meta] $(#[$cfgs:meta])* {$($fs:ident: $ts:ty),*} => $expect:expr) => {
test!(#[$cfg] {$($fs: $ts),*} => $expect);
Expand Down
Loading

0 comments on commit 36912cc

Please sign in to comment.