Skip to content

Commit 9842c31

Browse files
authored
Support repr(u128) and repr(i128) in derive(IntoBytes) (#2676)
Fixes #2663
1 parent 4dd201f commit 9842c31

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

zerocopy-derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ fn enum_size_from_repr(repr: &EnumRepr) -> Result<usize, Error> {
12291229
match repr {
12301230
Transparent(span)
12311231
| Compound(
1232-
Spanned { t: C | Rust | Primitive(U32 | I32 | U64 | I64 | Usize | Isize), span },
1232+
Spanned { t: C | Rust | Primitive(U32 | I32 | U64 | I64 | U128 | I128 | Usize | Isize), span },
12331233
_,
12341234
) => Err(Error::new(*span, "`FromBytes` only supported on enums with `#[repr(...)]` attributes `u8`, `i8`, `u16`, or `i16`")),
12351235
Compound(Spanned { t: Primitive(U8 | I8), span: _ }, _align) => Ok(8),

zerocopy-derive/src/output_tests.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ fn test_from_bytes_union() {
447447
}
448448

449449
#[test]
450-
fn test_into_bytes() {
450+
fn test_into_bytes_struct() {
451451
test! {
452452
IntoBytes {
453453
#[repr(C)]
@@ -486,6 +486,44 @@ fn test_into_bytes() {
486486
}
487487
}
488488

489+
#[test]
490+
fn test_into_bytes_enum() {
491+
macro_rules! test_repr {
492+
($(#[$attr:meta])*) => {
493+
$(test! {
494+
IntoBytes {
495+
#[$attr]
496+
enum Foo {
497+
Bar,
498+
}
499+
} expands to {
500+
#[allow(deprecated)]
501+
#[automatically_derived]
502+
unsafe impl ::zerocopy::IntoBytes for Foo {
503+
fn only_derive_is_allowed_to_implement_this_trait() {}
504+
}
505+
} no_build
506+
})*
507+
};
508+
}
509+
510+
test_repr! {
511+
#[repr(C)]
512+
#[repr(u8)]
513+
#[repr(u16)]
514+
#[repr(u32)]
515+
#[repr(u64)]
516+
#[repr(u128)]
517+
#[repr(usize)]
518+
#[repr(i8)]
519+
#[repr(i16)]
520+
#[repr(i32)]
521+
#[repr(i64)]
522+
#[repr(i128)]
523+
#[repr(isize)]
524+
}
525+
}
526+
489527
#[test]
490528
fn test_unaligned() {
491529
test! {

zerocopy-derive/src/repr.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ pub(crate) enum PrimitiveRepr {
4848
U16,
4949
U32,
5050
U64,
51+
U128,
5152
Usize,
5253
I8,
5354
I16,
5455
I32,
5556
I64,
57+
I128,
5658
Isize,
5759
}
5860

@@ -92,11 +94,13 @@ impl<Prim, Packed> Repr<Prim, Packed> {
9294
U16 => "repr(u16)",
9395
U32 => "repr(u32)",
9496
U64 => "repr(u64)",
97+
U128 => "repr(u128)",
9598
Usize => "repr(usize)",
9699
I8 => "repr(i8)",
97100
I16 => "repr(i16)",
98101
I32 => "repr(i32)",
99102
I64 => "repr(i64)",
103+
I128 => "repr(i128)",
100104
Isize => "repr(isize)",
101105
}),
102106
},
@@ -224,11 +228,13 @@ impl ToTokens for Spanned<PrimitiveRepr> {
224228
U16 => ts.append_all(quote_spanned! { self.span => #[repr(u16)] }),
225229
U32 => ts.append_all(quote_spanned! { self.span => #[repr(u32)] }),
226230
U64 => ts.append_all(quote_spanned! { self.span => #[repr(u64)] }),
231+
U128 => ts.append_all(quote_spanned! { self.span => #[repr(u128)] }),
227232
Usize => ts.append_all(quote_spanned! { self.span => #[repr(usize)] }),
228233
I8 => ts.append_all(quote_spanned! { self.span => #[repr(i8)] }),
229234
I16 => ts.append_all(quote_spanned! { self.span => #[repr(i16)] }),
230235
I32 => ts.append_all(quote_spanned! { self.span => #[repr(i32)] }),
231236
I64 => ts.append_all(quote_spanned! { self.span => #[repr(i64)] }),
237+
I128 => ts.append_all(quote_spanned! { self.span => #[repr(i128)] }),
232238
Isize => ts.append_all(quote_spanned! { self.span => #[repr(isize)] }),
233239
}
234240
}
@@ -267,11 +273,13 @@ pub(crate) enum RawRepr {
267273
U16,
268274
U32,
269275
U64,
276+
U128,
270277
Usize,
271278
I8,
272279
I16,
273280
I32,
274281
I64,
282+
I128,
275283
Isize,
276284
Align(NonZeroU32),
277285
PackedN(NonZeroU32),
@@ -302,18 +310,20 @@ impl<Prim: With<PrimitiveRepr>> TryFrom<RawRepr> for CompoundRepr<Prim> {
302310
match raw {
303311
C => Ok(CompoundRepr::C),
304312
Rust => Ok(CompoundRepr::Rust),
305-
raw @ (U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize) => {
313+
raw @ (U8 | U16 | U32 | U64 | U128 | Usize | I8 | I16 | I32 | I64 | I128 | Isize) => {
306314
Prim::try_with_or(
307315
|| match raw {
308316
U8 => Ok(PrimitiveRepr::U8),
309317
U16 => Ok(PrimitiveRepr::U16),
310318
U32 => Ok(PrimitiveRepr::U32),
311319
U64 => Ok(PrimitiveRepr::U64),
320+
U128 => Ok(PrimitiveRepr::U128),
312321
Usize => Ok(PrimitiveRepr::Usize),
313322
I8 => Ok(PrimitiveRepr::I8),
314323
I16 => Ok(PrimitiveRepr::I16),
315324
I32 => Ok(PrimitiveRepr::I32),
316325
I64 => Ok(PrimitiveRepr::I64),
326+
I128 => Ok(PrimitiveRepr::I128),
317327
Isize => Ok(PrimitiveRepr::Isize),
318328
Transparent | C | Rust | Align(_) | PackedN(_) | Packed => {
319329
Err(UnsupportedReprError)
@@ -338,16 +348,16 @@ impl<Pcked: With<NonZeroU32>> TryFrom<RawRepr> for AlignRepr<Pcked> {
338348
|| match raw {
339349
Packed => Ok(NonZeroU32::new(1).unwrap()),
340350
PackedN(n) => Ok(n),
341-
U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize | Transparent
342-
| C | Rust | Align(_) => Err(UnsupportedReprError),
351+
U8 | U16 | U32 | U64 | U128 | Usize | I8 | I16 | I32 | I64 | I128 | Isize
352+
| Transparent | C | Rust | Align(_) => Err(UnsupportedReprError),
343353
},
344354
UnsupportedReprError,
345355
)
346356
.map(AlignRepr::Packed)
347357
.map_err(FromRawReprError::Err),
348358
Align(n) => Ok(AlignRepr::Align(n)),
349-
U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize | Transparent | C
350-
| Rust => Err(FromRawReprError::None),
359+
U8 | U16 | U32 | U64 | U128 | Usize | I8 | I16 | I32 | I64 | I128 | Isize
360+
| Transparent | C | Rust => Err(FromRawReprError::None),
351361
}
352362
}
353363
}
@@ -571,11 +581,13 @@ impl RawRepr {
571581
("u16", None) => U16,
572582
("u32", None) => U32,
573583
("u64", None) => U64,
584+
("u128", None) => U128,
574585
("usize", None) => Usize,
575586
("i8", None) => I8,
576587
("i16", None) => I16,
577588
("i32", None) => I32,
578589
("i64", None) => I64,
590+
("i128", None) => I128,
579591
("isize", None) => Isize,
580592
("C", None) => C,
581593
("transparent", None) => Transparent,

0 commit comments

Comments
 (0)