isrepr 0.1.0

Generating validation from arbitrary memory to repr(C) types
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
//! Type conversions from arbitrary memory.
//!
//! ## Intro
//!
//! Rust allows us to specify stable layouts of types with `#[repr(C)]` and friends. However, this
//! does not allow us to cast any properly sized and aligned memory to those types, since some
//! values may be invalid and making such a cast in Rust is immediate undefined behaviour. For APIs
//! facing outside Rust, this is very inconvenient.
//!
//! This crate provides a [`HasRepr`] trait that defines the `Raw` associated type. That type has
//! the same layout as `Self`, but is valid for all memory contents. It also provides a method for
//! validating that the representation can be cast to a valid value, and methods that convert by
//! value and by reference that use this validating method.
//!
//! A derive macro [`IsRepr`] can derive an implementation of [`HasRepr`]. This derivation works
//! for most `repr(C)` types, as long as all their members implement [`HasRepr`] themselves.
//! Implementations of `HasRepr` are provided for most primitive types.
//!
//! As an extra, the module defines a simple `Repr<T>` wrapper which transparently wraps the
//! underlying representation of `T` and is guaranteed to be a unique type for every `T`.
//!
//! For example, a representation can be derived and used as follows:
//! ```
//! use isrepr::{IsRepr, Repr, ReprError};
//! use core::convert::TryInto;
//! use core::mem::transmute;
//!
//! #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
//! #[repr(u8)]
//! enum Foo {
//!     FOO = 1,
//!     BAR = 3,
//! }
//!
//! // We accept data from some "untrusted" context.
//! fn bar(f_repr: Repr<Foo>) -> Result<Foo, ReprError> {
//!     f_repr.repr_try_into()
//! }
//!
//! fn main() {
//!     // Pretend that we're some untrusted context.
//!     let foo = bar(unsafe { transmute(Foo::FOO) }).unwrap();
//!     assert_eq!(foo, Foo::FOO);
//!
//!     // Send an invalid value!
//!     bar(unsafe { transmute(17u8) }).unwrap_err();
//! }
//! ```
//!
//! ## Sources
//!
//! You can find Rust's type layout guarantees here:
//!
//! <https://doc.rust-lang.org/reference/type-layout.html>

#![no_std]

pub(crate) mod prims;
pub(crate) mod static_assert;
pub(crate) mod traits;

