@@ -178,6 +178,8 @@ fn test_weird_discriminants() {
178
178
) ;
179
179
}
180
180
181
+ // Technically non-portable since this is only `IntoBytes` if the discriminant
182
+ // is an `i32` or `u32`, but we'll cross that bridge when we get to it...
181
183
#[ derive(
182
184
Eq , PartialEq , Debug , imp:: KnownLayout , imp:: Immutable , imp:: TryFromBytes , imp:: IntoBytes ,
183
185
) ]
@@ -205,6 +207,43 @@ fn test_has_fields() {
205
207
) ;
206
208
}
207
209
210
+ #[ derive( Eq , PartialEq , Debug , imp:: KnownLayout , imp:: Immutable , imp:: TryFromBytes ) ]
211
+ #[ repr( C , align( 16 ) ) ]
212
+ enum HasFieldsAligned {
213
+ A ( u32 ) ,
214
+ B { foo : :: core:: num:: NonZeroU32 } ,
215
+ }
216
+
217
+ util_assert_impl_all ! ( HasFieldsAligned : imp:: TryFromBytes ) ;
218
+
219
+ #[ test]
220
+ fn test_has_fields_aligned ( ) {
221
+ const SIZE : usize = :: core:: mem:: size_of :: < HasFieldsAligned > ( ) ;
222
+
223
+ #[ derive( imp:: IntoBytes ) ]
224
+ #[ repr( C ) ]
225
+ struct BytesOfHasFieldsAligned {
226
+ has_fields : HasFields ,
227
+ padding : [ u8 ; 8 ] ,
228
+ }
229
+
230
+ let wrap = |has_fields| BytesOfHasFieldsAligned { has_fields, padding : [ 0 ; 8 ] } ;
231
+
232
+ let bytes: [ u8 ; SIZE ] = :: zerocopy:: transmute!( wrap( HasFields :: A ( 10 ) ) ) ;
233
+ imp:: assert_eq!(
234
+ <HasFieldsAligned as imp:: TryFromBytes >:: try_read_from_bytes( & bytes[ ..] ) ,
235
+ imp:: Ok ( HasFieldsAligned :: A ( 10 ) ) ,
236
+ ) ;
237
+
238
+ let bytes: [ u8 ; SIZE ] = :: zerocopy:: transmute!( wrap( HasFields :: B {
239
+ foo: :: core:: num:: NonZeroU32 :: new( 123456 ) . unwrap( )
240
+ } ) ) ;
241
+ imp:: assert_eq!(
242
+ <HasFieldsAligned as imp:: TryFromBytes >:: try_read_from_bytes( & bytes[ ..] ) ,
243
+ imp:: Ok ( HasFieldsAligned :: B { foo: :: core:: num:: NonZeroU32 :: new( 123456 ) . unwrap( ) } ) ,
244
+ ) ;
245
+ }
246
+
208
247
#[ derive(
209
248
Eq , PartialEq , Debug , imp:: KnownLayout , imp:: Immutable , imp:: TryFromBytes , imp:: IntoBytes ,
210
249
) ]
@@ -233,6 +272,45 @@ fn test_has_fields_primitive() {
233
272
) ;
234
273
}
235
274
275
+ #[ derive( Eq , PartialEq , Debug , imp:: KnownLayout , imp:: Immutable , imp:: TryFromBytes ) ]
276
+ #[ repr( u32 , align( 16 ) ) ]
277
+ enum HasFieldsPrimitiveAligned {
278
+ A ( u32 ) ,
279
+ B { foo : :: core:: num:: NonZeroU32 } ,
280
+ }
281
+
282
+ util_assert_impl_all ! ( HasFieldsPrimitiveAligned : imp:: TryFromBytes ) ;
283
+
284
+ #[ test]
285
+ fn test_has_fields_primitive_aligned ( ) {
286
+ const SIZE : usize = :: core:: mem:: size_of :: < HasFieldsPrimitiveAligned > ( ) ;
287
+
288
+ #[ derive( imp:: IntoBytes ) ]
289
+ #[ repr( C ) ]
290
+ struct BytesOfHasFieldsPrimitiveAligned {
291
+ has_fields : HasFieldsPrimitive ,
292
+ padding : [ u8 ; 8 ] ,
293
+ }
294
+
295
+ let wrap = |has_fields| BytesOfHasFieldsPrimitiveAligned { has_fields, padding : [ 0 ; 8 ] } ;
296
+
297
+ let bytes: [ u8 ; SIZE ] = :: zerocopy:: transmute!( wrap( HasFieldsPrimitive :: A ( 10 ) ) ) ;
298
+ imp:: assert_eq!(
299
+ <HasFieldsPrimitiveAligned as imp:: TryFromBytes >:: try_read_from_bytes( & bytes[ ..] ) ,
300
+ imp:: Ok ( HasFieldsPrimitiveAligned :: A ( 10 ) ) ,
301
+ ) ;
302
+
303
+ let bytes: [ u8 ; SIZE ] = :: zerocopy:: transmute!( wrap( HasFieldsPrimitive :: B {
304
+ foo: :: core:: num:: NonZeroU32 :: new( 123456 ) . unwrap( )
305
+ } ) ) ;
306
+ imp:: assert_eq!(
307
+ <HasFieldsPrimitiveAligned as imp:: TryFromBytes >:: try_read_from_bytes( & bytes[ ..] ) ,
308
+ imp:: Ok ( HasFieldsPrimitiveAligned :: B {
309
+ foo: :: core:: num:: NonZeroU32 :: new( 123456 ) . unwrap( )
310
+ } ) ,
311
+ ) ;
312
+ }
313
+
236
314
#[ derive( imp:: TryFromBytes ) ]
237
315
#[ repr( align( 4 ) , u32 ) ]
238
316
enum HasReprAlignFirst {
0 commit comments