mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-30 03:57:37 +00:00
Auto merge of #49551 - scottmcm:deprecate-offset_to, r=KodrAus
Deprecate offset_to; switch core&alloc to using offset_from instead Bonus: might make code than uses `.len()` on slice iterators faster cc https://github.com/rust-lang/rust/issues/41079
This commit is contained in:
commit
9afed64645
@ -99,11 +99,11 @@
|
|||||||
#![feature(lang_items)]
|
#![feature(lang_items)]
|
||||||
#![feature(needs_allocator)]
|
#![feature(needs_allocator)]
|
||||||
#![feature(nonzero)]
|
#![feature(nonzero)]
|
||||||
#![feature(offset_to)]
|
|
||||||
#![feature(optin_builtin_traits)]
|
#![feature(optin_builtin_traits)]
|
||||||
#![feature(pattern)]
|
#![feature(pattern)]
|
||||||
#![feature(pin)]
|
#![feature(pin)]
|
||||||
#![feature(ptr_internals)]
|
#![feature(ptr_internals)]
|
||||||
|
#![feature(ptr_offset_from)]
|
||||||
#![feature(rustc_attrs)]
|
#![feature(rustc_attrs)]
|
||||||
#![feature(slice_get_slice)]
|
#![feature(slice_get_slice)]
|
||||||
#![feature(slice_rsplit)]
|
#![feature(slice_rsplit)]
|
||||||
|
@ -2394,9 +2394,10 @@ impl<T> Iterator for IntoIter<T> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
let exact = match self.ptr.offset_to(self.end) {
|
let exact = if mem::size_of::<T>() == 0 {
|
||||||
Some(x) => x as usize,
|
(self.end as usize).wrapping_sub(self.ptr as usize)
|
||||||
None => (self.end as usize).wrapping_sub(self.ptr as usize),
|
} else {
|
||||||
|
unsafe { self.end.offset_from(self.ptr) as usize }
|
||||||
};
|
};
|
||||||
(exact, Some(exact))
|
(exact, Some(exact))
|
||||||
}
|
}
|
||||||
|
@ -677,6 +677,7 @@ impl<T: ?Sized> *const T {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(offset_to)]
|
/// #![feature(offset_to)]
|
||||||
|
/// #![allow(deprecated)]
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let a = [0; 5];
|
/// let a = [0; 5];
|
||||||
@ -689,14 +690,15 @@ impl<T: ?Sized> *const T {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "offset_to", issue = "41079")]
|
#[unstable(feature = "offset_to", issue = "41079")]
|
||||||
|
#[rustc_deprecated(since = "1.27.0", reason = "Replaced by `wrapping_offset_from`, with the \
|
||||||
|
opposite argument order. If you're writing unsafe code, consider `offset_from`.")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn offset_to(self, other: *const T) -> Option<isize> where T: Sized {
|
pub fn offset_to(self, other: *const T) -> Option<isize> where T: Sized {
|
||||||
let size = mem::size_of::<T>();
|
let size = mem::size_of::<T>();
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let diff = (other as isize).wrapping_sub(self as isize);
|
Some(other.wrapping_offset_from(self))
|
||||||
Some(diff / size as isize)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1442,6 +1444,7 @@ impl<T: ?Sized> *mut T {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// #![feature(offset_to)]
|
/// #![feature(offset_to)]
|
||||||
|
/// #![allow(deprecated)]
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let mut a = [0; 5];
|
/// let mut a = [0; 5];
|
||||||
@ -1454,14 +1457,15 @@ impl<T: ?Sized> *mut T {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "offset_to", issue = "41079")]
|
#[unstable(feature = "offset_to", issue = "41079")]
|
||||||
|
#[rustc_deprecated(since = "1.27.0", reason = "Replaced by `wrapping_offset_from`, with the \
|
||||||
|
opposite argument order. If you're writing unsafe code, consider `offset_from`.")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn offset_to(self, other: *const T) -> Option<isize> where T: Sized {
|
pub fn offset_to(self, other: *const T) -> Option<isize> where T: Sized {
|
||||||
let size = mem::size_of::<T>();
|
let size = mem::size_of::<T>();
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let diff = (other as isize).wrapping_sub(self as isize);
|
Some(other.wrapping_offset_from(self))
|
||||||
Some(diff / size as isize)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1185,7 +1185,7 @@ macro_rules! iterator {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
let exact = ptrdistance(self.ptr, self.end);
|
let exact = unsafe { ptrdistance(self.ptr, self.end) };
|
||||||
(exact, Some(exact))
|
(exact, Some(exact))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1593,10 +1593,11 @@ unsafe impl<'a, T> TrustedLen for IterMut<'a, T> {}
|
|||||||
// Return the number of elements of `T` from `start` to `end`.
|
// Return the number of elements of `T` from `start` to `end`.
|
||||||
// Return the arithmetic difference if `T` is zero size.
|
// Return the arithmetic difference if `T` is zero size.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn ptrdistance<T>(start: *const T, end: *const T) -> usize {
|
unsafe fn ptrdistance<T>(start: *const T, end: *const T) -> usize {
|
||||||
match start.offset_to(end) {
|
if mem::size_of::<T>() == 0 {
|
||||||
Some(x) => x as usize,
|
(end as usize).wrapping_sub(start as usize)
|
||||||
None => (end as usize).wrapping_sub(start as usize),
|
} else {
|
||||||
|
end.offset_from(start) as usize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user