/// Automatic derivation of [`HasRepr`].
///
/// [`HasRepr`] can be derived for fieldless enums if all values are explicitly specified:
/// ```
/// use isrepr::IsRepr;
///
/// #[derive(IsRepr, Clone, Copy, Debug)]
/// #[repr(u8)]
/// enum Foo {
///     FOO = 1,
///     BAR = 3,
/// }
/// ```
///
/// Fieldless enums can be converted directly from their underlying types:
/// ```
/// use isrepr::{HasRepr, IsRepr, Repr, RawTryInto};
/// use core::mem::transmute;
///
/// #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
/// #[repr(u8)]
/// enum Foo {
///     FOO = 1,
///     BAR = 3,
/// }
///
/// let foo = Foo::FOO;
/// let foo_u8 = foo as u8;
/// let foo_2: Foo = foo_u8.raw_try_into().unwrap();
/// assert_eq!(foo, foo_2);
/// ```
///
/// It can be derived for enums with fields and for structs, as long as all members implement
/// [`HasRepr`]:
///
/// ```
/// mod my_crate {
///     use isrepr::IsRepr;
///
///     #[derive(IsRepr, Clone, Copy, Debug)]
///     #[repr(u8)]
///     enum Foo {
///         FOO(u8),
///         BAR(*const usize),
///     }
///
///     #[derive(IsRepr, Clone, Copy, Debug)]
///     #[repr(C)]
///     struct Bar {
///         foo: u8,
///         bar: Foo,
///     }
/// }
/// ```
///
/// It supports both enum representations, `repr(C, prim)` and `repr(prim)`:
///
/// ```
/// mod my_crate {
///     use isrepr::IsRepr;
///
///     #[derive(IsRepr, Clone, Copy, Debug)]
///     #[repr(u8)]
///     enum Foo {
///         FOO(u8),
///         BAR(*const usize),
///     }
///
///     #[derive(IsRepr, Clone, Copy, Debug)]
///     #[repr(C, u8)]
///     enum Bar {
///         FOO(u8),
///         BAR(*const usize),
///     }
/// }
/// ```
///
/// [`HasRepr`] types can be converted by reference:
///
/// ```
/// use isrepr::{HasRepr, IsRepr, Repr};
/// use core::mem::transmute;
/// #[derive(IsRepr, Clone, Copy, Debug)]
/// #[repr(u8)]
/// enum Foo {
///     FOO = 1,
///     BAR = 3,
/// }
///
/// let foo: Repr<Foo> = unsafe { transmute(Foo::FOO) };
/// let _foo_ref: &Foo = foo.ref_try_into().unwrap();
/// ```
///
/// ## `IsRepr` for types with lifetimes
///
/// [`IsRepr`] has basic support for structs with lifetimes, enough to support types like nested
/// `PhantomData<&'a _>`. This support is implemented by erasing lifetime information in the
/// underlying type. Any `PhantomData` types are represented by unit `()`, and any data member `t:
/// T<'a, 'b ...>` is represented by `T<'static, 'static ...>::Raw`.
///
/// Lifetime support extends to the type-checked [`Repr`] type, so code that tries to circumvent
/// lifetimes by casting to representation and back will not compile:
///
/// ```compile_fail
/// use isrepr::{HasRepr, IsRepr, Repr};
/// use core::marker::PhantomData;
///
/// #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
/// #[repr(C)]
/// struct WithLifetime<'a> {
///     x: u8,
///     p: PhantomData<&'a u8>,
/// }
///
/// fn bad_cast<'a>(wl: WithLifetime<'a>) -> WithLifetime<'static> {
///     let el = wl.into_repr();
///     let bad_wl: WithLifetime<'static> = el.repr_try_into().unwrap();
///     bad_wl
/// }
/// ```
///
/// ## Limitations
///
/// * `#[derive(IsRepr)]` should be placed above all `#[repr(C)]`-like attributes. There's no
///   specification on the order of processing attibute macros, but apparrently it is done top
///   down.
/// * Automatic derivation for parametrized types with parameters that are not lifetimes is not
///   supported.
///
pub use isrepr_macros::IsRepr;

pub use traits::{HasRepr, RawTryInto, Repr, ReprError};

// Needed for macros work in tests.
#[cfg(test)]
extern crate self as isrepr;

#[cfg(test)]
mod test {
    extern crate alloc;
    extern crate std;
    use alloc::format;

    use crate::{
        traits::{HasRepr, Repr, ReprError},
        IsRepr,
    };
    use std::{
        marker::PhantomData,
        mem::{align_of, size_of, transmute_copy},
    };

    fn to_repr<T: HasRepr>(t: &T) -> Repr<T> {
        unsafe { transmute_copy(t) }
    }

    unsafe fn raw_write<T, M>(t: &mut T, m: M, off: usize) {
        let tp = t as *mut _ as *mut u8;
        let tm = &m as *const _ as *const u8;
        let tp = tp.offset(off as isize);
        tp.copy_from(tm, size_of::<M>());
    }

    mod layout {
        extern crate alloc;
        extern crate std;
        use super::{raw_write, to_repr};
        use alloc::vec::Vec;

        use crate::{
            traits::{HasRepr, Repr, ReprError},
            IsRepr,
        };
        use core::panic::RefUnwindSafe;
        use std::sync::Mutex;
        use std::{
            collections::HashSet,
            mem::{align_of, size_of},
        };
        // Start by testing layout. Here's how we do it.
        //
        // In a type, we encode its expected layout, like so:
        // struct Foo {
        //     x: L<0, 0>,
        //     y: L<8, 1>,
        // }
        //
        // The L<N, ID> types have a custom implementation of HasRepr that verifies that they have
        // the right offset relative to the type being checked. In order to check that offset, we
        // set each L to a unique bit pattern, which then gets verified inside the type. FIXME:
        // This is not 100% perfect as it doesn't guarantee that padding doesn't have the same
        // value.

        // The type comes first. Dirty shortcut: T should be valid for any memory contents.
        trait Num: Sized + Copy + Eq + std::fmt::Debug + Default {}
        impl<T: Sized + Copy + Eq + std::fmt::Debug + Default> Num for T {}

        #[derive(Clone, Copy, PartialEq, Eq, Debug)]
        #[repr(C)]
        struct L<T: Num, const OFF: usize, const ID: u8>(T);

        impl<T: Num, const OFF: usize, const ID: u8> L<T, OFF, ID> {
            fn unique_byte_pattern() -> Vec<u8> {
                alloc::vec![0xffu8 - ID; size_of::<Self>()]
            }

            fn new() -> Self {
                let mut me = Self(Default::default());
                let mep = &mut me as *mut _ as *mut u8;
                let unique_byte_pattern = Self::unique_byte_pattern();
                unsafe {
                    mep.copy_from(unique_byte_pattern.as_ptr(), size_of::<Self>());
                }
                me
            }
        }

        unsafe impl<T: Num, const OFF: usize, const ID: u8> HasRepr for L<T, OFF, ID> {
            type Raw = Self;

            fn raw_is_valid(value: &Self::Raw) -> Result<(), ReprError> {
                let unique_byte_pattern = Self::unique_byte_pattern();
                let current_byte_pattern = unsafe {
                    std::slice::from_raw_parts(value as *const _ as *const u8, size_of::<Self>())
                };
                assert_eq!(&unique_byte_pattern, current_byte_pattern);
                add_verified_offset(ID);
                Ok(())
            }
        }

        // Now, something to verify we checked all members and exactly the members we want. Can't
        // pass arguments to IsRepr, so we access a global in HasRepr implementation.
        struct GlobalLayoutInfo {
            verified_offsets: Vec<u8>,
        }
        unsafe impl Send for GlobalLayoutInfo {}

        // Hack: use a separate mutex, since HasRepr implementation is called from a different
        // context.
        static OFFSET_CHECK_LOCK: Mutex<()> = Mutex::new(());
        static LAYOUT_INFO: Mutex<GlobalLayoutInfo> = Mutex::new(GlobalLayoutInfo {
            verified_offsets: Vec::new(),
        });

        fn add_verified_offset(off: u8) {
            LAYOUT_INFO.lock().unwrap().verified_offsets.push(off);
        }

        fn clear_verified_offsets() {
            LAYOUT_INFO.lock().unwrap().verified_offsets.clear();
        }

        fn compare_verified_offsets(to: &[u8]) {
            let offs = LAYOUT_INFO.lock().unwrap().verified_offsets.clone();
            let mut expected_set: HashSet<u8> = HashSet::new();
            expected_set.extend(to);
            let mut actual_set = HashSet::new();
            actual_set.extend(offs);
            assert_eq!(expected_set, actual_set);
        }

        fn test_layout<T: HasRepr>(t: &T, expected_offsets: &[u8])
        where
            T::Raw: RefUnwindSafe,
        {
            assert_eq!(size_of::<T>(), size_of::<Repr<T>>());
            assert_eq!(align_of::<T>(), align_of::<Repr<T>>());
            let t_repr = to_repr(t);
            let t_repr_ref = &t_repr;

            let _lock = OFFSET_CHECK_LOCK.lock().unwrap();

            // Unlock the global lock on panic
            let p = std::panic::catch_unwind(|| {
                clear_verified_offsets();
                let _new_t = t_repr_ref.ref_try_into().unwrap();
                compare_verified_offsets(expected_offsets);
            });
            drop(_lock);
            match p {
                Ok(r) => r,
                Err(e) => std::panic::panic_any(e),
            }
        }

        // We cannot test tag offset directly, so we just check if invalid tag causes conversion to fail.
        unsafe fn test_enum_invalid_tag<E: HasRepr + std::fmt::Debug, M>(
            e: &mut E,
            m: M,
            off: usize,
        ) {
            let mut e = to_repr(e);
            raw_write(&mut e, m, off);
            e.repr_try_into().unwrap_err();
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(u8)]
        enum ValuelessEnum {
            FOO = 5,
            BAR = 10,
        }

