zerocopy/doctests.rs
1// Copyright 2025 The Fuchsia Authors
2//
3// Licensed under the 2-Clause BSD License <LICENSE-BSD or
4// https://opensource.org/license/bsd-2-clause>, Apache License, Version 2.0
5// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
6// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
7// This file may not be copied, modified, or distributed except according to
8// those terms.
9
10#![cfg(feature = "derive")] // Required for derives on `SliceDst`
11#![allow(dead_code)]
12
13//! Our UI test framework, built on the `trybuild` crate, does not support
14//! testing for post-monomorphization errors. Instead, we use doctests, which
15//! are able to test for post-monomorphization errors.
16
17use crate::*;
18
19#[derive(KnownLayout, FromBytes, IntoBytes, Immutable)]
20#[repr(C)]
21#[allow(missing_debug_implementations, missing_copy_implementations)]
22pub struct SliceDst<T, U> {
23 pub t: T,
24 pub u: [U],
25}
26
27#[allow(clippy::must_use_candidate, clippy::missing_inline_in_public_items, clippy::todo)]
28impl<T: FromBytes + IntoBytes, U: FromBytes + IntoBytes> SliceDst<T, U> {
29 pub fn new() -> &'static SliceDst<T, U> {
30 todo!()
31 }
32
33 pub fn new_mut() -> &'static mut SliceDst<T, U> {
34 todo!()
35 }
36}
37
38/// We require that the alignment of the destination type is not larger than the
39/// alignment of the source type.
40///
41/// ```compile_fail,E0080
42/// let increase_alignment: &u16 = zerocopy::transmute_ref!(&[0u8; 2]);
43/// ```
44///
45/// ```compile_fail,E0080
46/// let mut src = [0u8; 2];
47/// let increase_alignment: &mut u16 = zerocopy::transmute_mut!(&mut src);
48/// ```
49enum TransmuteRefMutAlignmentIncrease {}
50
51/// We require that the size of the destination type is not larger than the size
52/// of the source type.
53///
54/// ```compile_fail,E0080
55/// let increase_size: &[u8; 2] = zerocopy::transmute_ref!(&0u8);
56/// ```
57///
58/// ```compile_fail,E0080
59/// let mut src = 0u8;
60/// let increase_size: &mut [u8; 2] = zerocopy::transmute_mut!(&mut src);
61/// ```
62enum TransmuteRefMutSizeIncrease {}
63
64/// We require that the size of the destination type is not smaller than the
65/// size of the source type.
66///
67/// ```compile_fail,E0080
68/// let decrease_size: &u8 = zerocopy::transmute_ref!(&[0u8; 2]);
69/// ```
70///
71/// ```compile_fail,E0080
72/// let mut src = [0u8; 2];
73/// let decrease_size: &mut u8 = zerocopy::transmute_mut!(&mut src);
74/// ```
75enum TransmuteRefMutSizeDecrease {}
76
77/// It's not possible in the general case to increase the trailing slice offset
78/// during a reference transmutation - some pointer metadata values would not be
79/// supportable, and so such a transmutation would be fallible.
80///
81/// ```compile_fail,E0080
82/// use zerocopy::doctests::SliceDst;
83/// let src: &SliceDst<u8, u8> = SliceDst::new();
84/// let increase_offset: &SliceDst<[u8; 2], u8> = zerocopy::transmute_ref!(src);
85/// ```
86///
87/// ```compile_fail,E0080
88/// use zerocopy::doctests::SliceDst;
89/// let src: &mut SliceDst<u8, u8> = SliceDst::new_mut();
90/// let increase_offset: &mut SliceDst<[u8; 2], u8> = zerocopy::transmute_mut!(src);
91/// ```
92enum TransmuteRefMutDstOffsetIncrease {}
93
94/// Reference transmutes are not possible when the difference between the source
95/// and destination types' trailing slice offsets is not a multiple of the
96/// destination type's trailing slice element size.
97///
98/// ```compile_fail,E0080
99/// use zerocopy::doctests::SliceDst;
100/// let src: &SliceDst<[u8; 3], [u8; 2]> = SliceDst::new();
101/// let _: &SliceDst<[u8; 2], [u8; 2]> = zerocopy::transmute_ref!(src);
102/// ```
103///
104/// ```compile_fail,E0080
105/// use zerocopy::doctests::SliceDst;
106/// let src: &mut SliceDst<[u8; 3], [u8; 2]> = SliceDst::new_mut();
107/// let _: &mut SliceDst<[u8; 2], [u8; 2]> = zerocopy::transmute_mut!(src);
108/// ```
109enum TransmuteRefMutDstOffsetNotMultiple {}
110
111/// Reference transmutes are not possible when the source's trailing slice
112/// element size is not a multiple of the destination's.
113///
114/// ```compile_fail,E0080
115/// use zerocopy::doctests::SliceDst;
116/// let src: &SliceDst<(), [u8; 3]> = SliceDst::new();
117/// let _: &SliceDst<(), [u8; 2]> = zerocopy::transmute_ref!(src);
118/// ```
119///
120/// ```compile_fail,E0080
121/// use zerocopy::doctests::SliceDst;
122/// let src: &mut SliceDst<(), [u8; 3]> = SliceDst::new_mut();
123/// let _: &mut SliceDst<(), [u8; 2]> = zerocopy::transmute_mut!(src);
124/// ```
125enum TransmuteRefMutDstElemSizeNotMultiple {}