Debian Codegraph @

debian/rust-vendor/zerocopy/src/doctests.rs

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