        #[test]
        fn test_valueless_enum() {
            test_layout(&ValuelessEnum::FOO, &[]);
            test_layout(&ValuelessEnum::BAR, &[]);
            unsafe {
                test_enum_invalid_tag(&mut ValuelessEnum::FOO, 15u8, 0);
            }
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(u8)]
        enum EnumWithValues {
            Unit,
            Tuple(L<u64, 8, 0>, L<u64, 16, 1>),
            Struct {
                x: L<u32, 4, 2>,
                y: L<u32, 8, 3>,
                z: L<u32, 12, 4>,
            },
        }

        #[test]
        fn test_enum_with_values() {
            test_layout(&EnumWithValues::Unit, &[]);
            test_layout(&EnumWithValues::Tuple(L::new(), L::new()), &[0, 1]);
            test_layout(
                &EnumWithValues::Struct {
                    x: L::new(),
                    y: L::new(),
                    z: L::new(),
                },
                &[2, 3, 4],
            );
            unsafe {
                test_enum_invalid_tag(&mut EnumWithValues::Unit, 3u8, 0);
            }
        }

        // In a repr(C) enum, tag is a separate member outside the union. This means that Struct.x
        // has offset 8 rather than 4, since Tuple forces alignment to 8.
        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C, u8)]
        enum CEnumWithValues {
            Unit,
            Tuple(L<u64, 8, 0>, L<u64, 16, 1>),
            Struct {
                x: L<u32, 8, 2>,
                y: L<u32, 12, 3>,
                z: L<u32, 16, 4>,
            },
        }

        #[test]
        fn test_c_enum_with_values() {
            test_layout(&CEnumWithValues::Unit, &[]);
            test_layout(&CEnumWithValues::Tuple(L::new(), L::new()), &[0, 1]);
            test_layout(
                &CEnumWithValues::Struct {
                    x: L::new(),
                    y: L::new(),
                    z: L::new(),
                },
                &[2, 3, 4],
            );
            unsafe {
                test_enum_invalid_tag(&mut CEnumWithValues::Unit, 3u8, 0);
            }
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C)]
        struct Struct {
            x: L<u8, 0, 0>,
            y: L<u32, 4, 1>,
            z: L<u64, 8, 2>,
            t: L<u64, 16, 3>,
        }

        #[test]
        fn test_struct() {
            test_layout(
                &Struct {
                    x: L::new(),
                    y: L::new(),
                    z: L::new(),
                    t: L::new(),
                },
                &[0, 1, 2, 3],
            );
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C)]
        struct StructWithArray {
            x: L<[u8; 3], 0, 0>,
            y: L<[u32; 3], 4, 1>,
            z: L<[u8; 5], 16, 2>,
            t: L<u64, 24, 3>,
        }

        #[test]
        fn test_struct_with_array() {
            test_layout(
                &StructWithArray {
                    x: L::new(),
                    y: L::new(),
                    z: L::new(),
                    t: L::new(),
                },
                &[0, 1, 2, 3],
            );
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C)]
        struct NestedReprOuter {
            x: L<u8, 0, 0>,
            inner: NestedReprInner,
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(u8)]
        enum NestedReprInner {
            Unit(L<u8, 2, 1>),
        }

        #[test]
        fn test_nested_repr() {
            test_layout(
                &NestedReprOuter {
                    x: L::new(),
                    inner: NestedReprInner::Unit(L::new()),
                },
                &[0, 1],
            );
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C, align(16))]
        struct Aligned16Outer {
            foo: L<u32, 0, 0>,
            bar: Aligned16Inner,
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C, align(16))]
        struct Aligned16Inner {
            x: L<u32, 16, 1>,
            y: L<u32, 20, 2>,
            z: L<u32, 24, 3>,
        }

        #[test]
        fn test_aligned_struct() {
            test_layout(
                &Aligned16Outer {
                    foo: L::new(),
                    bar: Aligned16Inner {
                        x: L::new(),
                        y: L::new(),
                        z: L::new(),
                    },
                },
                &[0, 1, 2, 3],
            );
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C)]
        struct StructWithAligned4Enum {
            foo: L<u8, 0, 0>,
            bar: Aligned4Enum,
        }

        // Aligned enum behaves as if it was wrapped in a newtype struct with the specified alignment.
        // That is, its contents are unaffected by align, only its alignment as a whole.
        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(u8, align(4))]
        #[allow(dead_code)]
        enum Aligned4Enum {
            Bar,
            Foo(L<u8, 5, 1>),
        }

        #[test]
        fn test_aligned_enum() {
            test_layout(
                &StructWithAligned4Enum {
                    foo: L::new(),
                    bar: Aligned4Enum::Foo(L::new()),
                },
                &[0, 1],
            );
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C)]
        struct StructWithCAligned4Enum {
            foo: L<u8, 0, 0>,
            bar: CAligned4Enum,
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C, u8, align(4))]
        #[allow(dead_code)]
        enum CAligned4Enum {
            Bar(L<u16, 6, 1>),
            Foo(L<u8, 6, 2>),
        }

        #[test]
        fn test_c_aligned_enum() {
            test_layout(
                &StructWithCAligned4Enum {
                    foo: L::new(),
                    bar: CAligned4Enum::Foo(L::new()),
                },
                &[0, 2],
            );
            test_layout(
                &StructWithCAligned4Enum {
                    foo: L::new(),
                    bar: CAligned4Enum::Bar(L::new()),
                },
                &[0, 1],
            );
        }

        // Packed enums are disallowed in Rust.
        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C, packed)]
        struct Packed {
            foo: L<u8, 0, 0>,
            bar: L<u32, 1, 1>,
            baz: L<u64, 5, 2>,
        }

        // Packed(n) aligns all members to smaller of their natural alignment and 2^n.
        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(C, packed(2))]
        struct Packed2 {
            foo: L<u8, 0, 0>,
            bar: L<u32, 2, 1>,
            baz: L<u64, 6, 2>,
            bax: L<u8, 14, 3>,
            bav: L<u8, 15, 4>,
        }

        #[test]
        fn test_c_packed_enum() {
            test_layout(
                &Packed {
                    foo: L::new(),
                    bar: L::new(),
                    baz: L::new(),
                },
                &[0, 1, 2],
            );
            test_layout(
                &Packed2 {
                    foo: L::new(),
                    bar: L::new(),
                    baz: L::new(),
                    bax: L::new(),
                    bav: L::new(),
                },
                &[0, 1, 2, 3, 4],
            );
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(transparent)]
        enum TransparentEnum {
            FOO((), L<u32, 0, 0>, ()),
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(transparent)]
        enum EmptyTransparentEnum {
            FOO,
        }

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(transparent)]
        struct TransparentStruct((), L<u32, 0, 0>, ());

        #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
        #[repr(transparent)]
        struct EmptyTransparentStruct;

        #[test]
        fn test_transparent() {
            test_layout(&TransparentEnum::FOO((), L::new(), ()), &[0]);
            test_layout(&EmptyTransparentEnum::FOO, &[]);
            test_layout(&TransparentStruct((), L::new(), ()), &[0]);
            test_layout(&EmptyTransparentStruct, &[]);
        }
    }

    #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
    #[repr(u8)]
    enum ValuelessEnum {
        FOO = 5,
        BAR = 10,
    }

    #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
    #[repr(C, u8)]
    // 'pub' tests if we export repr type properly.
    pub enum CEnumWithValues {
        Unit,
        Tuple(u64, u64),
        Struct { x: u32, y: u32, z: u32 },
    }

    #[test]
    fn test_valueless_enum_from_raw() {
        assert_eq!(ValuelessEnum::try_from_raw(8), Err(ReprError));
        assert_eq!(
            ValuelessEnum::try_from_repr(<Repr<ValuelessEnum>>::from_raw(5)),
            Ok(ValuelessEnum::FOO)
        );
    }

    #[test]
    fn test_valueless_enum_ref() {
        let mut base_val = <Repr<ValuelessEnum>>::from_raw(5);
        let foo = ValuelessEnum::try_from_ref(&base_val).unwrap();
        assert_eq!(*foo, ValuelessEnum::FOO);

        let foo = ValuelessEnum::try_from_mut(&mut base_val).unwrap();
        assert_eq!(*foo, ValuelessEnum::FOO);

        *foo = ValuelessEnum::BAR;

        assert_eq!(base_val.repr_try_into().unwrap(), ValuelessEnum::BAR);
    }

    #[test]
    fn test_value_enum_with_values_debug() {
        let value = CEnumWithValues::Unit;
        let value_repr: Repr<CEnumWithValues> = to_repr(&value);
        assert_eq!(
            format!("{:?}", value_repr),
            "Repr(CEnumWithValuesRepr { _repr_tag: 0, f_Unit: Unit })"
        );

        let value = CEnumWithValues::Tuple(5, 10);
        let value_repr: Repr<CEnumWithValues> = to_repr(&value);
        assert_eq!(
            format!("{:?}", value_repr),
            "Repr(CEnumWithValuesRepr { _repr_tag: 1, f_Tuple: Tuple(5, 10) })"
        );

        let value = CEnumWithValues::Struct { x: 1, y: 2, z: 3 };
        let mut value_repr: Repr<CEnumWithValues> = to_repr(&value);
        assert_eq!(
            format!("{:?}", value_repr),
            "Repr(CEnumWithValuesRepr { _repr_tag: 2, f_Struct: Struct { x: 1, y: 2, z: 3 } })"
        );

        let repr_ptr = &mut value_repr as *mut Repr<CEnumWithValues> as *mut u8;
        unsafe { core::ptr::write(repr_ptr, 3) };
        assert_eq!(
            format!("{:?}", value_repr),
            "Repr(CEnumWithValuesRepr { _repr_tag: 3 })"
        );
    }

    #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
    #[repr(C, u8)]
    enum EnumWithBoolAndArray {
        Bool(bool),
        Array([bool; 7]),
    }

    #[test]
    fn test_bool_enum_with_values() {
        for value in &[
            EnumWithBoolAndArray::Bool(true),
            EnumWithBoolAndArray::Array([true, false, false, false, true, true, true]),
        ] {
            let r = to_repr(value);
            r.repr_try_into().unwrap();
        }

        let mut value = EnumWithBoolAndArray::Bool(true);
        unsafe { raw_write(&mut value, 2u8, 1) };
        let r = to_repr(&value);
        r.repr_try_into().unwrap_err();

        let mut value = EnumWithBoolAndArray::Array([true; 7]);
        unsafe { raw_write(&mut value, 2u8, 5) };
        let r = to_repr(&value);
        r.repr_try_into().unwrap_err();
    }

    #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
    #[repr(C)]
    struct StructWithLifetime<'a> {
        x: u8,
        p: PhantomData<&'a u8>,
    }

    #[test]
    fn test_repr_with_lifetime_layout_matches() {
        assert_eq!(
            size_of::<StructWithLifetime<'static>>(),
            size_of::<Repr<StructWithLifetime<'static>>>()
        );
        assert_eq!(
            align_of::<StructWithLifetime<'static>>(),
            align_of::<Repr<StructWithLifetime<'static>>>()
        );
    }

    #[derive(IsRepr)]
    #[repr(C)]
    struct NestedLifetimeInner<'a>(PhantomData<&'a u8>);

    #[derive(IsRepr, Clone, Copy, Debug, PartialEq, Eq)]
    #[repr(C)]
    struct NestedLifetimeOuter<'a>(*const Repr<NestedLifetimeInner<'a>>);

    #[test]
    fn test_nested_lifetime_struct() {
        // Test if it's properly generated and nothing more.
        let _ = NestedLifetimeOuter(std::ptr::null());
    }

    #[derive(IsRepr)]
    #[repr(C)]
    struct UnsizedPhantomData(PhantomData<[u8]>);

    #[derive(IsRepr)]
    #[repr(C)]
    struct UnsizedPtr(*mut [u8]);
}