-
Notifications
You must be signed in to change notification settings - Fork 13.3k
swap_unchecked ? #71874
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
Comments
It is relatively easy to implement this outside of libcore (in your own code). The preferred way (current best practice) would be to go through Use |
Thank you for the comments bluss, second version then: #[inline]
pub unsafe fn swap_unchecked(&mut self, i: usize, j: usize) {
let ptr1 = self.as_mut_ptr().add(i);
let ptr2 = self.as_mut_ptr().add(j);
ptr::swap(ptr1, ptr2);
} |
I'd propose this for anyone who wants to copy this implementation (this is what was meant with only using one // Method on [T]
pub unsafe fn swap_unchecked(&mut self, i: usize, j: usize) {
debug_assert!(i < self.len());
debug_assert!(j < self.len());
let data_ptr = self.as_mut_ptr();
std::ptr::swap(data_ptr.add(i), data_ptr.add(j));
} |
@bluss maybe add a |
For ptr::swap, equality is ok |
Ah you are right. |
Implemented. |
A slice method similar to slice::get_unchecked_mut that could be added:
To be added to the std lib only if there's enough usage for this method.
See also:
https://crates.io/crates/unchecked-index
The text was updated successfully, but these errors were encountered: