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/// ```
49///
50/// ```compile_fail,E0080
51/// let increase_alignment: &u16 = zerocopy::try_transmute_ref!(&[0u8; 2]).unwrap();
52/// ```
53///
54/// ```compile_fail,E0080
55/// let mut src = [0u8; 2];
56/// let increase_alignment: &mut u16 = zerocopy::try_transmute_mut!(&mut src).unwrap();
57/// ```
58enum TransmuteRefMutAlignmentIncrease {}
59
60/// We require that the size of the destination type is not larger than the size
61/// of the source type.
62///
63/// ```compile_fail,E0080
64/// let increase_size: &[u8; 2] = zerocopy::transmute_ref!(&0u8);
65/// ```
66///
67/// ```compile_fail,E0080
68/// let mut src = 0u8;
69/// let increase_size: &mut [u8; 2] = zerocopy::transmute_mut!(&mut src);
70/// ```
71///
72/// ```compile_fail,E0080
73/// let increase_size: &[u8; 2] = zerocopy::try_transmute_ref!(&0u8).unwrap();
74/// ```
75///
76/// ```compile_fail,E0080
77/// let mut src = 0u8;
78/// let increase_size: &mut [u8; 2] = zerocopy::try_transmute_mut!(&mut src).unwrap();
79/// ```
80enum TransmuteRefMutSizeIncrease {}
81
82/// We require that the size of the destination type is not smaller than the
83/// size of the source type.
84///
85/// ```compile_fail,E0080
86/// let decrease_size: &u8 = zerocopy::transmute_ref!(&[0u8; 2]);
87/// ```
88///
89/// ```compile_fail,E0080
90/// let mut src = [0u8; 2];
91/// let decrease_size: &mut u8 = zerocopy::transmute_mut!(&mut src);
92/// ```
93///
94/// ```compile_fail,E0080
95/// let decrease_size: &u8 = zerocopy::try_transmute_ref!(&[0u8; 2]).unwrap();
96/// ```
97///
98/// ```compile_fail,E0080
99/// let mut src = [0u8; 2];
100/// let decrease_size: &mut u8 = zerocopy::try_transmute_mut!(&mut src).unwrap();
101/// ```
102enum TransmuteRefMutSizeDecrease {}
103
104/// It's not possible in the general case to increase the trailing slice offset
105/// during a reference transmutation - some pointer metadata values would not be
106/// supportable, and so such a transmutation would be fallible.
107///
108/// ```compile_fail,E0080
109/// use zerocopy::doctests::SliceDst;
110/// let src: &SliceDst<u8, u8> = SliceDst::new();
111/// let increase_offset: &SliceDst<[u8; 2], u8> = zerocopy::transmute_ref!(src);
112/// ```
113///
114/// ```compile_fail,E0080
115/// use zerocopy::doctests::SliceDst;
116/// let src: &mut SliceDst<u8, u8> = SliceDst::new_mut();
117/// let increase_offset: &mut SliceDst<[u8; 2], u8> = zerocopy::transmute_mut!(src);
118/// ```
119enum TransmuteRefMutDstOffsetIncrease {}
120
121/// Reference transmutes are not possible when the difference between the source
122/// and destination types' trailing slice offsets is not a multiple of the
123/// destination type's trailing slice element size.
124///
125/// ```compile_fail,E0080
126/// use zerocopy::doctests::SliceDst;
127/// let src: &SliceDst<[u8; 3], [u8; 2]> = SliceDst::new();
128/// let _: &SliceDst<[u8; 2], [u8; 2]> = zerocopy::transmute_ref!(src);
129/// ```
130///
131/// ```compile_fail,E0080
132/// use zerocopy::doctests::SliceDst;
133/// let src: &mut SliceDst<[u8; 3], [u8; 2]> = SliceDst::new_mut();
134/// let _: &mut SliceDst<[u8; 2], [u8; 2]> = zerocopy::transmute_mut!(src);
135/// ```
136enum TransmuteRefMutDstOffsetNotMultiple {}
137
138/// Reference transmutes are not possible when the source's trailing slice
139/// element size is not a multiple of the destination's.
140///
141/// ```compile_fail,E0080
142/// use zerocopy::doctests::SliceDst;
143/// let src: &SliceDst<(), [u8; 3]> = SliceDst::new();
144/// let _: &SliceDst<(), [u8; 2]> = zerocopy::transmute_ref!(src);
145/// ```
146///
147/// ```compile_fail,E0080
148/// use zerocopy::doctests::SliceDst;
149/// let src: &mut SliceDst<(), [u8; 3]> = SliceDst::new_mut();
150/// let _: &mut SliceDst<(), [u8; 2]> = zerocopy::transmute_mut!(src);
151/// ```
152enum TransmuteRefMutDstElemSizeNotMultiple {}