Skip to content

Commit 3401924

Browse files
authored
[derive] Fix IntoBytes on repr(C) DST (#2836)
Closes #2835
1 parent 21b7ffc commit 3401924

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

src/util/macro_util.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ macro_rules! repr_c_struct_has_padding {
393393
);
394394
layout.requires_static_padding() || layout.requires_dynamic_padding()
395395
}};
396+
(@field ([$t:ty])) => {
397+
<[$t] as $crate::KnownLayout>::LAYOUT
398+
};
399+
(@field ($t:ty)) => {
400+
$crate::DstLayout::for_unpadded_type::<$t>()
401+
};
396402
(@field [$t:ty]) => {
397403
<[$t] as $crate::KnownLayout>::LAYOUT
398404
};

zerocopy-derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,7 @@ impl<'a, D: DataExt> ImplBlockBuilder<'a, D> {
17771777
.map(|check| {
17781778
let variant_types = variants.iter().map(|var| {
17791779
let types = var.iter().map(|(_vis, _name, ty)| ty);
1780-
quote!([#(#types),*])
1780+
quote!([#((#types)),*])
17811781
});
17821782
let validator_context = check.validator_macro_context();
17831783
let (trt, validator_macro) = check.validator_trait_and_macro_idents();

zerocopy-derive/src/output_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ fn test_into_bytes_struct() {
480480
u8: ::zerocopy::IntoBytes,
481481
(): ::zerocopy::util::macro_util::PaddingFree<
482482
Self,
483-
{ ::zerocopy::struct_padding!(Self, [u8, u8]) },
483+
{ ::zerocopy::struct_padding!(Self, [(u8), (u8)]) },
484484
>,
485485
{
486486
fn only_derive_is_allowed_to_implement_this_trait() {}
@@ -504,7 +504,7 @@ fn test_into_bytes_struct() {
504504
[Trailing]: ::zerocopy::IntoBytes,
505505
(): ::zerocopy::util::macro_util::DynamicPaddingFree<
506506
Self,
507-
{ ::zerocopy::repr_c_struct_has_padding!(Self, [u8, [Trailing]]) },
507+
{ ::zerocopy::repr_c_struct_has_padding!(Self, [(u8), ([Trailing])]) },
508508
>,
509509
{
510510
fn only_derive_is_allowed_to_implement_this_trait() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2025 The Fuchsia Authors
2+
//
3+
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
// This file may not be copied, modified, or distributed except according to
7+
// those terms.
8+
9+
use zerocopy::{IntoBytes, Unalign};
10+
11+
#[allow(unused)]
12+
#[derive(IntoBytes)]
13+
#[repr(C)]
14+
struct Struct {
15+
leading: Unalign<u32>,
16+
trailing: [u8],
17+
}
18+
19+
#[test]
20+
fn test_issue_2835() {
21+
// Compilation is enough to verify the fix
22+
}

0 commit comments

Comments
 (0)