Skip to content

Commit

Permalink
Loosen --cfg zerocopy_derive_union_into_bytes (#1819)
Browse files Browse the repository at this point in the history
Permit it to be passed either when compiling zerocopy-derive or when
compiling the user's crate. This makes the --cfg play more nicely with
different build systems.

Makes progress on #1792
  • Loading branch information
joshlf authored Oct 4, 2024
1 parent d204727 commit 733db7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 42 deletions.
27 changes: 19 additions & 8 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,15 +950,26 @@ fn derive_into_bytes_enum(ast: &DeriveInput, enm: &DataEnum) -> Result<TokenStre
/// - no padding (size of union equals size of each field type)
fn derive_into_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> Result<TokenStream, Error> {
// See #1792 for more context.
let cfg_compile_error = quote!(
const _: () = {
#[cfg(not(zerocopy_derive_union_into_bytes))]
::zerocopy::util::macro_util::core_reexport::compile_error!(
"requires --cfg zerocopy_derive_union_into_bytes;
//
// By checking for `zerocopy_derive_union_into_bytes` both here and in the
// generated code, we ensure that `--cfg zerocopy_derive_union_into_bytes`
// need only be passed *either* when compiling this crate *or* when
// compiling the user's crate. The former is preferable, but in some
// situations (such as when cross-compiling using `cargo build --target`),
// it doesn't get propagated to this crate's build by default.
let cfg_compile_error = if cfg!(zerocopy_derive_union_into_bytes) {
quote!()
} else {
quote!(
const _: () = {
#[cfg(not(zerocopy_derive_union_into_bytes))]
::zerocopy::util::macro_util::core_reexport::compile_error!(
"requires --cfg zerocopy_derive_union_into_bytes;
please let us know you use this feature: https://github.com/google/zerocopy/discussions/1802"
);
};
);
);
};
)
};

// TODO(#10): Support type parameters.
if !ast.generics.params.is_empty() {
Expand Down
34 changes: 0 additions & 34 deletions zerocopy-derive/src/output_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,40 +332,6 @@ fn test_into_bytes() {
}
}

#[test]
fn test_union_into_bytes() {
// Rustfmt spuriously adds spaces after the newline in the middle of the
// string literal.
#[rustfmt::skip]
test! {
IntoBytes {
#[repr(C)]
union Foo {
a: u8,
}
} expands to {
const _: () = {
#[cfg(not(zerocopy_derive_union_into_bytes))]
::zerocopy::util::macro_util::core_reexport::compile_error!(
"requires --cfg zerocopy_derive_union_into_bytes;
please let us know you use this feature: https://github.com/google/zerocopy/discussions/1802"
);
};
#[allow(deprecated)]
unsafe impl ::zerocopy::IntoBytes for Foo
where
u8: ::zerocopy::IntoBytes,
(): ::zerocopy::util::macro_util::PaddingFree<
Foo,
{ ::zerocopy::union_has_padding!(Foo, [u8]) },
>,
{
fn only_derive_is_allowed_to_implement_this_trait() {}
}
} no_build
}
}

#[test]
fn test_unaligned() {
test! {
Expand Down

0 comments on commit 733db7f

Please sign in to comment.