Skip to content

Commit 3fd3668

Browse files
authored
Rollup merge of rust-lang#103489 - WaffleLapkin:byte_offset_from_you, r=scottmcm
Make `pointer::byte_offset_from` more generic As suggested by rust-lang#96283 (comment) (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
2 parents d8a98b1 + 6279d09 commit 3fd3668

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

library/core/src/ptr/const_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ impl<T: ?Sized> *const T {
709709
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
710710
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
711711
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
712-
pub const unsafe fn byte_offset_from(self, origin: *const T) -> isize {
712+
pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
713713
// SAFETY: the caller must uphold the safety contract for `offset_from`.
714714
unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
715715
}

library/core/src/ptr/mut_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ impl<T: ?Sized> *mut T {
889889
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
890890
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
891891
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
892-
pub const unsafe fn byte_offset_from(self, origin: *const T) -> isize {
892+
pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
893893
// SAFETY: the caller must uphold the safety contract for `offset_from`.
894894
unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
895895
}

0 commit comments

Comments
 (0)