pub struct Ptr<'a, T, I>where
I: Invariants,
T: ?Sized,{ /* private fields */ }Expand description
A raw pointer with more restrictions.
Ptr<T> is similar to NonNull<T>, but it is more restrictive in the
following ways (note that these requirements only hold of non-zero-sized
referents):
- It must derive from a valid allocation.
- It must reference a byte range which is contained inside the
allocation from which it derives.
- As a consequence, the byte range it references must have a size
which does not overflow
isize.
- As a consequence, the byte range it references must have a size
which does not overflow
Depending on how Ptr is parameterized, it may have additional
invariants:
ptrconforms to the aliasing invariant ofI::Aliasing.ptrconforms to the alignment invariant ofI::Alignment.ptrconforms to the validity invariant ofI::Validity.
Ptr<'a, T> is covariant in 'a and invariant in T.
Implementations§
impl<'a, T> Ptr<'a, T, (Exclusive, Aligned, Valid)>where
T: 'a + ?Sized,
&'a mut T → Ptr<'a, T>
impl<'a, T, I> Ptr<'a, T, I>where
T: 'a + ?Sized,
I: Invariants<Alignment = Aligned, Validity = Valid>,
<I as Invariants>::Aliasing: Reference,
Ptr<'a, T> → &'a T
impl<'a, T> Ptr<'a, T, (Exclusive, Aligned, Valid)>where
T: 'a + ?Sized,
Ptr<'a, T> → &'a mut T
impl<'a, T, I> Ptr<'a, T, I>where
I: Invariants,
T: ?Sized,
Ptr<'a, T> → Ptr<'a, U>
impl<'a, T, I> Ptr<'a, T, I>where
I: Invariants,
Ptr<'a, T, (_, _, _)> → Ptr<'a, Unalign<T>, (_, Aligned, _)>
Source§impl<'a, T, I> Ptr<'a, T, I>where
I: Invariants<Validity = Valid>,
<I as Invariants>::Aliasing: Reference,
T: ?Sized,
impl<'a, T, I> Ptr<'a, T, I>where
I: Invariants<Validity = Valid>,
<I as Invariants>::Aliasing: Reference,
T: ?Sized,
Sourcepub fn read_unaligned<R>(self) -> Twhere
T: Copy + Read<<I as Invariants>::Aliasing, R>,
pub fn read_unaligned<R>(self) -> Twhere
T: Copy + Read<<I as Invariants>::Aliasing, R>,
Reads the referent.
Sourcepub fn unaligned_as_ref(self) -> &'a Twhere
T: Unaligned,
pub fn unaligned_as_ref(self) -> &'a Twhere
T: Unaligned,
Views the value as an aligned reference.
This is only available if T is [Unaligned].
impl<'a, I> Ptr<'a, [u8], I>where
I: Invariants<Validity = Valid>,
For caller convenience, these methods are generic over alignment
invariant. In practice, the referent is always well-aligned, because the
alignment of [u8] is 1.
Source§impl<'a, T, I> Ptr<'a, UnsafeCell<T>, I>where
T: 'a + ?Sized,
I: Invariants<Aliasing = Exclusive>,
impl<'a, T, I> Ptr<'a, UnsafeCell<T>, I>where
T: 'a + ?Sized,
I: Invariants<Aliasing = Exclusive>,
Sourcepub fn get_mut(self) -> Ptr<'a, T, I>
pub fn get_mut(self) -> Ptr<'a, T, I>
Converts this Ptr into a pointer to the underlying data.
This call borrows the UnsafeCell mutably (at compile-time) which
guarantees that we possess the only reference.
This is like UnsafeCell::get_mut, but for Ptr.
Trait Implementations§
Source§impl<'a, T, I> Clone for Ptr<'a, T, I>where
T: 'a + ?Sized,
I: Invariants<Aliasing = Shared>,
SAFETY: See the safety comment on Copy.
impl<'a, T, I> Clone for Ptr<'a, T, I>where
T: 'a + ?Sized,
I: Invariants<Aliasing = Shared>,
SAFETY: See the safety comment on Copy.
impl<'a, T, I> Copy for Ptr<'a, T, I>where
T: 'a + ?Sized,
I: Invariants<Aliasing = Shared>,
SAFETY: Shared pointers are safely Copy. Ptr’s other invariants
(besides aliasing) are unaffected by the number of references that exist
to Ptr’s referent. The notable cases are:
-
Alignment is a property of the referent type (
T) and the address, both of which are unchanged -
Let
S(T, V)be the set of bit values permitted to appear in the referent of aPtr<T, I: Invariants<Validity = V>>. Since this copy does not changeI::ValidityorT,S(T, I::Validity)is also unchanged.We are required to guarantee that the referents of the original
Ptrand of the copy (which, of course, are actually the same since they live in the same byte address range) both remain in the setS(T, I::Validity). Since this invariant holds on the originalPtr, it cannot be violated by the originalPtr, and thus the originalPtrcannot be used to violate this invariant on the copy. The inverse holds as well.