about summary refs log tree commit diff
path: root/library/core/src/ptr
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2023-05-06 02:00:45 -0700
committerScott McMurray <scottmcm@users.noreply.github.com>2023-05-10 13:01:43 -0700
commit15aa7fad7ec83db399c8a85c6b6777d0efc7bc53 (patch)
tree536d70cd09ce950ce744c6e9975a16e92d71a941 /library/core/src/ptr
parent50dff955a9367a4efc72b831549e368992807beb (diff)
downloadrust-15aa7fad7ec83db399c8a85c6b6777d0efc7bc53.tar.gz
rust-15aa7fad7ec83db399c8a85c6b6777d0efc7bc53.zip
Simplify the implementation of iterators over slices of ZSTs
Currently, slice iterators over ZSTs store `end = start.wrapping_byte_add(len)`.

That's slightly convenient for `is_empty`, but kinda annoying for pretty much everything else -- see bugs like 42789, for example.

This PR instead changes it to just `end = ptr::invalid(len)` instead.

That's easier to think about (IMHO, at least) as well as easier to represent.
Diffstat (limited to 'library/core/src/ptr')
-rw-r--r--library/core/src/ptr/non_null.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs
index 61fcdf58b4f..b492d2f07bc 100644
--- a/library/core/src/ptr/non_null.rs
+++ b/library/core/src/ptr/non_null.rs
@@ -449,6 +449,19 @@ impl<T: ?Sized> NonNull<T> {
         // SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
         unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
     }
+
+    /// See [`pointer::add`] for semantics and safety requirements.
+    #[inline]
+    pub(crate) const unsafe fn add(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 that would require wrapping the
+        // address space, which no legal objects are allowed to do.
+        // And the caller promised the `delta` is sound to add.
+        unsafe { NonNull { pointer: self.pointer.add(delta) } }
+    }
 }
 
 impl<T> NonNull<[T]> {