diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2023-07-04 16:01:16 -0700 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2023-07-20 11:33:49 -0700 |
| commit | 34732e8560a3fea72bab9f2c9840897faa4f55b5 (patch) | |
| tree | 0c965eb3f105dba15e765274bade0805cbb67ff3 /library/core/src/ptr | |
| parent | 06a53ddc0bd3a50f9bcf2f7c373011dc7869f59f (diff) | |
| download | rust-34732e8560a3fea72bab9f2c9840897faa4f55b5.tar.gz rust-34732e8560a3fea72bab9f2c9840897faa4f55b5.zip | |
Get `!nonnull` metadata consistently in slice iterators, without needing `assume`s
Diffstat (limited to 'library/core/src/ptr')
| -rw-r--r-- | library/core/src/ptr/non_null.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index b492d2f07bc..a8074c8659b 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -462,6 +462,30 @@ impl<T: ?Sized> NonNull<T> { // And the caller promised the `delta` is sound to add. unsafe { NonNull { pointer: self.pointer.add(delta) } } } + + /// See [`pointer::sub`] for semantics and safety requirements. + #[inline] + pub(crate) const unsafe fn sub(self, delta: usize) -> Self + where + T: Sized, + { + // SAFETY: We require that the delta stays in-bounds of the object, and + // thus it cannot become null, as no legal objects can be allocated + // in such as way that the null address is part of them. + // And the caller promised the `delta` is sound to subtract. + unsafe { NonNull { pointer: self.pointer.sub(delta) } } + } + + /// See [`pointer::sub_ptr`] for semantics and safety requirements. + #[inline] + pub(crate) const unsafe fn sub_ptr(self, subtrahend: Self) -> usize + where + T: Sized, + { + // SAFETY: The caller promised that this is safe to do, and + // the non-nullness is irrelevant to the operation. + unsafe { self.pointer.sub_ptr(subtrahend.pointer) } + } } impl<T> NonNull<[T]> { |
