mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-22 02:00:00 +03:00
Rollup merge of #103489 - WaffleLapkin:byte_offset_from_you, r=scottmcm
Make `pointer::byte_offset_from` more generic As suggested by https://github.com/rust-lang/rust/issues/96283#issuecomment-1288792955 (cc ````@scottmcm),```` make `pointer::byte_offset_from` work on pointers of different types. `byte_offset_from` really doesn't care about pointer types, so this is totally fine and, for example, allows patterns like this: ```rust ptr::addr_of!(x.b).byte_offset_from(ptr::addr_of!(x)) ``` The only possible downside is that this removes the `T` == `U` hint to inference, but I don't think this matter much. I don't think there are a lot of cases where you'd want to use `byte_offset_from` with a pointer of unbounded type (and in such cases you can just specify the type). ````@rustbot```` label +T-libs-api
This commit is contained in:
@@ -709,7 +709,7 @@ pub fn mask(self, mask: usize) -> *const T {
|
||||
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
|
||||
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
|
||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
|
||||
pub const unsafe fn byte_offset_from(self, origin: *const T) -> isize {
|
||||
pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
|
||||
// SAFETY: the caller must uphold the safety contract for `offset_from`.
|
||||
unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
|
||||
}
|
||||
|
||||
@@ -889,7 +889,7 @@ pub const fn guaranteed_ne(self, other: *mut T) -> Option<bool>
|
||||
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
|
||||
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
|
||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
|
||||
pub const unsafe fn byte_offset_from(self, origin: *const T) -> isize {
|
||||
pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
|
||||
// SAFETY: the caller must uphold the safety contract for `offset_from`.
|
||||
unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user