Tracking Issue for const_swap_nonoverlapping · Issue #133668 · rust-lang/rust (original) (raw)
Feature gate: #![feature(const_swap_nonoverlapping)]
This is a tracking issue for making swap_nonoverlapping
a const fn
.
Public API
mod ptr { pub const unsafe fn swap_nonoverlapping(x: *mut T, y: *mut T, count: usize); }
Steps / History
- Split out of Tracking Issue for const_swap #83163
- Resolve blocking issues
- Final comment period (FCP)1
- Stabilization PR
Blocking Issues
ptr::swap_nonoverlapping
has a limitation currently where it can fail when the data-to-swap contains pointers that cross the "element boundary" of such a swap (i.e., count > 1
and the pointer straddles the boundary between two T
). Here's an example of code that unexpectedly fails:
const {
let mut ptr1 = &1;
let mut ptr2 = &666;
// Swap ptr1 and ptr2, bytewise.
unsafe {
ptr::swap_nonoverlapping(
ptr::from_mut(&mut ptr1).cast::<u8>(),
ptr::from_mut(&mut ptr2).cast::<u8>(),
mem::size_of::<&i32>(),
);
}
// Make sure they still work.
assert!(*ptr1 == 666);
assert!(*ptr2 == 1);
};
The proper way to fix this is to implement rust-lang/const-eval#72.