Debian Codegraph @

debian/rust-vendor/zerocopy/tests/ui/transmute_mut.rs

raw
1 // Copyright 2023 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 include!("../include.rs");
10
11 use zerocopy::transmute_mut;
12
13 fn main() {}
14
15 // `transmute_mut!` does not support transmuting into a non-reference
16 // destination type.
17 const DST_NOT_A_REFERENCE: usize = transmute_mut!(&mut 0u8);
18 //~[msrv, stable, nightly]^ ERROR: mismatched types
19 //~[msrv, stable, nightly]^^ ERROR: mismatched types
20
21 #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
22 #[repr(C)]
23 struct SrcA;
24
25 #[derive(zerocopy::IntoBytes, zerocopy::Immutable)]
26 #[repr(C)]
27 struct DstA;
28
29 // `transmute_mut` requires that the destination type implements `FromBytes`
30 const DST_NOT_FROM_BYTES: &mut DstA = transmute_mut!(&mut SrcA);
31 //~[msrv, stable, nightly]^ ERROR: the trait bound `DstA: FromBytes` is not satisfied
32
33 #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
34 #[repr(C)]
35 struct SrcB;
36
37 #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
38 #[repr(C)]
39 struct DstB;
40
41 // `transmute_mut` requires that the destination type implements `IntoBytes`
42 const DST_NOT_AS_BYTES: &mut DstB = transmute_mut!(&mut SrcB);
43 //~[msrv, stable, nightly]^ ERROR: the trait bound `DstB: IntoBytes` is not satisfied
44
45 // `transmute_mut!` does not support transmuting between non-reference source
46 // and destination types.
47 const SRC_DST_NOT_REFERENCES: &mut usize = transmute_mut!(0usize);
48 //~[msrv, stable, nightly]^ ERROR: mismatched types
49
50 fn ref_src_immutable() {
51 // `transmute_mut!` requires that its source type be a mutable reference.
52 let _: &mut u8 = transmute_mut!(&0u8);
53 //~[msrv, stable, nightly]^ ERROR: mismatched types
54 }
55
56 // `transmute_mut!` does not support transmuting from a non-reference source
57 // type.
58 const SRC_NOT_A_REFERENCE: &mut u8 = transmute_mut!(0usize);
59 //~[msrv, stable, nightly]^ ERROR: mismatched types
60
61 #[derive(zerocopy::IntoBytes, zerocopy::Immutable)]
62 #[repr(C)]
63 struct SrcC;
64
65 #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
66 #[repr(C)]
67 struct DstC;
68
69 // `transmute_mut` requires that the source type implements `FromBytes`
70 const SRC_NOT_FROM_BYTES: &mut DstC = transmute_mut!(&mut SrcC);
71 //~[msrv, stable, nightly]^ ERROR: the trait bound `SrcC: FromBytes` is not satisfied
72
73 #[derive(zerocopy::FromBytes, zerocopy::Immutable)]
74 #[repr(C)]
75 struct SrcD;
76
77 #[derive(zerocopy::FromBytes, zerocopy::IntoBytes, zerocopy::Immutable)]
78 #[repr(C)]
79 struct DstD;
80
81 // `transmute_mut` requires that the source type implements `IntoBytes`
82 const SRC_NOT_AS_BYTES: &mut DstD = transmute_mut!(&mut SrcD);
83 //~[msrv, stable, nightly]^ ERROR: the trait bound `SrcD: IntoBytes` is not satisfied
84
85 // `transmute_mut!` does not support transmuting from an unsized source type to
86 // a sized destination type.
87 const SRC_UNSIZED: &mut [u8; 1] = transmute_mut!(&mut [0u8][..]);
88 //~[msrv, stable]^ ERROR: the method `transmute_mut` exists for struct `Wrap<&mut [u8], &mut [u8; 1]>`, but its trait bounds were not satisfied
89 //~[nightly]^^ ERROR: transmute_mut` exists for struct `zerocopy::util::macro_util::Wrap<&mut [u8], &mut [u8; 1]>`, but its trait bounds were not satisfied