Skip to content

Commit

Permalink
Remove some more consts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed May 3, 2021
1 parent ab7b1cc commit f99929f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 63 deletions.
12 changes: 0 additions & 12 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1755,18 +1755,6 @@ pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
!ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
}

/// Checks whether the regions of memory starting at `src` and `dst` of size
/// `count * size_of::<T>()` do *not* overlap.
pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
let src_usize = src as usize;
let dst_usize = dst as usize;
let size = mem::size_of::<T>().checked_mul(count).unwrap();
let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
// If the absolute distance between the ptrs is at least as big as the size of the buffer,
// they do not overlap.
diff >= size
}

/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
/// and destination must *not* overlap.
///
Expand Down
3 changes: 1 addition & 2 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,7 @@ const unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_replace", issue = "83164")]
pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
// SAFETY: the caller must guarantee that `dst` is valid to be
// cast to a mutable reference (valid for writes, aligned, initialized),
// and cannot overlap `src` since `dst` must point to a distinct
Expand Down
98 changes: 49 additions & 49 deletions library/core/tests/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,52 @@ fn mut_ptr_read() {
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
}

#[test]
fn write() {
use core::ptr;

const fn write_aligned() -> i32 {
let mut res = 0;
unsafe {
ptr::write(&mut res as *mut _, 42);
}
res
}
const ALIGNED: i32 = write_aligned();
assert_eq!(ALIGNED, 42);

const fn write_unaligned() -> [u16; 2] {
let mut two_aligned = [0u16; 2];
unsafe {
let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
ptr::write_unaligned(unaligned_ptr, u16::from_ne_bytes([0x23, 0x45]));
}
two_aligned
}
const UNALIGNED: [u16; 2] = write_unaligned();
assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
}

#[test]
fn mut_ptr_write() {
const fn aligned() -> i32 {
let mut res = 0;
unsafe {
(&mut res as *mut i32).write(42);
}
res
}
const ALIGNED: i32 = aligned();
assert_eq!(ALIGNED, 42);

const fn write_unaligned() -> [u16; 2] {
let mut two_aligned = [0u16; 2];
unsafe {
let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
unaligned_ptr.write_unaligned(u16::from_ne_bytes([0x23, 0x45]));
}
two_aligned
}
const UNALIGNED: [u16; 2] = write_unaligned();
assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
}
//#[test]
//fn write() {
// use core::ptr;
//
// const fn write_aligned() -> i32 {
// let mut res = 0;
// unsafe {
// ptr::write(&mut res as *mut _, 42);
// }
// res
// }
// const ALIGNED: i32 = write_aligned();
// assert_eq!(ALIGNED, 42);
//
// const fn write_unaligned() -> [u16; 2] {
// let mut two_aligned = [0u16; 2];
// unsafe {
// let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
// ptr::write_unaligned(unaligned_ptr, u16::from_ne_bytes([0x23, 0x45]));
// }
// two_aligned
// }
// const UNALIGNED: [u16; 2] = write_unaligned();
// assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
//}

//#[test]
//fn mut_ptr_write() {
// const fn aligned() -> i32 {
// let mut res = 0;
// unsafe {
// (&mut res as *mut i32).write(42);
// }
// res
// }
// const ALIGNED: i32 = aligned();
// assert_eq!(ALIGNED, 42);
//
// const fn write_unaligned() -> [u16; 2] {
// let mut two_aligned = [0u16; 2];
// unsafe {
// let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
// unaligned_ptr.write_unaligned(u16::from_ne_bytes([0x23, 0x45]));
// }
// two_aligned
// }
// const UNALIGNED: [u16; 2] = write_unaligned();
// assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
//}

0 comments on commit f99929f

Please sign in to comment.