From 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:20:39 +0200 Subject: Merging upstream version 1.70.0+dfsg1. Signed-off-by: Daniel Baumann --- library/core/src/slice/index.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'library/core/src/slice/index.rs') diff --git a/library/core/src/slice/index.rs b/library/core/src/slice/index.rs index c295a0e06..353935324 100644 --- a/library/core/src/slice/index.rs +++ b/library/core/src/slice/index.rs @@ -2,6 +2,7 @@ use crate::intrinsics::assert_unsafe_precondition; use crate::intrinsics::const_eval_select; +use crate::intrinsics::unchecked_sub; use crate::ops; use crate::ptr; @@ -371,25 +372,25 @@ unsafe impl const SliceIndex<[T]> for ops::Range { #[inline] unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] { - let this = ops::Range { start: self.start, end: self.end }; + let this = ops::Range { ..self }; // SAFETY: the caller guarantees that `slice` is not dangling, so it // cannot be longer than `isize::MAX`. They also guarantee that // `self` is in bounds of `slice` so `self` cannot overflow an `isize`, - // so the call to `add` is safe. - + // so the call to `add` is safe and the length calculation cannot overflow. unsafe { assert_unsafe_precondition!( "slice::get_unchecked requires that the range is within the slice", [T](this: ops::Range, slice: *const [T]) => this.end >= this.start && this.end <= slice.len() ); - ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), self.end - self.start) + let new_len = unchecked_sub(self.end, self.start); + ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len) } } #[inline] unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] { - let this = ops::Range { start: self.start, end: self.end }; + let this = ops::Range { ..self }; // SAFETY: see comments for `get_unchecked` above. unsafe { assert_unsafe_precondition!( @@ -397,7 +398,8 @@ unsafe impl const SliceIndex<[T]> for ops::Range { [T](this: ops::Range, slice: *mut [T]) => this.end >= this.start && this.end <= slice.len() ); - ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), self.end - self.start) + let new_len = unchecked_sub(self.end, self.start); + ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len) } } -- cgit v1.2.3