Skip to content

Commit 73de723

Browse files
committed
Make the polymorphic is_nonoverlapping private
1 parent 6cc4843 commit 73de723

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

library/core/src/cell.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@
237237

238238
use crate::cmp::Ordering;
239239
use crate::fmt::{self, Debug, Display};
240-
use crate::intrinsics::is_nonoverlapping;
240+
use crate::intrinsics;
241241
use crate::marker::{PhantomData, Unsize};
242-
use crate::mem;
242+
use crate::mem::{self, size_of};
243243
use crate::ops::{CoerceUnsized, Deref, DerefMut, DispatchFromDyn};
244244
use crate::ptr::{self, NonNull};
245245

@@ -435,11 +435,15 @@ impl<T> Cell<T> {
435435
#[inline]
436436
#[stable(feature = "move_cell", since = "1.17.0")]
437437
pub fn swap(&self, other: &Self) {
438+
fn is_nonoverlapping<T>(src: *const T, dst: *const T) -> bool {
439+
intrinsics::is_nonoverlapping(src.cast(), dst.cast(), size_of::<T>(), 1)
440+
}
441+
438442
if ptr::eq(self, other) {
439443
// Swapping wouldn't change anything.
440444
return;
441445
}
442-
if !is_nonoverlapping(self, other, 1) {
446+
if !is_nonoverlapping(self, other) {
443447
// See <https://github.com/rust-lang/rust/issues/80778> for why we need to stop here.
444448
panic!("`Cell::swap` on overlapping non-identical `Cell`s");
445449
}

library/core/src/intrinsics.rs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
use crate::marker::DiscriminantKind;
5858
use crate::marker::Tuple;
59-
use crate::mem::{self, align_of};
59+
use crate::mem::align_of;
6060

6161
pub mod mir;
6262
pub mod simd;
@@ -2659,33 +2659,12 @@ pub(crate) fn is_valid_allocation_size(size: usize, len: usize) -> bool {
26592659
len <= max_len
26602660
}
26612661

2662-
pub(crate) fn is_nonoverlapping_mono(
2663-
src: *const (),
2664-
dst: *const (),
2665-
size: usize,
2666-
count: usize,
2667-
) -> bool {
2668-
let src_usize = src.addr();
2669-
let dst_usize = dst.addr();
2670-
let Some(size) = size.checked_mul(count) else {
2671-
crate::panicking::panic_nounwind(
2672-
"is_nonoverlapping: `size_of::<T>() * count` overflows a usize",
2673-
)
2674-
};
2675-
let diff = src_usize.abs_diff(dst_usize);
2676-
// If the absolute distance between the ptrs is at least as big as the size of the buffer,
2677-
// they do not overlap.
2678-
diff >= size
2679-
}
2680-
26812662
/// Checks whether the regions of memory starting at `src` and `dst` of size
2682-
/// `count * size_of::<T>()` do *not* overlap.
2683-
#[inline]
2684-
pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
2663+
/// `count * size` do *not* overlap.
2664+
pub(crate) fn is_nonoverlapping(src: *const (), dst: *const (), size: usize, count: usize) -> bool {
26852665
let src_usize = src.addr();
26862666
let dst_usize = dst.addr();
2687-
let Some(size) = mem::size_of::<T>().checked_mul(count) else {
2688-
// Use panic_nounwind instead of Option::expect, so that this function is nounwind.
2667+
let Some(size) = size.checked_mul(count) else {
26892668
crate::panicking::panic_nounwind(
26902669
"is_nonoverlapping: `size_of::<T>() * count` overflows a usize",
26912670
)
@@ -2809,7 +2788,7 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
28092788
) =>
28102789
is_aligned_and_not_null(src, align)
28112790
&& is_aligned_and_not_null(dst, align)
2812-
&& is_nonoverlapping_mono(src, dst, size, count)
2791+
&& is_nonoverlapping(src, dst, size, count)
28132792
);
28142793
copy_nonoverlapping(src, dst, count)
28152794
}

library/core/src/ptr/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ use crate::cmp::Ordering;
381381
use crate::fmt;
382382
use crate::hash;
383383
use crate::intrinsics::{
384-
self, assert_unsafe_precondition, is_aligned_and_not_null, is_nonoverlapping_mono,
384+
self, assert_unsafe_precondition, is_aligned_and_not_null, is_nonoverlapping,
385385
};
386386
use crate::marker::FnPtr;
387387

@@ -976,7 +976,7 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
976976
) =>
977977
is_aligned_and_not_null(x, align)
978978
&& is_aligned_and_not_null(y, align)
979-
&& is_nonoverlapping_mono(x, y, size, count)
979+
&& is_nonoverlapping(x, y, size, count)
980980
);
981981
}
982982

0 commit comments

Comments
 (0)