From f99929f24aa5cb3962dfa8c45afe76a262376238 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Mon, 3 May 2021 14:41:52 -0400 Subject: [PATCH] Remove some more consts --- library/core/src/intrinsics.rs | 12 ---- library/core/src/ptr/mod.rs | 3 +- library/core/tests/const_ptr.rs | 98 ++++++++++++++++----------------- 3 files changed, 50 insertions(+), 63 deletions(-) diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index 84fd3124107d8..073210c5b6857 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -1755,18 +1755,6 @@ pub(crate) fn is_aligned_and_not_null(ptr: *const T) -> bool { !ptr.is_null() && ptr as usize % mem::align_of::() == 0 } -/// Checks whether the regions of memory starting at `src` and `dst` of size -/// `count * size_of::()` do *not* overlap. -pub(crate) fn is_nonoverlapping(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::().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::()` bytes from `src` to `dst`. The source /// and destination must *not* overlap. /// diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 67185217d5e40..fd24469bd3cd7 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -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(dst: *mut T, mut src: T) -> T { +pub unsafe fn replace(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 diff --git a/library/core/tests/const_ptr.rs b/library/core/tests/const_ptr.rs index 152fed803ecdb..4158a4dece729 100644 --- a/library/core/tests/const_ptr.rs +++ b/library/core/tests/const_ptr.rs @@ -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])]); +//}