Debian Codegraph @

.cargo/vendor/zerocopy-derive/src/output_tests.rs

raw
1 // Copyright 2024 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 dissimilar::Chunk;
10 use proc_macro2::TokenStream;
11
12 use crate::IntoTokenStream;
13
14 macro_rules! use_as_trait_name {
15 ($($alias:ident => $derive:ident),* $(,)?) => {
16 $(use super::$derive as $alias;)*
17 };
18 }
19
20 // This permits invocations of `test!` to be more ergonomic, passing the name of
21 // the trait under test rather than the name of the inner derive function.
22 use_as_trait_name!(
23 KnownLayout => derive_known_layout_inner,
24 Immutable => derive_no_cell_inner,
25 TryFromBytes => derive_try_from_bytes_inner,
26 FromZeros => derive_from_zeros_inner,
27 FromBytes => derive_from_bytes_inner,
28 IntoBytes => derive_into_bytes_inner,
29 Unaligned => derive_unaligned_inner,
30 ByteHash => derive_hash_inner,
31 ByteEq => derive_eq_inner,
32 SplitAt => derive_split_at_inner,
33 );
34
35 /// Test that the given derive input expands to the expected output.
36 ///
37 /// Equality is tested by formatting both token streams using `prettyplease` and
38 /// performing string equality on the results. This has the effect of making the
39 /// tests less brittle and robust against meaningless formatting changes.
40 // Adapted from https://github.com/joshlf/synstructure/blob/400499aaf54840056ff56718beb7810540e6be59/src/macros.rs#L212-L317
41 macro_rules! test {
42 ($name:ident { $($i:tt)* } expands to { $($o:tt)* }) => {
43 {
44 #[allow(dead_code)]
45 fn ensure_compiles() {
46 $($i)*
47 $($o)*
48 }
49
50 test!($name { $($i)* } expands to { $($o)* } no_build);
51 }
52 };
53
54 ($name:ident { $($i:tt)* } expands to { $($o:tt)* } no_build) => {
55 {
56 let ts: proc_macro2::TokenStream = quote::quote!( $($i)* );
57 let ast = syn::parse2::<syn::DeriveInput>(ts).unwrap();
58 let res = $name(&ast, crate::Trait::$name, &syn::parse_quote!(::zerocopy));
59 let expected_toks = quote::quote!( $($o)* );
60 assert_eq_streams(expected_toks.into(), res.into_ts().into());
61 }
62 };
63 }
64
65 #[track_caller]
66 fn assert_eq_streams(expect: TokenStream, res: TokenStream) {
67 let pretty =
68 |ts: TokenStream| prettyplease::unparse(&syn::parse_file(&ts.to_string()).unwrap());
69
70 let expect = pretty(expect.clone());
71 let res = pretty(res.clone());
72 if expect != res {
73 let diff = dissimilar::diff(&expect, &res)
74 .into_iter()
75 .flat_map(|chunk| {
76 let (prefix, chunk) = match chunk {
77 Chunk::Equal(chunk) => (" ", chunk),
78 Chunk::Delete(chunk) => ("-", chunk),
79 Chunk::Insert(chunk) => ("+", chunk),
80 };
81 [prefix, chunk, "\n"]
82 })
83 .collect::<String>();
84
85 panic!(
86 "\
87 test failed:
88 got:
89 ```
90 {}
91 ```
92
93 diff (expected vs got):
94 ```
95 {}
96 ```\n",
97 res, diff
98 );
99 }
100 }
101
102 #[test]
103 fn test_known_layout() {
104 test! {
105 KnownLayout {
106 struct Foo;
107 } expands to {
108 #[allow(deprecated)]
109 #[automatically_derived]
110 unsafe impl ::zerocopy::KnownLayout for Foo
111 where
112 Self: ::zerocopy::util::macro_util::core_reexport::marker::Sized,
113 {
114 fn only_derive_is_allowed_to_implement_this_trait() {}
115
116 type PointerMetadata = ();
117
118 type MaybeUninit = ::zerocopy::util::macro_util::core_reexport::mem::MaybeUninit<Self>;
119
120 const LAYOUT: ::zerocopy::DstLayout = ::zerocopy::DstLayout::for_type::<Self>();
121
122 #[inline(always)]
123 fn raw_from_ptr_len(
124 bytes: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<u8>,
125 _meta: (),
126 ) -> ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self> {
127 bytes.cast::<Self>()
128 }
129
130 #[inline(always)]
131 fn pointer_to_metadata(_ptr: *mut Self) -> () {}
132 }
133 } no_build
134 }
135
136 test! {
137 KnownLayout {
138 #[repr(C, align(2))]
139 struct Foo<T, U>(T, U);
140 }
141 expands to {
142 const _: () = {
143 #[allow(deprecated)]
144 #[automatically_derived]
145 unsafe impl<T, U> ::zerocopy::KnownLayout for Foo<T, U>
146 where
147 U: ::zerocopy::KnownLayout,
148 {
149 fn only_derive_is_allowed_to_implement_this_trait() {}
150 type PointerMetadata = <U as ::zerocopy::KnownLayout>::PointerMetadata;
151 type MaybeUninit = __ZerocopyKnownLayoutMaybeUninit<T, U>;
152 const LAYOUT: ::zerocopy::DstLayout = {
153 use ::zerocopy::util::macro_util::core_reexport::num::NonZeroUsize;
154 use ::zerocopy::{DstLayout, KnownLayout};
155 DstLayout::for_repr_c_struct(
156 ::zerocopy::util::macro_util::core_reexport::num::NonZeroUsize::new(
157 2u32 as usize,
158 ),
159 ::zerocopy::util::macro_util::core_reexport::option::Option::None,
160 &[DstLayout::for_type::<T>(), <U as KnownLayout>::LAYOUT],
161 )
162 };
163 #[inline(always)]
164 fn raw_from_ptr_len(
165 bytes: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<u8>,
166 meta: Self::PointerMetadata,
167 ) -> ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self> {
168 use ::zerocopy::KnownLayout;
169 let trailing = <U as KnownLayout>::raw_from_ptr_len(bytes, meta);
170 let slf = trailing.as_ptr() as *mut Self;
171 unsafe {
172 ::zerocopy::util::macro_util::core_reexport::ptr::NonNull::new_unchecked(
173 slf,
174 )
175 }
176 }
177 #[inline(always)]
178 fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
179 <U>::pointer_to_metadata(ptr as *mut _)
180 }
181 }
182 #[allow(non_camel_case_types)]
183 struct __Zerocopy_Field_0;
184 #[allow(non_camel_case_types)]
185 struct __Zerocopy_Field_1;
186 unsafe impl<T, U> ::zerocopy::util::macro_util::Field<__Zerocopy_Field_0>
187 for Foo<T, U> {
188 type Type = T;
189 }
190 unsafe impl<T, U> ::zerocopy::util::macro_util::Field<__Zerocopy_Field_1>
191 for Foo<T, U> {
192 type Type = U;
193 }
194 #[repr(C)]
195 #[repr(align(2))]
196 #[doc(hidden)]
197 #[allow(private_bounds)]
198 struct __ZerocopyKnownLayoutMaybeUninit<T, U>(
199 ::zerocopy::util::macro_util::core_reexport::mem::MaybeUninit<
200 <Foo<T, U> as ::zerocopy::util::macro_util::Field<__Zerocopy_Field_0>>::Type,
201 >,
202 ::zerocopy::util::macro_util::core_reexport::mem::ManuallyDrop<
203 <<Foo<
204 T,
205 U,
206 > as ::zerocopy::util::macro_util::Field<
207 __Zerocopy_Field_1,
208 >>::Type as ::zerocopy::KnownLayout>::MaybeUninit,
209 >,
210 )
211 where
212 <Foo<
213 T,
214 U,
215 > as ::zerocopy::util::macro_util::Field<
216 __Zerocopy_Field_1,
217 >>::Type: ::zerocopy::KnownLayout;
218 unsafe impl<T, U> ::zerocopy::KnownLayout for __ZerocopyKnownLayoutMaybeUninit<T, U>
219 where
220 <Foo<
221 T,
222 U,
223 > as ::zerocopy::util::macro_util::Field<
224 __Zerocopy_Field_1,
225 >>::Type: ::zerocopy::KnownLayout,
226 {
227 #[allow(clippy::missing_inline_in_public_items)]
228 fn only_derive_is_allowed_to_implement_this_trait() {}
229 type PointerMetadata = <Foo<T, U> as ::zerocopy::KnownLayout>::PointerMetadata;
230 type MaybeUninit = Self;
231 const LAYOUT: ::zerocopy::DstLayout = <Foo<
232 T,
233 U,
234 > as ::zerocopy::KnownLayout>::LAYOUT;
235 #[inline(always)]
236 fn raw_from_ptr_len(
237 bytes: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<u8>,
238 meta: Self::PointerMetadata,
239 ) -> ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self> {
240 use ::zerocopy::KnownLayout;
241 let trailing = <<<Foo<
242 T,
243 U,
244 > as ::zerocopy::util::macro_util::Field<
245 __Zerocopy_Field_1,
246 >>::Type as ::zerocopy::KnownLayout>::MaybeUninit as KnownLayout>::raw_from_ptr_len(
247 bytes,
248 meta,
249 );
250 let slf = trailing.as_ptr() as *mut Self;
251 unsafe {
252 ::zerocopy::util::macro_util::core_reexport::ptr::NonNull::new_unchecked(
253 slf,
254 )
255 }
256 }
257 #[inline(always)]
258 fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
259 <<<Foo<
260 T,
261 U,
262 > as ::zerocopy::util::macro_util::Field<
263 __Zerocopy_Field_1,
264 >>::Type as ::zerocopy::KnownLayout>::MaybeUninit>::pointer_to_metadata(
265 ptr as *mut _,
266 )
267 }
268 }
269 };
270 } no_build
271 }
272 }
273
274 #[test]
275 fn test_immutable() {
276 test! {
277 Immutable {
278 struct Foo;
279 } expands to {
280 #[allow(deprecated)]
281 #[automatically_derived]
282 unsafe impl ::zerocopy::Immutable for Foo {
283 fn only_derive_is_allowed_to_implement_this_trait() {}
284 }
285 } no_build
286 }
287 }
288
289 #[test]
290 fn test_try_from_bytes() {
291 test! {
292 TryFromBytes {
293 struct Foo;
294 } expands to {
295 #[allow(deprecated)]
296 #[automatically_derived]
297 unsafe impl ::zerocopy::TryFromBytes for Foo {
298 fn only_derive_is_allowed_to_implement_this_trait() {}
299
300 fn is_bit_valid<___ZerocopyAliasing>(
301 mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
302 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
303 where
304 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
305 {
306 use ::zerocopy::util::macro_util::core_reexport;
307 use ::zerocopy::pointer::PtrInner;
308 true
309 }
310 }
311 } no_build
312 }
313 }
314
315 #[test]
316 fn test_from_zeros() {
317 test! {
318 FromZeros {
319 struct Foo;
320 } expands to {
321 #[allow(deprecated)]
322 #[automatically_derived]
323 unsafe impl ::zerocopy::TryFromBytes for Foo {
324 fn only_derive_is_allowed_to_implement_this_trait() {}
325
326 fn is_bit_valid<___ZerocopyAliasing>(
327 mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
328 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
329 where
330 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
331 {
332 use ::zerocopy::util::macro_util::core_reexport;
333 use ::zerocopy::pointer::PtrInner;
334 true
335 }
336 }
337
338 #[allow(deprecated)]
339 #[automatically_derived]
340 unsafe impl ::zerocopy::FromZeros for Foo {
341 fn only_derive_is_allowed_to_implement_this_trait() {}
342 }
343 } no_build
344 }
345 }
346
347 #[test]
348 fn test_from_bytes_struct() {
349 test! {
350 FromBytes {
351 struct Foo;
352 } expands to {
353 #[allow(deprecated)]
354 #[automatically_derived]
355 unsafe impl ::zerocopy::TryFromBytes for Foo {
356 fn only_derive_is_allowed_to_implement_this_trait() {}
357
358 fn is_bit_valid<___ZerocopyAliasing>(
359 _candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
360 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
361 where
362 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
363 {
364 if false {
365 fn assert_is_from_bytes<T>()
366 where
367 T: ::zerocopy::FromBytes,
368 T: ?::zerocopy::util::macro_util::core_reexport::marker::Sized,
369 {}
370 assert_is_from_bytes::<Self>();
371 }
372
373 true
374 }
375 }
376
377 #[allow(deprecated)]
378 #[automatically_derived]
379 unsafe impl ::zerocopy::FromZeros for Foo {
380 fn only_derive_is_allowed_to_implement_this_trait() {}
381 }
382
383 #[allow(deprecated)]
384 #[automatically_derived]
385 unsafe impl ::zerocopy::FromBytes for Foo {
386 fn only_derive_is_allowed_to_implement_this_trait() {}
387 }
388 } no_build
389 }
390 }
391
392 #[test]
393 fn test_from_bytes_union() {
394 test! {
395 FromBytes {
396 union Foo {
397 a: u8,
398 }
399 } expands to {
400 #[allow(deprecated)]
401 #[automatically_derived]
402 unsafe impl ::zerocopy::TryFromBytes for Foo
403 where
404 u8: ::zerocopy::TryFromBytes + ::zerocopy::Immutable,
405 {
406 fn only_derive_is_allowed_to_implement_this_trait() {}
407
408 fn is_bit_valid<___ZerocopyAliasing>(
409 _candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
410 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
411 where
412 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
413 {
414 if false {
415 fn assert_is_from_bytes<T>()
416 where
417 T: ::zerocopy::FromBytes,
418 T: ?::zerocopy::util::macro_util::core_reexport::marker::Sized,
419 {}
420 assert_is_from_bytes::<Self>();
421 }
422
423 true
424 }
425 }
426
427 #[allow(deprecated)]
428 #[automatically_derived]
429 unsafe impl ::zerocopy::FromZeros for Foo
430 where
431 u8: ::zerocopy::FromZeros + ::zerocopy::Immutable,
432 {
433 fn only_derive_is_allowed_to_implement_this_trait() {}
434 }
435
436 #[allow(deprecated)]
437 #[automatically_derived]
438 unsafe impl ::zerocopy::FromBytes for Foo
439 where
440 u8: ::zerocopy::FromBytes + ::zerocopy::Immutable,
441 {
442 fn only_derive_is_allowed_to_implement_this_trait() {}
443 }
444 } no_build
445 }
446 }
447
448 #[test]
449 fn test_into_bytes_struct() {
450 test! {
451 IntoBytes {
452 #[repr(C)]
453 struct Foo;
454 } expands to {
455 #[allow(deprecated)]
456 #[automatically_derived]
457 unsafe impl ::zerocopy::IntoBytes for Foo {
458 fn only_derive_is_allowed_to_implement_this_trait() {}
459 }
460 } no_build
461 }
462
463 test! {
464 IntoBytes {
465 #[repr(C)]
466 struct Foo {
467 a: u8,
468 b: u8,
469 }
470 } expands to {
471 #[allow(deprecated)]
472 #[automatically_derived]
473 unsafe impl ::zerocopy::IntoBytes for Foo
474 where
475 u8: ::zerocopy::IntoBytes,
476 u8: ::zerocopy::IntoBytes,
477 (): ::zerocopy::util::macro_util::PaddingFree<
478 Self,
479 { ::zerocopy::struct_padding!(Self, [u8, u8]) },
480 >,
481 {
482 fn only_derive_is_allowed_to_implement_this_trait() {}
483 }
484 } no_build
485 }
486
487 test! {
488 IntoBytes {
489 #[repr(C)]
490 struct Foo {
491 a: u8,
492 b: [Trailing],
493 }
494 } expands to {
495 #[allow(deprecated)]
496 #[automatically_derived]
497 unsafe impl ::zerocopy::IntoBytes for Foo
498 where
499 u8: ::zerocopy::IntoBytes,
500 [Trailing]: ::zerocopy::IntoBytes,
501 (): ::zerocopy::util::macro_util::DynamicPaddingFree<
502 Self,
503 { ::zerocopy::repr_c_struct_has_padding!(Self, [u8, [Trailing]]) },
504 >,
505 {
506 fn only_derive_is_allowed_to_implement_this_trait() {}
507 }
508 } no_build
509 }
510
511 test! {
512 IntoBytes {
513 #[repr(C)]
514 struct Foo<Trailing> {
515 a: u8,
516 b: [Trailing],
517 }
518 } expands to {
519 #[allow(deprecated)]
520 #[automatically_derived]
521 unsafe impl<Trailing> ::zerocopy::IntoBytes for Foo<Trailing>
522 where
523 u8: ::zerocopy::IntoBytes + ::zerocopy::Unaligned,
524 [Trailing]: ::zerocopy::IntoBytes + ::zerocopy::Unaligned,
525 {
526 fn only_derive_is_allowed_to_implement_this_trait() {}
527 }
528 } no_build
529 }
530 }
531
532 #[test]
533 fn test_into_bytes_enum() {
534 macro_rules! test_repr {
535 ($(#[$attr:meta])*) => {
536 $(test! {
537 IntoBytes {
538 #[$attr]
539 enum Foo {
540 Bar,
541 }
542 } expands to {
543 #[allow(deprecated)]
544 #[automatically_derived]
545 unsafe impl ::zerocopy::IntoBytes for Foo {
546 fn only_derive_is_allowed_to_implement_this_trait() {}
547 }
548 } no_build
549 })*
550 };
551 }
552
553 test_repr! {
554 #[repr(C)]
555 #[repr(u8)]
556 #[repr(u16)]
557 #[repr(u32)]
558 #[repr(u64)]
559 #[repr(u128)]
560 #[repr(usize)]
561 #[repr(i8)]
562 #[repr(i16)]
563 #[repr(i32)]
564 #[repr(i64)]
565 #[repr(i128)]
566 #[repr(isize)]
567 }
568 }
569
570 #[test]
571 fn test_unaligned() {
572 test! {
573 Unaligned {
574 #[repr(C)]
575 struct Foo;
576 } expands to {
577 #[allow(deprecated)]
578 #[automatically_derived]
579 unsafe impl ::zerocopy::Unaligned for Foo {
580 fn only_derive_is_allowed_to_implement_this_trait() {}
581 }
582 } no_build
583 }
584 }
585
586 #[test]
587 fn test_try_from_bytes_enum() {
588 test! {
589 TryFromBytes {
590 #[repr(u8)]
591 enum ComplexWithGenerics<'a: 'static, const N: usize, X, Y: Deref>
592 where
593 X: Deref<Target = &'a [(X, Y); N]>,
594 {
595 UnitLike,
596 StructLike { a: u8, b: X, c: X::Target, d: Y::Target, e: [(X, Y); N] },
597 TupleLike(bool, Y, PhantomData<&'a [(X, Y); N]>),
598 }
599 } expands to {
600 #[allow(deprecated)]
601 #[automatically_derived]
602 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
603 for ComplexWithGenerics<'a, { N }, X, Y>
604 where
605 X: Deref<Target = &'a [(X, Y); N]>,
606 u8: ::zerocopy::TryFromBytes,
607 X: ::zerocopy::TryFromBytes,
608 X::Target: ::zerocopy::TryFromBytes,
609 Y::Target: ::zerocopy::TryFromBytes,
610 [(X, Y); N]: ::zerocopy::TryFromBytes,
611 bool: ::zerocopy::TryFromBytes,
612 Y: ::zerocopy::TryFromBytes,
613 PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
614 {
615 fn only_derive_is_allowed_to_implement_this_trait() {}
616 fn is_bit_valid<___ZerocopyAliasing>(
617 mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
618 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
619 where
620 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
621 {
622 use ::zerocopy::util::macro_util::core_reexport;
623
624 #[repr(u8)]
625 #[allow(dead_code, non_camel_case_types)]
626 enum ___ZerocopyTag {
627 UnitLike,
628 StructLike,
629 TupleLike,
630 }
631 type ___ZerocopyTagPrimitive = ::zerocopy::util::macro_util::SizeToTag<
632 { core_reexport::mem::size_of::<___ZerocopyTag>() },
633 >;
634 #[allow(non_upper_case_globals)]
635 const ___ZEROCOPY_TAG_UnitLike: ___ZerocopyTagPrimitive =
636 ___ZerocopyTag::UnitLike as ___ZerocopyTagPrimitive;
637 #[allow(non_upper_case_globals)]
638 const ___ZEROCOPY_TAG_StructLike: ___ZerocopyTagPrimitive =
639 ___ZerocopyTag::StructLike as ___ZerocopyTagPrimitive;
640 #[allow(non_upper_case_globals)]
641 const ___ZEROCOPY_TAG_TupleLike: ___ZerocopyTagPrimitive =
642 ___ZerocopyTag::TupleLike as ___ZerocopyTagPrimitive;
643 type ___ZerocopyOuterTag = ();
644 type ___ZerocopyInnerTag = ___ZerocopyTag;
645 #[repr(C)]
646 #[allow(non_snake_case)]
647 struct ___ZerocopyVariantStruct_StructLike<'a: 'static, const N: usize, X, Y: Deref>(
648 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>,
649 u8,
650 X,
651 X::Target,
652 Y::Target,
653 [(X, Y); N],
654 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>,
655 )
656 where
657 X: Deref<Target = &'a [(X, Y); N]>;
658 #[allow(deprecated)]
659 #[automatically_derived]
660 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
661 for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
662 where
663 X: Deref<Target = &'a [(X, Y); N]>,
664 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>: ::zerocopy::TryFromBytes,
665 u8: ::zerocopy::TryFromBytes,
666 X: ::zerocopy::TryFromBytes,
667 X::Target: ::zerocopy::TryFromBytes,
668 Y::Target: ::zerocopy::TryFromBytes,
669 [(X, Y); N]: ::zerocopy::TryFromBytes,
670 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>:
671 ::zerocopy::TryFromBytes,
672 {
673 fn only_derive_is_allowed_to_implement_this_trait() {}
674 fn is_bit_valid<___ZerocopyAliasing>(
675 mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
676 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
677 where
678 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
679 {
680 use ::zerocopy::util::macro_util::core_reexport;
681 use ::zerocopy::pointer::PtrInner;
682
683 true && {
684 let field_candidate = unsafe {
685 let project = |slf: PtrInner<'_, Self>| {
686 let slf = slf.as_non_null().as_ptr();
687 let field = core_reexport::ptr::addr_of_mut!((*slf).0);
688 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
689 unsafe { PtrInner::new(ptr) }
690 };
691 candidate.reborrow().cast_unsized_unchecked(project)
692 };
693 <core_reexport::mem::MaybeUninit<
694 ___ZerocopyInnerTag,
695 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
696 } && {
697 let field_candidate = unsafe {
698 let project = |slf: PtrInner<'_, Self>| {
699 let slf = slf.as_non_null().as_ptr();
700 let field = core_reexport::ptr::addr_of_mut!((*slf).1);
701 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
702 unsafe { PtrInner::new(ptr) }
703 };
704 candidate.reborrow().cast_unsized_unchecked(project)
705 };
706 <u8 as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
707 } && {
708 let field_candidate = unsafe {
709 let project = |slf: PtrInner<'_, Self>| {
710 let slf = slf.as_non_null().as_ptr();
711 let field = core_reexport::ptr::addr_of_mut!((*slf).2);
712 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
713 unsafe { PtrInner::new(ptr) }
714 };
715 candidate.reborrow().cast_unsized_unchecked(project)
716 };
717 <X as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
718 } && {
719 let field_candidate = unsafe {
720 let project = |slf: PtrInner<'_, Self>| {
721 let slf = slf.as_non_null().as_ptr();
722 let field = core_reexport::ptr::addr_of_mut!((*slf).3);
723 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
724 unsafe { PtrInner::new(ptr) }
725 };
726 candidate.reborrow().cast_unsized_unchecked(project)
727 };
728 <X::Target as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
729 } && {
730 let field_candidate = unsafe {
731 let project = |slf: PtrInner<'_, Self>| {
732 let slf = slf.as_non_null().as_ptr();
733 let field = core_reexport::ptr::addr_of_mut!((*slf).4);
734 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
735 unsafe { PtrInner::new(ptr) }
736 };
737 candidate.reborrow().cast_unsized_unchecked(project)
738 };
739 <Y::Target as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
740 } && {
741 let field_candidate = unsafe {
742 let project = |slf: PtrInner<'_, Self>| {
743 let slf = slf.as_non_null().as_ptr();
744 let field = core_reexport::ptr::addr_of_mut!((*slf).5);
745 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
746 unsafe { PtrInner::new(ptr) }
747 };
748 candidate.reborrow().cast_unsized_unchecked(project)
749 };
750 <[(X, Y); N] as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
751 } && {
752 let field_candidate = unsafe {
753 let project = |slf: PtrInner<'_, Self>| {
754 let slf = slf.as_non_null().as_ptr();
755 let field = core_reexport::ptr::addr_of_mut!((*slf).6);
756 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
757 unsafe { PtrInner::new(ptr) }
758 };
759 candidate.reborrow().cast_unsized_unchecked(project)
760 };
761 <core_reexport::marker::PhantomData<
762 ComplexWithGenerics<'a, N, X, Y>,
763 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
764 }
765 }
766 }
767 #[repr(C)]
768 #[allow(non_snake_case)]
769 struct ___ZerocopyVariantStruct_TupleLike<'a: 'static, const N: usize, X, Y: Deref>(
770 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>,
771 bool,
772 Y,
773 PhantomData<&'a [(X, Y); N]>,
774 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>,
775 )
776 where
777 X: Deref<Target = &'a [(X, Y); N]>;
778 #[allow(deprecated)]
779 #[automatically_derived]
780 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
781 for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
782 where
783 X: Deref<Target = &'a [(X, Y); N]>,
784 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>: ::zerocopy::TryFromBytes,
785 bool: ::zerocopy::TryFromBytes,
786 Y: ::zerocopy::TryFromBytes,
787 PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
788 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>:
789 ::zerocopy::TryFromBytes,
790 {
791 fn only_derive_is_allowed_to_implement_this_trait() {}
792 fn is_bit_valid<___ZerocopyAliasing>(
793 mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
794 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
795 where
796 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
797 {
798 use ::zerocopy::util::macro_util::core_reexport;
799 use ::zerocopy::pointer::PtrInner;
800
801 true && {
802 let field_candidate = unsafe {
803 let project = |slf: PtrInner<'_, Self>| {
804 let slf = slf.as_non_null().as_ptr();
805 let field = core_reexport::ptr::addr_of_mut!((*slf).0);
806 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
807 unsafe { PtrInner::new(ptr) }
808 };
809 candidate.reborrow().cast_unsized_unchecked(project)
810 };
811 <core_reexport::mem::MaybeUninit<
812 ___ZerocopyInnerTag,
813 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
814 } && {
815 let field_candidate = unsafe {
816 let project = |slf: PtrInner<'_, Self>| {
817 let slf = slf.as_non_null().as_ptr();
818 let field = core_reexport::ptr::addr_of_mut!((*slf).1);
819 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
820 unsafe { PtrInner::new(ptr) }
821 };
822 candidate.reborrow().cast_unsized_unchecked(project)
823 };
824 <bool as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
825 } && {
826 let field_candidate = unsafe {
827 let project = |slf: PtrInner<'_, Self>| {
828 let slf = slf.as_non_null().as_ptr();
829 let field = core_reexport::ptr::addr_of_mut!((*slf).2);
830 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
831 unsafe { PtrInner::new(ptr) }
832 };
833 candidate.reborrow().cast_unsized_unchecked(project)
834 };
835 <Y as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
836 } && {
837 let field_candidate = unsafe {
838 let project = |slf: PtrInner<'_, Self>| {
839 let slf = slf.as_non_null().as_ptr();
840 let field = core_reexport::ptr::addr_of_mut!((*slf).3);
841 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
842 unsafe { PtrInner::new(ptr) }
843 };
844 candidate.reborrow().cast_unsized_unchecked(project)
845 };
846 <PhantomData<&'a [(X, Y); N]> as ::zerocopy::TryFromBytes>::is_bit_valid(
847 field_candidate,
848 )
849 } && {
850 let field_candidate = unsafe {
851 let project = |slf: PtrInner<'_, Self>| {
852 let slf = slf.as_non_null().as_ptr();
853 let field = core_reexport::ptr::addr_of_mut!((*slf).4);
854 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
855 unsafe { PtrInner::new(ptr) }
856 };
857 candidate.reborrow().cast_unsized_unchecked(project)
858 };
859 <core_reexport::marker::PhantomData<
860 ComplexWithGenerics<'a, N, X, Y>,
861 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
862 }
863 }
864 }
865 #[repr(C)]
866 #[allow(non_snake_case)]
867 union ___ZerocopyVariants<'a: 'static, const N: usize, X, Y: Deref> {
868 __field_StructLike:
869 core_reexport::mem::ManuallyDrop<___ZerocopyVariantStruct_StructLike<'a, N, X, Y>>,
870 __field_TupleLike:
871 core_reexport::mem::ManuallyDrop<___ZerocopyVariantStruct_TupleLike<'a, N, X, Y>>,
872 __nonempty: (),
873 }
874 #[repr(C)]
875 struct ___ZerocopyRawEnum<'a: 'static, const N: usize, X, Y: Deref> {
876 tag: ___ZerocopyOuterTag,
877 variants: ___ZerocopyVariants<'a, N, X, Y>,
878 }
879 let tag = {
880 let tag_ptr = unsafe {
881 candidate.reborrow().cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, Self>| { p.cast_sized::<___ZerocopyTagPrimitive>() })
882 };
883 let tag_ptr = unsafe { tag_ptr.assume_initialized() };
884 tag_ptr.recall_validity::<_, (_, (_, _))>().read_unaligned::<::zerocopy::BecauseImmutable>()
885 };
886 let raw_enum = unsafe {
887 candidate.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, Self>| { p.cast_sized::<___ZerocopyRawEnum<'a, N, X, Y>>() })
888 };
889 let raw_enum = unsafe { raw_enum.assume_initialized() };
890 let variants = unsafe {
891 use ::zerocopy::pointer::PtrInner;
892 raw_enum.cast_unsized_unchecked(|p: PtrInner<'_, ___ZerocopyRawEnum<'a, N, X, Y>>| {
893 let p = p.as_non_null().as_ptr();
894 let ptr = core_reexport::ptr::addr_of_mut!((*p).variants);
895 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(ptr) };
896 unsafe { PtrInner::new(ptr) }
897 })
898 };
899 #[allow(non_upper_case_globals)]
900 match tag {
901 ___ZEROCOPY_TAG_UnitLike => true,
902 ___ZEROCOPY_TAG_StructLike => {
903 let variant = unsafe {
904 variants.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, ___ZerocopyVariants<'a, N, X, Y>>| {
905 p.cast_sized::<___ZerocopyVariantStruct_StructLike<'a, N, X, Y>>()
906 })
907 };
908 let variant = unsafe { variant.assume_initialized() };
909 <___ZerocopyVariantStruct_StructLike<'a, N, X, Y> as ::zerocopy ::TryFromBytes>::is_bit_valid (
910 variant)
911 }
912 ___ZEROCOPY_TAG_TupleLike => {
913 let variant = unsafe {
914 variants.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, ___ZerocopyVariants<'a, N, X, Y>>| {
915 p.cast_sized::<___ZerocopyVariantStruct_TupleLike<'a, N, X, Y>>()
916 })
917 };
918 let variant = unsafe { variant.assume_initialized() };
919 <___ZerocopyVariantStruct_TupleLike<'a, N, X, Y> as ::zerocopy ::TryFromBytes>::is_bit_valid (
920 variant)
921 }
922 _ => false,
923 }
924 }
925 }
926 } no_build
927 }
928
929 test! {
930 TryFromBytes {
931 #[repr(u32)]
932 enum ComplexWithGenerics<'a: 'static, const N: usize, X, Y: Deref>
933 where
934 X: Deref<Target = &'a [(X, Y); N]>,
935 {
936 UnitLike,
937 StructLike { a: u8, b: X, c: X::Target, d: Y::Target, e: [(X, Y); N] },
938 TupleLike(bool, Y, PhantomData<&'a [(X, Y); N]>),
939 }
940 } expands to {
941 #[allow(deprecated)]
942 #[automatically_derived]
943 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
944 for ComplexWithGenerics<'a, { N }, X, Y>
945 where
946 X: Deref<Target = &'a [(X, Y); N]>,
947 u8: ::zerocopy::TryFromBytes,
948 X: ::zerocopy::TryFromBytes,
949 X::Target: ::zerocopy::TryFromBytes,
950 Y::Target: ::zerocopy::TryFromBytes,
951 [(X, Y); N]: ::zerocopy::TryFromBytes,
952 bool: ::zerocopy::TryFromBytes,
953 Y: ::zerocopy::TryFromBytes,
954 PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
955 {
956 fn only_derive_is_allowed_to_implement_this_trait() {}
957 fn is_bit_valid<___ZerocopyAliasing>(
958 mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
959 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
960 where
961 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
962 {
963 use ::zerocopy::util::macro_util::core_reexport;
964
965 #[repr(u32)]
966 #[allow(dead_code, non_camel_case_types)]
967 enum ___ZerocopyTag {
968 UnitLike,
969 StructLike,
970 TupleLike,
971 }
972 type ___ZerocopyTagPrimitive = ::zerocopy::util::macro_util::SizeToTag<
973 { core_reexport::mem::size_of::<___ZerocopyTag>() },
974 >;
975 #[allow(non_upper_case_globals)]
976 const ___ZEROCOPY_TAG_UnitLike: ___ZerocopyTagPrimitive =
977 ___ZerocopyTag::UnitLike as ___ZerocopyTagPrimitive;
978 #[allow(non_upper_case_globals)]
979 const ___ZEROCOPY_TAG_StructLike: ___ZerocopyTagPrimitive =
980 ___ZerocopyTag::StructLike as ___ZerocopyTagPrimitive;
981 #[allow(non_upper_case_globals)]
982 const ___ZEROCOPY_TAG_TupleLike: ___ZerocopyTagPrimitive =
983 ___ZerocopyTag::TupleLike as ___ZerocopyTagPrimitive;
984 type ___ZerocopyOuterTag = ();
985 type ___ZerocopyInnerTag = ___ZerocopyTag;
986 #[repr(C)]
987 #[allow(non_snake_case)]
988 struct ___ZerocopyVariantStruct_StructLike<'a: 'static, const N: usize, X, Y: Deref>(
989 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>,
990 u8,
991 X,
992 X::Target,
993 Y::Target,
994 [(X, Y); N],
995 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>,
996 )
997 where
998 X: Deref<Target = &'a [(X, Y); N]>;
999 #[allow(deprecated)]
1000 #[automatically_derived]
1001 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
1002 for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
1003 where
1004 X: Deref<Target = &'a [(X, Y); N]>,
1005 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>: ::zerocopy::TryFromBytes,
1006 u8: ::zerocopy::TryFromBytes,
1007 X: ::zerocopy::TryFromBytes,
1008 X::Target: ::zerocopy::TryFromBytes,
1009 Y::Target: ::zerocopy::TryFromBytes,
1010 [(X, Y); N]: ::zerocopy::TryFromBytes,
1011 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>:
1012 ::zerocopy::TryFromBytes,
1013 {
1014 fn only_derive_is_allowed_to_implement_this_trait() {}
1015 fn is_bit_valid<___ZerocopyAliasing>(
1016 mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
1017 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
1018 where
1019 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
1020 {
1021 use ::zerocopy::util::macro_util::core_reexport;
1022 use ::zerocopy::pointer::PtrInner;
1023
1024 true && {
1025 let field_candidate = unsafe {
1026 let project = |slf: PtrInner<'_, Self>| {
1027 let slf = slf.as_non_null().as_ptr();
1028 let field = core_reexport::ptr::addr_of_mut!((*slf).0);
1029 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1030 unsafe { PtrInner::new(ptr) }
1031 };
1032 candidate.reborrow().cast_unsized_unchecked(project)
1033 };
1034 <core_reexport::mem::MaybeUninit<
1035 ___ZerocopyInnerTag,
1036 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1037 } && {
1038 let field_candidate = unsafe {
1039 let project = |slf: PtrInner<'_, Self>| {
1040 let slf = slf.as_non_null().as_ptr();
1041 let field = core_reexport::ptr::addr_of_mut!((*slf).1);
1042 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1043 unsafe { PtrInner::new(ptr) }
1044 };
1045 candidate.reborrow().cast_unsized_unchecked(project)
1046 };
1047 <u8 as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1048 } && {
1049 let field_candidate = unsafe {
1050 let project = |slf: PtrInner<'_, Self>| {
1051 let slf = slf.as_non_null().as_ptr();
1052 let field = core_reexport::ptr::addr_of_mut!((*slf).2);
1053 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1054 unsafe { PtrInner::new(ptr) }
1055 };
1056 candidate.reborrow().cast_unsized_unchecked(project)
1057 };
1058 <X as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1059 } && {
1060 let field_candidate = unsafe {
1061 let project = |slf: PtrInner<'_, Self>| {
1062 let slf = slf.as_non_null().as_ptr();
1063 let field = core_reexport::ptr::addr_of_mut!((*slf).3);
1064 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1065 unsafe { PtrInner::new(ptr) }
1066 };
1067 candidate.reborrow().cast_unsized_unchecked(project)
1068 };
1069 <X::Target as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1070 } && {
1071 let field_candidate = unsafe {
1072 let project = |slf: PtrInner<'_, Self>| {
1073 let slf = slf.as_non_null().as_ptr();
1074 let field = core_reexport::ptr::addr_of_mut!((*slf).4);
1075 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1076 unsafe { PtrInner::new(ptr) }
1077 };
1078 candidate.reborrow().cast_unsized_unchecked(project)
1079 };
1080 <Y::Target as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1081 } && {
1082 let field_candidate = unsafe {
1083 let project = |slf: PtrInner<'_, Self>| {
1084 let slf = slf.as_non_null().as_ptr();
1085 let field = core_reexport::ptr::addr_of_mut!((*slf).5);
1086 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1087 unsafe { PtrInner::new(ptr) }
1088 };
1089 candidate.reborrow().cast_unsized_unchecked(project)
1090 };
1091 <[(X, Y); N] as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1092 } && {
1093 let field_candidate = unsafe {
1094 let project = |slf: PtrInner<'_, Self>| {
1095 let slf = slf.as_non_null().as_ptr();
1096 let field = core_reexport::ptr::addr_of_mut!((*slf).6);
1097 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1098 unsafe { PtrInner::new(ptr) }
1099 };
1100 candidate.reborrow().cast_unsized_unchecked(project)
1101 };
1102 <core_reexport::marker::PhantomData<
1103 ComplexWithGenerics<'a, N, X, Y>,
1104 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1105 }
1106 }
1107 }
1108 #[repr(C)]
1109 #[allow(non_snake_case)]
1110 struct ___ZerocopyVariantStruct_TupleLike<'a: 'static, const N: usize, X, Y: Deref>(
1111 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>,
1112 bool,
1113 Y,
1114 PhantomData<&'a [(X, Y); N]>,
1115 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>,
1116 )
1117 where
1118 X: Deref<Target = &'a [(X, Y); N]>;
1119 #[allow(deprecated)]
1120 #[automatically_derived]
1121 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
1122 for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
1123 where
1124 X: Deref<Target = &'a [(X, Y); N]>,
1125 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>: ::zerocopy::TryFromBytes,
1126 bool: ::zerocopy::TryFromBytes,
1127 Y: ::zerocopy::TryFromBytes,
1128 PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
1129 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>:
1130 ::zerocopy::TryFromBytes,
1131 {
1132 fn only_derive_is_allowed_to_implement_this_trait() {}
1133 fn is_bit_valid<___ZerocopyAliasing>(
1134 mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
1135 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
1136 where
1137 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
1138 {
1139 use ::zerocopy::util::macro_util::core_reexport;
1140 use ::zerocopy::pointer::PtrInner;
1141
1142 true && {
1143 let field_candidate = unsafe {
1144 let project = |slf: PtrInner<'_, Self>| {
1145 let slf = slf.as_non_null().as_ptr();
1146 let field = core_reexport::ptr::addr_of_mut!((*slf).0);
1147 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1148 unsafe { PtrInner::new(ptr) }
1149 };
1150 candidate.reborrow().cast_unsized_unchecked(project)
1151 };
1152 <core_reexport::mem::MaybeUninit<
1153 ___ZerocopyInnerTag,
1154 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1155 } && {
1156 let field_candidate = unsafe {
1157 let project = |slf: PtrInner<'_, Self>| {
1158 let slf = slf.as_non_null().as_ptr();
1159 let field = core_reexport::ptr::addr_of_mut!((*slf).1);
1160 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1161 unsafe { PtrInner::new(ptr) }
1162 };
1163 candidate.reborrow().cast_unsized_unchecked(project)
1164 };
1165 <bool as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1166 } && {
1167 let field_candidate = unsafe {
1168 let project = |slf: PtrInner<'_, Self>| {
1169 let slf = slf.as_non_null().as_ptr();
1170 let field = core_reexport::ptr::addr_of_mut!((*slf).2);
1171 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1172 unsafe { PtrInner::new(ptr) }
1173 };
1174 candidate.reborrow().cast_unsized_unchecked(project)
1175 };
1176 <Y as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1177 } && {
1178 let field_candidate = unsafe {
1179 let project = |slf: PtrInner<'_, Self>| {
1180 let slf = slf.as_non_null().as_ptr();
1181 let field = core_reexport::ptr::addr_of_mut!((*slf).3);
1182 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1183 unsafe { PtrInner::new(ptr) }
1184 };
1185 candidate.reborrow().cast_unsized_unchecked(project)
1186 };
1187 <PhantomData<&'a [(X, Y); N]> as ::zerocopy::TryFromBytes>::is_bit_valid(
1188 field_candidate,
1189 )
1190 } && {
1191 let field_candidate = unsafe {
1192 let project = |slf: PtrInner<'_, Self>| {
1193 let slf = slf.as_non_null().as_ptr();
1194 let field = core_reexport::ptr::addr_of_mut!((*slf).4);
1195 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1196 unsafe { PtrInner::new(ptr) }
1197 };
1198 candidate.reborrow().cast_unsized_unchecked(project)
1199 };
1200 <core_reexport::marker::PhantomData<
1201 ComplexWithGenerics<'a, N, X, Y>,
1202 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1203 }
1204 }
1205 }
1206 #[repr(C)]
1207 #[allow(non_snake_case)]
1208 union ___ZerocopyVariants<'a: 'static, const N: usize, X, Y: Deref> {
1209 __field_StructLike:
1210 core_reexport::mem::ManuallyDrop<___ZerocopyVariantStruct_StructLike<'a, N, X, Y>>,
1211 __field_TupleLike:
1212 core_reexport::mem::ManuallyDrop<___ZerocopyVariantStruct_TupleLike<'a, N, X, Y>>,
1213 __nonempty: (),
1214 }
1215 #[repr(C)]
1216 struct ___ZerocopyRawEnum<'a: 'static, const N: usize, X, Y: Deref> {
1217 tag: ___ZerocopyOuterTag,
1218 variants: ___ZerocopyVariants<'a, N, X, Y>,
1219 }
1220 let tag = {
1221 let tag_ptr = unsafe {
1222 candidate.reborrow().cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, Self>| { p.cast_sized::<___ZerocopyTagPrimitive> ()})
1223 };
1224 let tag_ptr = unsafe { tag_ptr.assume_initialized() };
1225 tag_ptr.recall_validity::<_, (_, (_, _))>().read_unaligned::<::zerocopy::BecauseImmutable>()
1226 };
1227 let raw_enum = unsafe {
1228 candidate.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, Self>| { p.cast_sized::<___ZerocopyRawEnum<'a, N, X, Y>> ()})
1229 };
1230 let raw_enum = unsafe { raw_enum.assume_initialized() };
1231 let variants = unsafe {
1232 use ::zerocopy::pointer::PtrInner;
1233 raw_enum.cast_unsized_unchecked(|p: PtrInner<'_, ___ZerocopyRawEnum<'a, N, X, Y>>| {
1234 let p = p.as_non_null().as_ptr();
1235 let ptr = core_reexport::ptr::addr_of_mut!((*p).variants);
1236 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(ptr) };
1237 unsafe { PtrInner::new(ptr) }
1238 })
1239 };
1240 #[allow(non_upper_case_globals)]
1241 match tag {
1242 ___ZEROCOPY_TAG_UnitLike => true,
1243 ___ZEROCOPY_TAG_StructLike => {
1244 let variant = unsafe {
1245 variants.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, ___ZerocopyVariants<'a, N, X, Y>>| {
1246 p.cast_sized::<___ZerocopyVariantStruct_StructLike<'a, N, X, Y>>()
1247 })
1248 };
1249 let variant = unsafe { variant.assume_initialized() };
1250 <___ZerocopyVariantStruct_StructLike<'a, N, X, Y> as ::zerocopy ::TryFromBytes>::is_bit_valid (
1251 variant)
1252 }
1253 ___ZEROCOPY_TAG_TupleLike => {
1254 let variant = unsafe {
1255 variants.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, ___ZerocopyVariants<'a, N, X, Y>>| {
1256 p.cast_sized::<___ZerocopyVariantStruct_TupleLike<'a, N, X, Y>>()
1257 })
1258 };
1259 let variant = unsafe { variant.assume_initialized() };
1260 <___ZerocopyVariantStruct_TupleLike<'a, N, X, Y> as ::zerocopy ::TryFromBytes>::is_bit_valid (
1261 variant)
1262 }
1263 _ => false,
1264 }
1265 }
1266 }
1267 } no_build
1268 }
1269
1270 test! {
1271 TryFromBytes {
1272 #[repr(C)]
1273 enum ComplexWithGenerics<'a: 'static, const N: usize, X, Y: Deref>
1274 where
1275 X: Deref<Target = &'a [(X, Y); N]>,
1276 {
1277 UnitLike,
1278 StructLike { a: u8, b: X, c: X::Target, d: Y::Target, e: [(X, Y); N] },
1279 TupleLike(bool, Y, PhantomData<&'a [(X, Y); N]>),
1280 }
1281 } expands to {
1282 #[allow(deprecated)]
1283 #[automatically_derived]
1284 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
1285 for ComplexWithGenerics<'a, { N }, X, Y>
1286 where
1287 X: Deref<Target = &'a [(X, Y); N]>,
1288 u8: ::zerocopy::TryFromBytes,
1289 X: ::zerocopy::TryFromBytes,
1290 X::Target: ::zerocopy::TryFromBytes,
1291 Y::Target: ::zerocopy::TryFromBytes,
1292 [(X, Y); N]: ::zerocopy::TryFromBytes,
1293 bool: ::zerocopy::TryFromBytes,
1294 Y: ::zerocopy::TryFromBytes,
1295 PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
1296 {
1297 fn only_derive_is_allowed_to_implement_this_trait() {}
1298 fn is_bit_valid<___ZerocopyAliasing>(
1299 mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
1300 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
1301 where
1302 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
1303 {
1304 use ::zerocopy::util::macro_util::core_reexport;
1305
1306 #[repr(C)]
1307 #[allow(dead_code, non_camel_case_types)]
1308 enum ___ZerocopyTag {
1309 UnitLike,
1310 StructLike,
1311 TupleLike,
1312 }
1313 type ___ZerocopyTagPrimitive = ::zerocopy::util::macro_util::SizeToTag<
1314 { core_reexport::mem::size_of::<___ZerocopyTag>() },
1315 >;
1316 #[allow(non_upper_case_globals)]
1317 const ___ZEROCOPY_TAG_UnitLike: ___ZerocopyTagPrimitive =
1318 ___ZerocopyTag::UnitLike as ___ZerocopyTagPrimitive;
1319 #[allow(non_upper_case_globals)]
1320 const ___ZEROCOPY_TAG_StructLike: ___ZerocopyTagPrimitive =
1321 ___ZerocopyTag::StructLike as ___ZerocopyTagPrimitive;
1322 #[allow(non_upper_case_globals)]
1323 const ___ZEROCOPY_TAG_TupleLike: ___ZerocopyTagPrimitive =
1324 ___ZerocopyTag::TupleLike as ___ZerocopyTagPrimitive;
1325 type ___ZerocopyOuterTag = ___ZerocopyTag;
1326 type ___ZerocopyInnerTag = ();
1327 #[repr(C)]
1328 #[allow(non_snake_case)]
1329 struct ___ZerocopyVariantStruct_StructLike<'a: 'static, const N: usize, X, Y: Deref>(
1330 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>,
1331 u8,
1332 X,
1333 X::Target,
1334 Y::Target,
1335 [(X, Y); N],
1336 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>,
1337 )
1338 where
1339 X: Deref<Target = &'a [(X, Y); N]>;
1340 #[allow(deprecated)]
1341 #[automatically_derived]
1342 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
1343 for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
1344 where
1345 X: Deref<Target = &'a [(X, Y); N]>,
1346 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>: ::zerocopy::TryFromBytes,
1347 u8: ::zerocopy::TryFromBytes,
1348 X: ::zerocopy::TryFromBytes,
1349 X::Target: ::zerocopy::TryFromBytes,
1350 Y::Target: ::zerocopy::TryFromBytes,
1351 [(X, Y); N]: ::zerocopy::TryFromBytes,
1352 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>:
1353 ::zerocopy::TryFromBytes,
1354 {
1355 fn only_derive_is_allowed_to_implement_this_trait() {}
1356 fn is_bit_valid<___ZerocopyAliasing>(
1357 mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
1358 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
1359 where
1360 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
1361 {
1362 use ::zerocopy::util::macro_util::core_reexport;
1363 use ::zerocopy::pointer::PtrInner;
1364
1365 true && {
1366 let field_candidate = unsafe {
1367 let project = |slf: PtrInner<'_, Self>| {
1368 let slf = slf.as_non_null().as_ptr();
1369 let field = core_reexport::ptr::addr_of_mut!((*slf).0);
1370 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1371 unsafe { PtrInner::new(ptr) }
1372 };
1373 candidate.reborrow().cast_unsized_unchecked(project)
1374 };
1375 <core_reexport::mem::MaybeUninit<
1376 ___ZerocopyInnerTag,
1377 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1378 } && {
1379 let field_candidate = unsafe {
1380 let project = |slf: PtrInner<'_, Self>| {
1381 let slf = slf.as_non_null().as_ptr();
1382 let field = core_reexport::ptr::addr_of_mut!((*slf).1);
1383 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1384 unsafe { PtrInner::new(ptr) }
1385 };
1386 candidate.reborrow().cast_unsized_unchecked(project)
1387 };
1388 <u8 as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1389 } && {
1390 let field_candidate = unsafe {
1391 let project = |slf: PtrInner<'_, Self>| {
1392 let slf = slf.as_non_null().as_ptr();
1393 let field = core_reexport::ptr::addr_of_mut!((*slf).2);
1394 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1395 unsafe { PtrInner::new(ptr) }
1396 };
1397 candidate.reborrow().cast_unsized_unchecked(project)
1398 };
1399 <X as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1400 } && {
1401 let field_candidate = unsafe {
1402 let project = |slf: PtrInner<'_, Self>| {
1403 let slf = slf.as_non_null().as_ptr();
1404 let field = core_reexport::ptr::addr_of_mut!((*slf).3);
1405 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1406 unsafe { PtrInner::new(ptr) }
1407 };
1408 candidate.reborrow().cast_unsized_unchecked(project)
1409 };
1410 <X::Target as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1411 } && {
1412 let field_candidate = unsafe {
1413 let project = |slf: PtrInner<'_, Self>| {
1414 let slf = slf.as_non_null().as_ptr();
1415 let field = core_reexport::ptr::addr_of_mut!((*slf).4);
1416 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1417 unsafe { PtrInner::new(ptr) }
1418 };
1419 candidate.reborrow().cast_unsized_unchecked(project)
1420 };
1421 <Y::Target as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1422 } && {
1423 let field_candidate = unsafe {
1424 let project = |slf: PtrInner<'_, Self>| {
1425 let slf = slf.as_non_null().as_ptr();
1426 let field = core_reexport::ptr::addr_of_mut!((*slf).5);
1427 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1428 unsafe { PtrInner::new(ptr) }
1429 };
1430 candidate.reborrow().cast_unsized_unchecked(project)
1431 };
1432 <[(X, Y); N] as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1433 } && {
1434 let field_candidate = unsafe {
1435 let project = |slf: PtrInner<'_, Self>| {
1436 let slf = slf.as_non_null().as_ptr();
1437 let field = core_reexport::ptr::addr_of_mut!((*slf).6);
1438 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1439 unsafe { PtrInner::new(ptr) }
1440 };
1441 candidate.reborrow().cast_unsized_unchecked(project)
1442 };
1443 <core_reexport::marker::PhantomData<
1444 ComplexWithGenerics<'a, N, X, Y>,
1445 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1446 }
1447 }
1448 }
1449 #[repr(C)]
1450 #[allow(non_snake_case)]
1451 struct ___ZerocopyVariantStruct_TupleLike<'a: 'static, const N: usize, X, Y: Deref>(
1452 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>,
1453 bool,
1454 Y,
1455 PhantomData<&'a [(X, Y); N]>,
1456 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>,
1457 )
1458 where
1459 X: Deref<Target = &'a [(X, Y); N]>;
1460 #[allow(deprecated)]
1461 #[automatically_derived]
1462 unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
1463 for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
1464 where
1465 X: Deref<Target = &'a [(X, Y); N]>,
1466 core_reexport::mem::MaybeUninit<___ZerocopyInnerTag>: ::zerocopy::TryFromBytes,
1467 bool: ::zerocopy::TryFromBytes,
1468 Y: ::zerocopy::TryFromBytes,
1469 PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
1470 core_reexport::marker::PhantomData<ComplexWithGenerics<'a, N, X, Y>>:
1471 ::zerocopy::TryFromBytes,
1472 {
1473 fn only_derive_is_allowed_to_implement_this_trait() {}
1474 fn is_bit_valid<___ZerocopyAliasing>(
1475 mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
1476 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
1477 where
1478 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
1479 {
1480 use ::zerocopy::util::macro_util::core_reexport;
1481 use ::zerocopy::pointer::PtrInner;
1482
1483 true && {
1484 let field_candidate = unsafe {
1485 let project = |slf: PtrInner<'_, Self>| {
1486 let slf = slf.as_non_null().as_ptr();
1487 let field = core_reexport::ptr::addr_of_mut!((*slf).0);
1488 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1489 unsafe { PtrInner::new(ptr) }
1490 };
1491 candidate.reborrow().cast_unsized_unchecked(project)
1492 };
1493 <core_reexport::mem::MaybeUninit<
1494 ___ZerocopyInnerTag,
1495 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1496 } && {
1497 let field_candidate = unsafe {
1498 let project = |slf: PtrInner<'_, Self>| {
1499 let slf = slf.as_non_null().as_ptr();
1500 let field = core_reexport::ptr::addr_of_mut!((*slf).1);
1501 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1502 unsafe { PtrInner::new(ptr) }
1503 };
1504 candidate.reborrow().cast_unsized_unchecked(project)
1505 };
1506 <bool as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1507 } && {
1508 let field_candidate = unsafe {
1509 let project = |slf: PtrInner<'_, Self>| {
1510 let slf = slf.as_non_null().as_ptr();
1511 let field = core_reexport::ptr::addr_of_mut!((*slf).2);
1512 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1513 unsafe { PtrInner::new(ptr) }
1514 };
1515 candidate.reborrow().cast_unsized_unchecked(project)
1516 };
1517 <Y as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1518 } && {
1519 let field_candidate = unsafe {
1520 let project = |slf: PtrInner<'_, Self>| {
1521 let slf = slf.as_non_null().as_ptr();
1522 let field = core_reexport::ptr::addr_of_mut!((*slf).3);
1523 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1524 unsafe { PtrInner::new(ptr) }
1525 };
1526 candidate.reborrow().cast_unsized_unchecked(project)
1527 };
1528 <PhantomData<&'a [(X, Y); N]> as ::zerocopy::TryFromBytes>::is_bit_valid(
1529 field_candidate,
1530 )
1531 } && {
1532 let field_candidate = unsafe {
1533 let project = |slf: PtrInner<'_, Self>| {
1534 let slf = slf.as_non_null().as_ptr();
1535 let field = core_reexport::ptr::addr_of_mut!((*slf).4);
1536 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(field) };
1537 unsafe { PtrInner::new(ptr) }
1538 };
1539 candidate.reborrow().cast_unsized_unchecked(project)
1540 };
1541 <core_reexport::marker::PhantomData<
1542 ComplexWithGenerics<'a, N, X, Y>,
1543 > as ::zerocopy::TryFromBytes>::is_bit_valid(field_candidate)
1544 }
1545 }
1546 }
1547 #[repr(C)]
1548 #[allow(non_snake_case)]
1549 union ___ZerocopyVariants<'a: 'static, const N: usize, X, Y: Deref> {
1550 __field_StructLike:
1551 core_reexport::mem::ManuallyDrop<___ZerocopyVariantStruct_StructLike<'a, N, X, Y>>,
1552 __field_TupleLike:
1553 core_reexport::mem::ManuallyDrop<___ZerocopyVariantStruct_TupleLike<'a, N, X, Y>>,
1554 __nonempty: (),
1555 }
1556 #[repr(C)]
1557 struct ___ZerocopyRawEnum<'a: 'static, const N: usize, X, Y: Deref> {
1558 tag: ___ZerocopyOuterTag,
1559 variants: ___ZerocopyVariants<'a, N, X, Y>,
1560 }
1561 let tag = {
1562 let tag_ptr = unsafe {
1563 candidate.reborrow().cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, Self>| { p.cast_sized::<___ZerocopyTagPrimitive> ()})
1564 };
1565 let tag_ptr = unsafe { tag_ptr.assume_initialized() };
1566 tag_ptr.recall_validity::<_, (_, (_, _))>().read_unaligned::<::zerocopy::BecauseImmutable>()
1567 };
1568 let raw_enum = unsafe {
1569 candidate.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, Self>| { p.cast_sized::<___ZerocopyRawEnum<'a, N, X, Y>> ()})
1570 };
1571 let raw_enum = unsafe { raw_enum.assume_initialized() };
1572 let variants = unsafe {
1573 use ::zerocopy::pointer::PtrInner;
1574 raw_enum.cast_unsized_unchecked(|p: PtrInner<'_, ___ZerocopyRawEnum<'a, N, X, Y>>| {
1575 let p = p.as_non_null().as_ptr();
1576 let ptr = core_reexport::ptr::addr_of_mut!((*p).variants);
1577 let ptr = unsafe { core_reexport::ptr::NonNull::new_unchecked(ptr) };
1578 unsafe { PtrInner::new(ptr) }
1579 })
1580 };
1581 #[allow(non_upper_case_globals)]
1582 match tag {
1583 ___ZEROCOPY_TAG_UnitLike => true,
1584 ___ZEROCOPY_TAG_StructLike => {
1585 let variant = unsafe {
1586 variants.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, ___ZerocopyVariants<'a, N, X, Y>>| {
1587 p.cast_sized::<___ZerocopyVariantStruct_StructLike<'a, N, X, Y>>()
1588 })
1589 };
1590 let variant = unsafe { variant.assume_initialized() };
1591 <___ZerocopyVariantStruct_StructLike<'a, N, X, Y> as ::zerocopy ::TryFromBytes>::is_bit_valid (
1592 variant)
1593 }
1594 ___ZEROCOPY_TAG_TupleLike => {
1595 let variant = unsafe {
1596 variants.cast_unsized_unchecked(|p: ::zerocopy::pointer::PtrInner<'_, ___ZerocopyVariants<'a, N, X, Y>>| {
1597 p.cast_sized::<___ZerocopyVariantStruct_TupleLike<'a, N, X, Y>>()
1598 })
1599 };
1600 let variant = unsafe { variant.assume_initialized() };
1601 <___ZerocopyVariantStruct_TupleLike<'a, N, X, Y> as ::zerocopy ::TryFromBytes>::is_bit_valid (
1602 variant)
1603 }
1604 _ => false,
1605 }
1606 }
1607 }
1608 } no_build
1609 }
1610 }
1611
1612 // This goes at the bottom because it's so verbose and it makes scrolling past
1613 // other code a pain.
1614 #[test]
1615 fn test_from_bytes_enum() {
1616 test! {
1617 FromBytes {
1618 #[repr(u8)]
1619 enum Foo {
1620 Variant0,
1621 Variant1,
1622 Variant2,
1623 Variant3,
1624 Variant4,
1625 Variant5,
1626 Variant6,
1627 Variant7,
1628 Variant8,
1629 Variant9,
1630 Variant10,
1631 Variant11,
1632 Variant12,
1633 Variant13,
1634 Variant14,
1635 Variant15,
1636 Variant16,
1637 Variant17,
1638 Variant18,
1639 Variant19,
1640 Variant20,
1641 Variant21,
1642 Variant22,
1643 Variant23,
1644 Variant24,
1645 Variant25,
1646 Variant26,
1647 Variant27,
1648 Variant28,
1649 Variant29,
1650 Variant30,
1651 Variant31,
1652 Variant32,
1653 Variant33,
1654 Variant34,
1655 Variant35,
1656 Variant36,
1657 Variant37,
1658 Variant38,
1659 Variant39,
1660 Variant40,
1661 Variant41,
1662 Variant42,
1663 Variant43,
1664 Variant44,
1665 Variant45,
1666 Variant46,
1667 Variant47,
1668 Variant48,
1669 Variant49,
1670 Variant50,
1671 Variant51,
1672 Variant52,
1673 Variant53,
1674 Variant54,
1675 Variant55,
1676 Variant56,
1677 Variant57,
1678 Variant58,
1679 Variant59,
1680 Variant60,
1681 Variant61,
1682 Variant62,
1683 Variant63,
1684 Variant64,
1685 Variant65,
1686 Variant66,
1687 Variant67,
1688 Variant68,
1689 Variant69,
1690 Variant70,
1691 Variant71,
1692 Variant72,
1693 Variant73,
1694 Variant74,
1695 Variant75,
1696 Variant76,
1697 Variant77,
1698 Variant78,
1699 Variant79,
1700 Variant80,
1701 Variant81,
1702 Variant82,
1703 Variant83,
1704 Variant84,
1705 Variant85,
1706 Variant86,
1707 Variant87,
1708 Variant88,
1709 Variant89,
1710 Variant90,
1711 Variant91,
1712 Variant92,
1713 Variant93,
1714 Variant94,
1715 Variant95,
1716 Variant96,
1717 Variant97,
1718 Variant98,
1719 Variant99,
1720 Variant100,
1721 Variant101,
1722 Variant102,
1723 Variant103,
1724 Variant104,
1725 Variant105,
1726 Variant106,
1727 Variant107,
1728 Variant108,
1729 Variant109,
1730 Variant110,
1731 Variant111,
1732 Variant112,
1733 Variant113,
1734 Variant114,
1735 Variant115,
1736 Variant116,
1737 Variant117,
1738 Variant118,
1739 Variant119,
1740 Variant120,
1741 Variant121,
1742 Variant122,
1743 Variant123,
1744 Variant124,
1745 Variant125,
1746 Variant126,
1747 Variant127,
1748 Variant128,
1749 Variant129,
1750 Variant130,
1751 Variant131,
1752 Variant132,
1753 Variant133,
1754 Variant134,
1755 Variant135,
1756 Variant136,
1757 Variant137,
1758 Variant138,
1759 Variant139,
1760 Variant140,
1761 Variant141,
1762 Variant142,
1763 Variant143,
1764 Variant144,
1765 Variant145,
1766 Variant146,
1767 Variant147,
1768 Variant148,
1769 Variant149,
1770 Variant150,
1771 Variant151,
1772 Variant152,
1773 Variant153,
1774 Variant154,
1775 Variant155,
1776 Variant156,
1777 Variant157,
1778 Variant158,
1779 Variant159,
1780 Variant160,
1781 Variant161,
1782 Variant162,
1783 Variant163,
1784 Variant164,
1785 Variant165,
1786 Variant166,
1787 Variant167,
1788 Variant168,
1789 Variant169,
1790 Variant170,
1791 Variant171,
1792 Variant172,
1793 Variant173,
1794 Variant174,
1795 Variant175,
1796 Variant176,
1797 Variant177,
1798 Variant178,
1799 Variant179,
1800 Variant180,
1801 Variant181,
1802 Variant182,
1803 Variant183,
1804 Variant184,
1805 Variant185,
1806 Variant186,
1807 Variant187,
1808 Variant188,
1809 Variant189,
1810 Variant190,
1811 Variant191,
1812 Variant192,
1813 Variant193,
1814 Variant194,
1815 Variant195,
1816 Variant196,
1817 Variant197,
1818 Variant198,
1819 Variant199,
1820 Variant200,
1821 Variant201,
1822 Variant202,
1823 Variant203,
1824 Variant204,
1825 Variant205,
1826 Variant206,
1827 Variant207,
1828 Variant208,
1829 Variant209,
1830 Variant210,
1831 Variant211,
1832 Variant212,
1833 Variant213,
1834 Variant214,
1835 Variant215,
1836 Variant216,
1837 Variant217,
1838 Variant218,
1839 Variant219,
1840 Variant220,
1841 Variant221,
1842 Variant222,
1843 Variant223,
1844 Variant224,
1845 Variant225,
1846 Variant226,
1847 Variant227,
1848 Variant228,
1849 Variant229,
1850 Variant230,
1851 Variant231,
1852 Variant232,
1853 Variant233,
1854 Variant234,
1855 Variant235,
1856 Variant236,
1857 Variant237,
1858 Variant238,
1859 Variant239,
1860 Variant240,
1861 Variant241,
1862 Variant242,
1863 Variant243,
1864 Variant244,
1865 Variant245,
1866 Variant246,
1867 Variant247,
1868 Variant248,
1869 Variant249,
1870 Variant250,
1871 Variant251,
1872 Variant252,
1873 Variant253,
1874 Variant254,
1875 Variant255,
1876 }
1877 } expands to {
1878 #[allow(deprecated)]
1879 #[automatically_derived]
1880 unsafe impl ::zerocopy::TryFromBytes for Foo {
1881 fn only_derive_is_allowed_to_implement_this_trait() {}
1882
1883 fn is_bit_valid<___ZerocopyAliasing>(
1884 _candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
1885 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
1886 where
1887 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
1888 {
1889 if false {
1890 fn assert_is_from_bytes<T>()
1891 where
1892 T: ::zerocopy::FromBytes,
1893 T: ?::zerocopy::util::macro_util::core_reexport::marker::Sized,
1894 {}
1895 assert_is_from_bytes::<Self>();
1896 }
1897
1898 true
1899 }
1900 }
1901
1902 #[allow(deprecated)]
1903 #[automatically_derived]
1904 unsafe impl ::zerocopy::FromZeros for Foo {
1905 fn only_derive_is_allowed_to_implement_this_trait() {}
1906 }
1907
1908 #[allow(deprecated)]
1909 #[automatically_derived]
1910 unsafe impl ::zerocopy::FromBytes for Foo {
1911 fn only_derive_is_allowed_to_implement_this_trait() {}
1912 }
1913 } no_build
1914 }
1915 }
1916
1917 #[test]
1918 fn test_try_from_bytes_trivial_is_bit_valid_enum() {
1919 // Even when we aren't deriving `FromBytes` as the top-level trait,
1920 // `TryFromBytes` on enums still detects whether we *could* derive
1921 // `FromBytes`, and if so, performs the same "trivial `is_bit_valid`"
1922 // optimization.
1923 test! {
1924 TryFromBytes {
1925 #[repr(u8)]
1926 enum Foo {
1927 Variant0,
1928 Variant1,
1929 Variant2,
1930 Variant3,
1931 Variant4,
1932 Variant5,
1933 Variant6,
1934 Variant7,
1935 Variant8,
1936 Variant9,
1937 Variant10,
1938 Variant11,
1939 Variant12,
1940 Variant13,
1941 Variant14,
1942 Variant15,
1943 Variant16,
1944 Variant17,
1945 Variant18,
1946 Variant19,
1947 Variant20,
1948 Variant21,
1949 Variant22,
1950 Variant23,
1951 Variant24,
1952 Variant25,
1953 Variant26,
1954 Variant27,
1955 Variant28,
1956 Variant29,
1957 Variant30,
1958 Variant31,
1959 Variant32,
1960 Variant33,
1961 Variant34,
1962 Variant35,
1963 Variant36,
1964 Variant37,
1965 Variant38,
1966 Variant39,
1967 Variant40,
1968 Variant41,
1969 Variant42,
1970 Variant43,
1971 Variant44,
1972 Variant45,
1973 Variant46,
1974 Variant47,
1975 Variant48,
1976 Variant49,
1977 Variant50,
1978 Variant51,
1979 Variant52,
1980 Variant53,
1981 Variant54,
1982 Variant55,
1983 Variant56,
1984 Variant57,
1985 Variant58,
1986 Variant59,
1987 Variant60,
1988 Variant61,
1989 Variant62,
1990 Variant63,
1991 Variant64,
1992 Variant65,
1993 Variant66,
1994 Variant67,
1995 Variant68,
1996 Variant69,
1997 Variant70,
1998 Variant71,
1999 Variant72,
2000 Variant73,
2001 Variant74,
2002 Variant75,
2003 Variant76,
2004 Variant77,
2005 Variant78,
2006 Variant79,
2007 Variant80,
2008 Variant81,
2009 Variant82,
2010 Variant83,
2011 Variant84,
2012 Variant85,
2013 Variant86,
2014 Variant87,
2015 Variant88,
2016 Variant89,
2017 Variant90,
2018 Variant91,
2019 Variant92,
2020 Variant93,
2021 Variant94,
2022 Variant95,
2023 Variant96,
2024 Variant97,
2025 Variant98,
2026 Variant99,
2027 Variant100,
2028 Variant101,
2029 Variant102,
2030 Variant103,
2031 Variant104,
2032 Variant105,
2033 Variant106,
2034 Variant107,
2035 Variant108,
2036 Variant109,
2037 Variant110,
2038 Variant111,
2039 Variant112,
2040 Variant113,
2041 Variant114,
2042 Variant115,
2043 Variant116,
2044 Variant117,
2045 Variant118,
2046 Variant119,
2047 Variant120,
2048 Variant121,
2049 Variant122,
2050 Variant123,
2051 Variant124,
2052 Variant125,
2053 Variant126,
2054 Variant127,
2055 Variant128,
2056 Variant129,
2057 Variant130,
2058 Variant131,
2059 Variant132,
2060 Variant133,
2061 Variant134,
2062 Variant135,
2063 Variant136,
2064 Variant137,
2065 Variant138,
2066 Variant139,
2067 Variant140,
2068 Variant141,
2069 Variant142,
2070 Variant143,
2071 Variant144,
2072 Variant145,
2073 Variant146,
2074 Variant147,
2075 Variant148,
2076 Variant149,
2077 Variant150,
2078 Variant151,
2079 Variant152,
2080 Variant153,
2081 Variant154,
2082 Variant155,
2083 Variant156,
2084 Variant157,
2085 Variant158,
2086 Variant159,
2087 Variant160,
2088 Variant161,
2089 Variant162,
2090 Variant163,
2091 Variant164,
2092 Variant165,
2093 Variant166,
2094 Variant167,
2095 Variant168,
2096 Variant169,
2097 Variant170,
2098 Variant171,
2099 Variant172,
2100 Variant173,
2101 Variant174,
2102 Variant175,
2103 Variant176,
2104 Variant177,
2105 Variant178,
2106 Variant179,
2107 Variant180,
2108 Variant181,
2109 Variant182,
2110 Variant183,
2111 Variant184,
2112 Variant185,
2113 Variant186,
2114 Variant187,
2115 Variant188,
2116 Variant189,
2117 Variant190,
2118 Variant191,
2119 Variant192,
2120 Variant193,
2121 Variant194,
2122 Variant195,
2123 Variant196,
2124 Variant197,
2125 Variant198,
2126 Variant199,
2127 Variant200,
2128 Variant201,
2129 Variant202,
2130 Variant203,
2131 Variant204,
2132 Variant205,
2133 Variant206,
2134 Variant207,
2135 Variant208,
2136 Variant209,
2137 Variant210,
2138 Variant211,
2139 Variant212,
2140 Variant213,
2141 Variant214,
2142 Variant215,
2143 Variant216,
2144 Variant217,
2145 Variant218,
2146 Variant219,
2147 Variant220,
2148 Variant221,
2149 Variant222,
2150 Variant223,
2151 Variant224,
2152 Variant225,
2153 Variant226,
2154 Variant227,
2155 Variant228,
2156 Variant229,
2157 Variant230,
2158 Variant231,
2159 Variant232,
2160 Variant233,
2161 Variant234,
2162 Variant235,
2163 Variant236,
2164 Variant237,
2165 Variant238,
2166 Variant239,
2167 Variant240,
2168 Variant241,
2169 Variant242,
2170 Variant243,
2171 Variant244,
2172 Variant245,
2173 Variant246,
2174 Variant247,
2175 Variant248,
2176 Variant249,
2177 Variant250,
2178 Variant251,
2179 Variant252,
2180 Variant253,
2181 Variant254,
2182 Variant255,
2183 }
2184 } expands to {
2185 #[allow(deprecated)]
2186 #[automatically_derived]
2187 unsafe impl ::zerocopy::TryFromBytes for Foo {
2188 fn only_derive_is_allowed_to_implement_this_trait() {}
2189
2190 fn is_bit_valid<___ZerocopyAliasing>(
2191 _candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
2192 ) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
2193 where
2194 ___ZerocopyAliasing: ::zerocopy::pointer::invariant::Reference,
2195 {
2196 true
2197 }
2198 }
2199 } no_build
2200 }
2201 }
2202
2203 #[test]
2204 fn test_hash() {
2205 test! {
2206 ByteHash {
2207 struct Foo<T: Clone>(T) where Self: Sized;
2208 } expands to {
2209 #[allow(deprecated)]
2210 #[automatically_derived]
2211 impl<T: Clone> ::zerocopy::util::macro_util::core_reexport::hash::Hash for Foo<T>
2212 where
2213 Self: ::zerocopy::IntoBytes + ::zerocopy::Immutable,
2214 Self: Sized,
2215 {
2216 fn hash<H>(&self, state: &mut H)
2217 where
2218 H: ::zerocopy::util::macro_util::core_reexport::hash::Hasher,
2219 {
2220 ::zerocopy::util::macro_util::core_reexport::hash::Hasher::write(
2221 state,
2222 ::zerocopy::IntoBytes::as_bytes(self)
2223 )
2224 }
2225
2226 fn hash_slice<H>(data: &[Self], state: &mut H)
2227 where
2228 H: ::zerocopy::util::macro_util::core_reexport::hash::Hasher,
2229 {
2230 ::zerocopy::util::macro_util::core_reexport::hash::Hasher::write(
2231 state,
2232 ::zerocopy::IntoBytes::as_bytes(data)
2233 )
2234 }
2235 }
2236 } no_build
2237 }
2238 }
2239
2240 #[test]
2241 fn test_eq() {
2242 test! {
2243 ByteEq {
2244 struct Foo<T: Clone>(T) where Self: Sized;
2245 } expands to {
2246 #[allow(deprecated)]
2247 #[automatically_derived]
2248 impl<T: Clone> ::zerocopy::util::macro_util::core_reexport::cmp::PartialEq for Foo<T>
2249 where
2250 Self: ::zerocopy::IntoBytes + ::zerocopy::Immutable,
2251 Self: Sized,
2252 {
2253 fn eq(&self, other: &Self) -> bool {
2254 ::zerocopy::util::macro_util::core_reexport::cmp::PartialEq::eq(
2255 ::zerocopy::IntoBytes::as_bytes(self),
2256 ::zerocopy::IntoBytes::as_bytes(other),
2257 )
2258 }
2259 }
2260
2261 #[allow(deprecated)]
2262 #[automatically_derived]
2263 impl<T: Clone> ::zerocopy::util::macro_util::core_reexport::cmp::Eq for Foo<T>
2264 where
2265 Self: ::zerocopy::IntoBytes + ::zerocopy::Immutable,
2266 Self: Sized,
2267 {
2268 }
2269 } no_build
2270 }
2271 }
2272
2273 #[test]
2274 fn test_split_at() {
2275 test! {
2276 SplitAt {
2277 #[repr(C)]
2278 struct Foo<T: ?Sized + Copy>(T) where Self: Copy;
2279 } expands to {
2280 #[allow(deprecated)]
2281 #[automatically_derived]
2282 unsafe impl<T: ?Sized + Copy> ::zerocopy::SplitAt for Foo<T>
2283 where
2284 Self: Copy,
2285 T: ::zerocopy::SplitAt,
2286 {
2287 fn only_derive_is_allowed_to_implement_this_trait() {}
2288 type Elem = <T as ::zerocopy::SplitAt>::Elem;
2289 }
2290 } no_build
2291 }
2292
2293 test! {
2294 SplitAt {
2295 #[repr(transparent)]
2296 struct Foo<T: ?Sized + Copy>(T) where Self: Copy;
2297 } expands to {
2298 #[allow(deprecated)]
2299 #[automatically_derived]
2300 unsafe impl<T: ?Sized + Copy> ::zerocopy::SplitAt for Foo<T>
2301 where
2302 Self: Copy,
2303 T: ::zerocopy::SplitAt,
2304 {
2305 fn only_derive_is_allowed_to_implement_this_trait() {}
2306 type Elem = <T as ::zerocopy::SplitAt>::Elem;
2307 }
2308 } no_build
2309 }
2310
2311 test! {
2312 SplitAt {
2313 #[repr(packed)]
2314 struct Foo<T: ?Sized + Copy>(T) where Self: Copy;
2315 } expands to {
2316 ::core::compile_error! {
2317 "must not have #[repr(packed)] attribute"
2318 }
2319 } no_build
2320 }
2321
2322 test! {
2323 SplitAt {
2324 #[repr(packed(2))]
2325 struct Foo<T: ?Sized + Copy>(T) where Self: Copy;
2326 } expands to {
2327 ::core::compile_error! {
2328 "must not have #[repr(packed)] attribute"
2329 }
2330 } no_build
2331 }
2332
2333 test! {
2334 SplitAt {
2335 enum Foo {}
2336 } expands to {
2337 ::core::compile_error! {
2338 "can only be applied to structs"
2339 }
2340 } no_build
2341 }
2342
2343 test! {
2344 SplitAt {
2345 union Foo { a: () }
2346 } expands to {
2347 ::core::compile_error! {
2348 "can only be applied to structs"
2349 }
2350 } no_build
2351 }
2352
2353 test! {
2354 SplitAt {
2355 struct Foo<T: ?Sized + Copy>(T) where Self: Copy;
2356 } expands to {
2357 ::core::compile_error! {
2358 "must have #[repr(C)] or #[repr(transparent)] in order to guarantee this type's layout is splitable"
2359 }
2360 } no_build
2361 }
2362 }