We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 149d705 commit 9648340Copy full SHA for 9648340
src/tools/miri/tests/pass/issues/issue-134713-swap_nonoverlapping_untyped.rs
@@ -0,0 +1,30 @@
1
+use std::mem::{size_of, align_of};
2
+
3
+// See <https://github.com/rust-lang/rust/issues/134713>
4
5
+#[repr(C)]
6
+struct Foo(usize, u8);
7
8
+fn main() {
9
+ let buf1: [usize; 2] = [1000, 2000];
10
+ let buf2: [usize; 2] = [3000, 4000];
11
12
+ // Foo and [usize; 2] have the same size and alignment,
13
+ // so swap_nonoverlapping should treat them the same
14
+ assert_eq!(size_of::<Foo>(), size_of::<[usize; 2]>());
15
+ assert_eq!(align_of::<Foo>(), align_of::<[usize; 2]>());
16
17
+ let mut b1 = buf1;
18
+ let mut b2 = buf2;
19
+ // Safety: b1 and b2 are distinct local variables,
20
+ // with the same size and alignment as Foo.
21
+ unsafe {
22
+ std::ptr::swap_nonoverlapping(
23
+ b1.as_mut_ptr().cast::<Foo>(),
24
+ b2.as_mut_ptr().cast::<Foo>(),
25
+ 1,
26
+ );
27
+ }
28
+ assert_eq!(b1, buf2);
29
+ assert_eq!(b2, buf1);
30
+}
0 commit comments