Repository: rust
Issue: Tracking Issue for `sub_ptr` (feature `ptr_sub_ptr`)
Labels: T-lang,T-libs-api,C-tracking-issue,disposition-merge,finished-final-comment-period

<!--
Thank you for creating a tracking issue!

Tracking issues are for tracking a feature from implementation to stabilization.

Make sure to include the relevant RFC for the feature if it has one.

If the new feature is small, it may be fine to skip the RFC process. In that
case, you can use use `issue = "none"` in your initial implementation PR. The
reviewer will ask you to open a tracking issue if they agree your feature can be
added without an RFC.
-->

Feature gate: `#![feature(ptr_sub_ptr)]` & `#![feature(const_ptr_sub_ptr)]` 

This is a tracking issue for the `<*const _>::sub_ptr` & `<*mut _>::sub_ptr` methods.

This is the produces-`usize` version of `offset_from`, the same way that `add` and `sub` are the takes-`usize` versions of `offset`.

It turns out that people almost always actually know which pointer is greater than which when doing this operation, and would rather a `usize` instead of an `isize`  -- every use of `offset_from` in the library was followed with `as usize` in practice.  So like how `.add(d)` greatly improved code compared to needing `.offset(d as isize)`, being able to use `ptr.sub_ptr(origin)` instead of `ptr.offset_from(origin) as usize` is also a major improvement.  And Miri can check the unsafety better, too, since if you get the order wrong it'll [detect that](https://github.com/rust-lang/rust/pull/95837/files#diff-10506a8d62761f810db4f83a4b6b90f29c7a35c97ebd922bfd1183f418fa70f9R83-R88), unlike happens with the `as usize` approach.

This also tracks the `const`ness of operations, though with #92980 stabilizing `offset_from` being `const` , this being `const` is likely uncontroversial.

<!--
Include a short description of the feature.
-->

### Public API

<!--
For most library features, it'd be useful to include a summarized version of the public API.
(E.g. just the public function signatures without their doc comments or implementation.)
-->

```rust
impl<T> *const T {
    pub const unsafe fn sub_ptr(self, origin: *const T) -> usize;
    pub const unsafe fn byte_sub_ptr<U>(self, origin: *const U) -> usize;
}

impl<T> *mut T {
    pub const unsafe fn sub_ptr(self, origin: *const T) -> usize;
    pub const unsafe fn byte_sub_ptr<U>(self, origin: *const U) -> usize;
}

impl <T> NonNull<T> {
    pub const unsafe fn sub_ptr(self, subtracted: NonNull<T>) -> usize;
    pub const unsafe fn byte_sub_ptr<U>(self, origin: NonNull<U>) -> usize;
}
```

### Steps / History

<!--
For larger features, more steps might be involved.
If the feature is changed later, please add those PRs here as well.
-->

- [x] Implementation: #95837
- [x] Move the `NonNull` method to this gate https://github.com/rust-lang/rust/pull/124498
- [ ] Final comment period (FCP)
- [ ] Stabilization PR

<!--
Once the feature has gone through a few release cycles and there are no
unresolved questions left, the feature might be ready for stabilization.

If this feature didn't go through the RFC process, a final comment period
(FCP) is always needed before stabilization. This works as follows:

A library API team member can kick off the stabilization process, at which point
the rfcbot will ask all the team members to verify they agree with
stabilization. Once enough members agree and there are no concerns, the final
comment period begins: this issue will be marked as such and will be listed
in the next This Week in Rust newsletter. If no blocking concerns are raised in
that period of 10 days, a stabilzation PR can be opened by anyone.
-->

### Unresolved Questions

<!--
Include any open questions that need to be answered before the feature can be
stabilised. If multiple (unrelated) big questions come up, it can be a good idea
to open a separate issue for each, to make it easier to keep track of the
discussions.

It's useful to link any relevant discussions and conclusions (whether on GitHub,
Zulip, or the internals forum) here.
-->

- [ ] This uses the terminology of subtraction to match the existing `add` and `sub` methods, but there might be another name that would be more evocative for people https://github.com/rust-lang/rust/pull/95837#issuecomment-1094134054