Skip to content

Commit c28877c

Browse files
committed
add test for bytewise ptr::swap of a pointer
1 parent 9bfd462 commit c28877c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(const_eval_select)]
1919
#![feature(const_heap)]
2020
#![feature(const_nonnull_new)]
21+
#![feature(const_swap)]
2122
#![feature(const_trait_impl)]
2223
#![feature(core_intrinsics)]
2324
#![feature(core_io_borrowed_buf)]

library/core/tests/ptr.rs

+19
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,25 @@ fn test_const_copy() {
897897
};
898898
}
899899

900+
#[test]
901+
fn test_const_swap() {
902+
const {
903+
let mut ptr1 = &1;
904+
let mut ptr2 = &666;
905+
906+
// Swap ptr1 and ptr2, bytewise. `swap` does not take a count
907+
// so the best we can do is use an array.
908+
type T = [u8; mem::size_of::<&i32>()];
909+
unsafe {
910+
ptr::swap(ptr::from_mut(&mut ptr1).cast::<T>(), ptr::from_mut(&mut ptr2).cast::<T>());
911+
}
912+
913+
// Make sure they still work.
914+
assert!(*ptr1 == 666);
915+
assert!(*ptr2 == 1);
916+
};
917+
}
918+
900919
#[test]
901920
fn test_null_array_as_slice() {
902921
let arr: *mut [u8; 4] = null_mut();

0 commit comments

Comments
 (0)