Skip to content

Commit 2a27a9c

Browse files
committed
Auto merge of #137412 - scottmcm:redo-swap, r=cuviper
Ensure `swap_nonoverlapping` is really always untyped This replaces #134954, which was arguably overcomplicated. ## Fixes #134713 Actually using the type passed to `ptr::swap_nonoverlapping` for anything other than its size + align turns out to not work, so this goes back to always erasing the types down to just bytes. (Except in `const`, which keeps doing the same thing as before to preserve `@RalfJung's` fix from #134689) ## Fixes #134946 I'd previously moved the swapping to use auto-vectorization *on bytes*, but someone pointed out on Discord that the tail loop handling from that left a whole bunch of byte-by-byte swapping around. This goes back to manual tail handling to avoid that, then still triggers auto-vectorization on pointer-width values. (So you'll see `<4 x i64>` on `x86-64-v3` for example.)
2 parents 1765240 + beb355e commit 2a27a9c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)