-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking Issue for const_swap #83163
Comments
This got reverted again by #86003, but hopefully we can re-land it soon. |
I have reverted the constness reverts in #86295 |
Extend the const swap feature Adds the `const_swap` feature gate to three more swap functions. cc tracking issue rust-lang#83163 ```Rust impl<T> [T] { pub const fn swap(&mut self, a: usize, b: usize); pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize); } impl<T: ?Sized> *mut T { pub const unsafe fn swap(self, with: *mut T); }
This should be blocked on const mut refs, I believe. |
The following code used to work with #![feature(const_swap)]
#![feature(const_mut_refs)]
use std::{
mem::{self, MaybeUninit},
ptr,
};
const X: () = {
let mut ptr1 = &1;
let mut ptr2 = &2;
// Swap them, bytewise.
unsafe {
ptr::swap_nonoverlapping(
&mut ptr1 as *mut _ as *mut MaybeUninit<u8>,
&mut ptr2 as *mut _ as *mut MaybeUninit<u8>,
mem::size_of::<&i32>(),
);
}
// Make sure they still work.
assert!(*ptr1 == 2);
assert!(*ptr2 == 1);
}; That is a regression introduced by the new implementation of Not being able to work bytewise on pointer is a general limitation of CTFE, so we could document this as such. However, note that |
…ulacrum test const_copy to make sure bytewise pointer copies are working This is non-trivial; for `swap_nonoverlapping`, this is [not working](rust-lang#83163 (comment)).
test const_copy to make sure bytewise pointer copies are working This is non-trivial; for `swap_nonoverlapping`, this is [not working](rust-lang/rust#83163 (comment)).
So... we could easily stabilize Fixing this requires either an intrinsic for swapping (maybe only used by const-eval), or using const_heap... we have to allocate enough space to do the 3-copy version of swapping, as the more efficient "progressive" swapping is exactly what causes the problem. |
Here's the test for this: #[test]
fn test_const_swap() {
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);
};
const {
let mut ptr1 = &1;
let mut ptr2 = &666;
// Swap ptr1 and ptr2, bytewise. `swap` does not take a count
// so the best we can do is use an array.
type T = [u8; mem::size_of::<&i32>()];
unsafe {
ptr::swap(
ptr::from_mut(&mut ptr1).cast::<T>(),
ptr::from_mut(&mut ptr2).cast::<T>(),
);
}
// Make sure they still work.
assert!(*ptr1 == 666);
assert!(*ptr2 == 1);
};
} |
Just to comment on this, but |
Feel free to make a PR that proposes stabilizing |
Feature gate:
#![feature(const_swap)]
This is a tracking issue for making the functions
mem::swap
andptr::swap[_nonoverlapping]
and some other swap related functionsconst fn
.Public API
Steps / History
mem
andptr
): Constifycopy
related functions #83091[T]
and*mut T )
Extend the const swap feature #90644NonNull
methods stabilized,const
ness moved to this gate Stabilizenon_null_convenience
#124498Unresolved Questions
The text was updated successfully, but these errors were encountered: