Coverage Report

Created: 2026-03-10 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zerocopy-0.8.42/src/pointer/ptr.rs
Line
Count
Source
1
// Copyright 2023 The Fuchsia Authors
2
//
3
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6
// This file may not be copied, modified, or distributed except according to
7
// those terms.
8
9
use core::{
10
    fmt::{Debug, Formatter},
11
    marker::PhantomData,
12
};
13
14
use crate::{
15
    pointer::{
16
        inner::PtrInner,
17
        invariant::*,
18
        transmute::{MutationCompatible, SizeEq, TransmuteFromPtr},
19
    },
20
    AlignmentError, CastError, CastType, KnownLayout, SizeError, TryFromBytes, ValidityError,
21
};
22
23
/// Module used to gate access to [`Ptr`]'s fields.
24
mod def {
25
    #[cfg(doc)]
26
    use super::super::invariant;
27
    use super::*;
28
29
    /// A raw pointer with more restrictions.
30
    ///
31
    /// `Ptr<T>` is similar to [`NonNull<T>`], but it is more restrictive in the
32
    /// following ways (note that these requirements only hold of non-zero-sized
33
    /// referents):
34
    /// - It must derive from a valid allocation.
35
    /// - It must reference a byte range which is contained inside the
36
    ///   allocation from which it derives.
37
    ///   - As a consequence, the byte range it references must have a size
38
    ///     which does not overflow `isize`.
39
    ///
40
    /// Depending on how `Ptr` is parameterized, it may have additional
41
    /// invariants:
42
    /// - `ptr` conforms to the aliasing invariant of
43
    ///   [`I::Aliasing`](invariant::Aliasing).
44
    /// - `ptr` conforms to the alignment invariant of
45
    ///   [`I::Alignment`](invariant::Alignment).
46
    /// - `ptr` conforms to the validity invariant of
47
    ///   [`I::Validity`](invariant::Validity).
48
    ///
49
    /// `Ptr<'a, T>` is [covariant] in `'a` and invariant in `T`.
50
    ///
51
    /// [`NonNull<T>`]: core::ptr::NonNull
52
    /// [covariant]: https://doc.rust-lang.org/reference/subtyping.html
53
    pub struct Ptr<'a, T, I>
54
    where
55
        T: ?Sized,
56
        I: Invariants,
57
    {
58
        /// # Invariants
59
        ///
60
        /// 0. `ptr` conforms to the aliasing invariant of
61
        ///    [`I::Aliasing`](invariant::Aliasing).
62
        /// 1. `ptr` conforms to the alignment invariant of
63
        ///    [`I::Alignment`](invariant::Alignment).
64
        /// 2. `ptr` conforms to the validity invariant of
65
        ///    [`I::Validity`](invariant::Validity).
66
        // SAFETY: `PtrInner<'a, T>` is covariant in `'a` and invariant in `T`.
67
        ptr: PtrInner<'a, T>,
68
        _invariants: PhantomData<I>,
69
    }
70
71
    impl<'a, T, I> Ptr<'a, T, I>
72
    where
73
        T: 'a + ?Sized,
74
        I: Invariants,
75
    {
76
        /// Constructs a new `Ptr` from a [`PtrInner`].
77
        ///
78
        /// # Safety
79
        ///
80
        /// The caller promises that:
81
        ///
82
        /// 0. `ptr` conforms to the aliasing invariant of
83
        ///    [`I::Aliasing`](invariant::Aliasing).
84
        /// 1. `ptr` conforms to the alignment invariant of
85
        ///    [`I::Alignment`](invariant::Alignment).
86
        /// 2. `ptr` conforms to the validity invariant of
87
        ///    [`I::Validity`](invariant::Validity).
88
0
        pub(crate) unsafe fn from_inner(ptr: PtrInner<'a, T>) -> Ptr<'a, T, I> {
89
            // SAFETY: The caller has promised to satisfy all safety invariants
90
            // of `Ptr`.
91
0
            Self { ptr, _invariants: PhantomData }
92
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::from_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, _>>::from_inner
93
94
        /// Converts this `Ptr<T>` to a [`PtrInner<T>`].
95
        ///
96
        /// Note that this method does not consume `self`. The caller should
97
        /// watch out for `unsafe` code which uses the returned value in a way
98
        /// that violates the safety invariants of `self`.
99
0
        pub(crate) fn as_inner(&self) -> PtrInner<'a, T> {
100
0
            self.ptr
101
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::as_inner
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, _>>::as_inner
102
    }
103
}
104
105
#[allow(unreachable_pub)] // This is a false positive on our MSRV toolchain.
106
pub use def::Ptr;
107
108
/// External trait implementations on [`Ptr`].
109
mod _external {
110
    use super::*;
111
112
    /// SAFETY: Shared pointers are safely `Copy`. `Ptr`'s other invariants
113
    /// (besides aliasing) are unaffected by the number of references that exist
114
    /// to `Ptr`'s referent. The notable cases are:
115
    /// - Alignment is a property of the referent type (`T`) and the address,
116
    ///   both of which are unchanged
117
    /// - Let `S(T, V)` be the set of bit values permitted to appear in the
118
    ///   referent of a `Ptr<T, I: Invariants<Validity = V>>`. Since this copy
119
    ///   does not change `I::Validity` or `T`, `S(T, I::Validity)` is also
120
    ///   unchanged.
121
    ///
122
    ///   We are required to guarantee that the referents of the original `Ptr`
123
    ///   and of the copy (which, of course, are actually the same since they
124
    ///   live in the same byte address range) both remain in the set `S(T,
125
    ///   I::Validity)`. Since this invariant holds on the original `Ptr`, it
126
    ///   cannot be violated by the original `Ptr`, and thus the original `Ptr`
127
    ///   cannot be used to violate this invariant on the copy. The inverse
128
    ///   holds as well.
129
    impl<'a, T, I> Copy for Ptr<'a, T, I>
130
    where
131
        T: 'a + ?Sized,
132
        I: Invariants<Aliasing = Shared>,
133
    {
134
    }
135
136
    /// SAFETY: See the safety comment on `Copy`.
137
    impl<'a, T, I> Clone for Ptr<'a, T, I>
138
    where
139
        T: 'a + ?Sized,
140
        I: Invariants<Aliasing = Shared>,
141
    {
142
        #[inline]
143
0
        fn clone(&self) -> Self {
144
0
            *self
145
0
        }
146
    }
147
148
    impl<'a, T, I> Debug for Ptr<'a, T, I>
149
    where
150
        T: 'a + ?Sized,
151
        I: Invariants,
152
    {
153
        #[inline]
154
0
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
155
0
            self.as_inner().as_non_null().fmt(f)
156
0
        }
157
    }
158
}
159
160
/// Methods for converting to and from `Ptr` and Rust's safe reference types.
161
mod _conversions {
162
    use super::*;
163
    use crate::pointer::cast::{CastExact, CastSized, IdCast};
164
165
    /// `&'a T` → `Ptr<'a, T>`
166
    impl<'a, T> Ptr<'a, T, (Shared, Aligned, Valid)>
167
    where
168
        T: 'a + ?Sized,
169
    {
170
        /// Constructs a `Ptr` from a shared reference.
171
        #[doc(hidden)]
172
        #[inline(always)]
173
0
        pub fn from_ref(ptr: &'a T) -> Self {
174
0
            let inner = PtrInner::from_ref(ptr);
175
            // SAFETY:
176
            // 0. `ptr`, by invariant on `&'a T`, conforms to the aliasing
177
            //    invariant of `Shared`.
178
            // 1. `ptr`, by invariant on `&'a T`, conforms to the alignment
179
            //    invariant of `Aligned`.
180
            // 2. `ptr`'s referent, by invariant on `&'a T`, is a bit-valid `T`.
181
            //    This satisfies the requirement that a `Ptr<T, (_, _, Valid)>`
182
            //    point to a bit-valid `T`. Even if `T` permits interior
183
            //    mutation, this invariant guarantees that the returned `Ptr`
184
            //    can only ever be used to modify the referent to store
185
            //    bit-valid `T`s, which ensures that the returned `Ptr` cannot
186
            //    be used to violate the soundness of the original `ptr: &'a T`
187
            //    or of any other references that may exist to the same
188
            //    referent.
189
0
            unsafe { Self::from_inner(inner) }
190
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_ref
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_ref
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_ref
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_ref
191
    }
192
193
    /// `&'a mut T` → `Ptr<'a, T>`
194
    impl<'a, T> Ptr<'a, T, (Exclusive, Aligned, Valid)>
195
    where
196
        T: 'a + ?Sized,
197
    {
198
        /// Constructs a `Ptr` from an exclusive reference.
199
        #[doc(hidden)]
200
        #[inline(always)]
201
0
        pub fn from_mut(ptr: &'a mut T) -> Self {
202
0
            let inner = PtrInner::from_mut(ptr);
203
            // SAFETY:
204
            // 0. `ptr`, by invariant on `&'a mut T`, conforms to the aliasing
205
            //    invariant of `Exclusive`.
206
            // 1. `ptr`, by invariant on `&'a mut T`, conforms to the alignment
207
            //    invariant of `Aligned`.
208
            // 2. `ptr`'s referent, by invariant on `&'a mut T`, is a bit-valid
209
            //    `T`. This satisfies the requirement that a `Ptr<T, (_, _,
210
            //    Valid)>` point to a bit-valid `T`. This invariant guarantees
211
            //    that the returned `Ptr` can only ever be used to modify the
212
            //    referent to store bit-valid `T`s, which ensures that the
213
            //    returned `Ptr` cannot be used to violate the soundness of the
214
            //    original `ptr: &'a mut T`.
215
0
            unsafe { Self::from_inner(inner) }
216
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_mut
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_mut
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_mut
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::from_mut
217
    }
218
219
    /// `Ptr<'a, T>` → `&'a T`
220
    impl<'a, T, I> Ptr<'a, T, I>
221
    where
222
        T: 'a + ?Sized,
223
        I: Invariants<Alignment = Aligned, Validity = Valid>,
224
        I::Aliasing: Reference,
225
    {
226
        /// Converts `self` to a shared reference.
227
        // This consumes `self`, not `&self`, because `self` is, logically, a
228
        // pointer. For `I::Aliasing = invariant::Shared`, `Self: Copy`, and so
229
        // this doesn't prevent the caller from still using the pointer after
230
        // calling `as_ref`.
231
        #[allow(clippy::wrong_self_convention)]
232
0
        pub(crate) fn as_ref(self) -> &'a T {
233
0
            let raw = self.as_inner().as_non_null();
234
            // SAFETY: `self` satisfies the `Aligned` invariant, so we know that
235
            // `raw` is validly-aligned for `T`.
236
            #[cfg(miri)]
237
            unsafe {
238
                crate::util::miri_promise_symbolic_alignment(
239
                    raw.as_ptr().cast(),
240
                    core::mem::align_of_val_raw(raw.as_ptr()),
241
                );
242
            }
243
            // SAFETY: This invocation of `NonNull::as_ref` satisfies its
244
            // documented safety preconditions:
245
            //
246
            // 1. The pointer is properly aligned. This is ensured by-contract
247
            //    on `Ptr`, because the `I::Alignment` is `Aligned`.
248
            //
249
            // 2. If the pointer's referent is not zero-sized, then the pointer
250
            //    must be “dereferenceable” in the sense defined in the module
251
            //    documentation; i.e.:
252
            //
253
            //    > The memory range of the given size starting at the pointer
254
            //    > must all be within the bounds of a single allocated object.
255
            //    > [2]
256
            //
257
            //   This is ensured by contract on all `PtrInner`s.
258
            //
259
            // 3. The pointer must point to a validly-initialized instance of
260
            //    `T`. This is ensured by-contract on `Ptr`, because the
261
            //    `I::Validity` is `Valid`.
262
            //
263
            // 4. You must enforce Rust’s aliasing rules. This is ensured by
264
            //    contract on `Ptr`, because `I::Aliasing: Reference`. Either it
265
            //    is `Shared` or `Exclusive`. If it is `Shared`, other
266
            //    references may not mutate the referent outside of
267
            //    `UnsafeCell`s.
268
            //
269
            // [1]: https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.as_ref
270
            // [2]: https://doc.rust-lang.org/std/ptr/index.html#safety
271
0
            unsafe { raw.as_ref() }
272
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::as_ref
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, _>>::as_ref
273
    }
274
275
    impl<'a, T, I> Ptr<'a, T, I>
276
    where
277
        T: 'a + ?Sized,
278
        I: Invariants,
279
        I::Aliasing: Reference,
280
    {
281
        /// Reborrows `self`, producing another `Ptr`.
282
        ///
283
        /// Since `self` is borrowed mutably, this prevents any methods from
284
        /// being called on `self` as long as the returned `Ptr` exists.
285
        #[doc(hidden)]
286
        #[inline]
287
        #[allow(clippy::needless_lifetimes)] // Allows us to name the lifetime in the safety comment below.
288
0
        pub fn reborrow<'b>(&'b mut self) -> Ptr<'b, T, I>
289
0
        where
290
0
            'a: 'b,
291
        {
292
            // SAFETY: The following all hold by invariant on `self`, and thus
293
            // hold of `ptr = self.as_inner()`:
294
            // 0. SEE BELOW.
295
            // 1. `ptr` conforms to the alignment invariant of
296
            //    [`I::Alignment`](invariant::Alignment).
297
            // 2. `ptr` conforms to the validity invariant of
298
            //    [`I::Validity`](invariant::Validity). `self` and the returned
299
            //    `Ptr` permit the same bit values in their referents since they
300
            //    have the same referent type (`T`) and the same validity
301
            //    (`I::Validity`). Thus, regardless of what mutation is
302
            //    permitted (`Exclusive` aliasing or `Shared`-aliased interior
303
            //    mutation), neither can be used to write a value to the
304
            //    referent which violates the other's validity invariant.
305
            //
306
            // For aliasing (0 above), since `I::Aliasing: Reference`,
307
            // there are two cases for `I::Aliasing`:
308
            // - For `invariant::Shared`: `'a` outlives `'b`, and so the
309
            //   returned `Ptr` does not permit accessing the referent any
310
            //   longer than is possible via `self`. For shared aliasing, it is
311
            //   sound for multiple `Ptr`s to exist simultaneously which
312
            //   reference the same memory, so creating a new one is not
313
            //   problematic.
314
            // - For `invariant::Exclusive`: Since `self` is `&'b mut` and we
315
            //   return a `Ptr` with lifetime `'b`, `self` is inaccessible to
316
            //   the caller for the lifetime `'b` - in other words, `self` is
317
            //   inaccessible to the caller as long as the returned `Ptr`
318
            //   exists. Since `self` is an exclusive `Ptr`, no other live
319
            //   references or `Ptr`s may exist which refer to the same memory
320
            //   while `self` is live. Thus, as long as the returned `Ptr`
321
            //   exists, no other references or `Ptr`s which refer to the same
322
            //   memory may be live.
323
0
            unsafe { Ptr::from_inner(self.as_inner()) }
324
0
        }
325
326
        /// Reborrows `self` as shared, producing another `Ptr` with `Shared`
327
        /// aliasing.
328
        ///
329
        /// Since `self` is borrowed mutably, this prevents any methods from
330
        /// being called on `self` as long as the returned `Ptr` exists.
331
        #[doc(hidden)]
332
        #[inline]
333
        #[allow(clippy::needless_lifetimes)] // Allows us to name the lifetime in the safety comment below.
334
0
        pub fn reborrow_shared<'b>(&'b mut self) -> Ptr<'b, T, (Shared, I::Alignment, I::Validity)>
335
0
        where
336
0
            'a: 'b,
337
        {
338
            // SAFETY: The following all hold by invariant on `self`, and thus
339
            // hold of `ptr = self.as_inner()`:
340
            // 0. SEE BELOW.
341
            // 1. `ptr` conforms to the alignment invariant of
342
            //    [`I::Alignment`](invariant::Alignment).
343
            // 2. `ptr` conforms to the validity invariant of
344
            //    [`I::Validity`](invariant::Validity). `self` and the returned
345
            //    `Ptr` permit the same bit values in their referents since they
346
            //    have the same referent type (`T`) and the same validity
347
            //    (`I::Validity`). Thus, regardless of what mutation is
348
            //    permitted (`Exclusive` aliasing or `Shared`-aliased interior
349
            //    mutation), neither can be used to write a value to the
350
            //    referent which violates the other's validity invariant.
351
            //
352
            // For aliasing (0 above), since `I::Aliasing: Reference`,
353
            // there are two cases for `I::Aliasing`:
354
            // - For `invariant::Shared`: `'a` outlives `'b`, and so the
355
            //   returned `Ptr` does not permit accessing the referent any
356
            //   longer than is possible via `self`. For shared aliasing, it is
357
            //   sound for multiple `Ptr`s to exist simultaneously which
358
            //   reference the same memory, so creating a new one is not
359
            //   problematic.
360
            // - For `invariant::Exclusive`: Since `self` is `&'b mut` and we
361
            //   return a `Ptr` with lifetime `'b`, `self` is inaccessible to
362
            //   the caller for the lifetime `'b` - in other words, `self` is
363
            //   inaccessible to the caller as long as the returned `Ptr`
364
            //   exists. Since `self` is an exclusive `Ptr`, no other live
365
            //   references or `Ptr`s may exist which refer to the same memory
366
            //   while `self` is live. Thus, as long as the returned `Ptr`
367
            //   exists, no other references or `Ptr`s which refer to the same
368
            //   memory may be live.
369
0
            unsafe { Ptr::from_inner(self.as_inner()) }
370
0
        }
371
    }
372
373
    /// `Ptr<'a, T>` → `&'a mut T`
374
    impl<'a, T> Ptr<'a, T, (Exclusive, Aligned, Valid)>
375
    where
376
        T: 'a + ?Sized,
377
    {
378
        /// Converts `self` to a mutable reference.
379
        #[allow(clippy::wrong_self_convention)]
380
0
        pub(crate) fn as_mut(self) -> &'a mut T {
381
0
            let mut raw = self.as_inner().as_non_null();
382
            // SAFETY: `self` satisfies the `Aligned` invariant, so we know that
383
            // `raw` is validly-aligned for `T`.
384
            #[cfg(miri)]
385
            unsafe {
386
                crate::util::miri_promise_symbolic_alignment(
387
                    raw.as_ptr().cast(),
388
                    core::mem::align_of_val_raw(raw.as_ptr()),
389
                );
390
            }
391
            // SAFETY: This invocation of `NonNull::as_mut` satisfies its
392
            // documented safety preconditions:
393
            //
394
            // 1. The pointer is properly aligned. This is ensured by-contract
395
            //    on `Ptr`, because the `ALIGNMENT_INVARIANT` is `Aligned`.
396
            //
397
            // 2. If the pointer's referent is not zero-sized, then the pointer
398
            //    must be “dereferenceable” in the sense defined in the module
399
            //    documentation; i.e.:
400
            //
401
            //    > The memory range of the given size starting at the pointer
402
            //    > must all be within the bounds of a single allocated object.
403
            //    > [2]
404
            //
405
            //   This is ensured by contract on all `PtrInner`s.
406
            //
407
            // 3. The pointer must point to a validly-initialized instance of
408
            //    `T`. This is ensured by-contract on `Ptr`, because the
409
            //    validity invariant is `Valid`.
410
            //
411
            // 4. You must enforce Rust’s aliasing rules. This is ensured by
412
            //    contract on `Ptr`, because the `ALIASING_INVARIANT` is
413
            //    `Exclusive`.
414
            //
415
            // [1]: https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.as_mut
416
            // [2]: https://doc.rust-lang.org/std/ptr/index.html#safety
417
0
            unsafe { raw.as_mut() }
418
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::as_mut
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::as_mut
419
    }
420
421
    /// `Ptr<'a, T>` → `Ptr<'a, U>`
422
    impl<'a, T: ?Sized, I> Ptr<'a, T, I>
423
    where
424
        I: Invariants,
425
    {
426
        #[must_use]
427
        #[inline(always)]
428
0
        pub fn transmute<U, V, R>(self) -> Ptr<'a, U, (I::Aliasing, Unaligned, V)>
429
0
        where
430
0
            V: Validity,
431
0
            U: TransmuteFromPtr<T, I::Aliasing, I::Validity, V, <U as SizeEq<T>>::CastFrom, R>
432
0
                + SizeEq<T>
433
0
                + ?Sized,
434
        {
435
0
            self.transmute_with::<U, V, <U as SizeEq<T>>::CastFrom, R>()
436
0
        }
437
438
0
        pub(crate) fn transmute_with<U, V, C, R>(self) -> Ptr<'a, U, (I::Aliasing, Unaligned, V)>
439
0
        where
440
0
            V: Validity,
441
0
            U: TransmuteFromPtr<T, I::Aliasing, I::Validity, V, C, R> + ?Sized,
442
0
            C: CastExact<T, U>,
443
        {
444
            // SAFETY:
445
            // - By `C: CastExact`, `C` preserves referent address, and so we
446
            //   don't need to consider projections in the following safety
447
            //   arguments.
448
            // - If aliasing is `Shared`, then by `U: TransmuteFromPtr<T>`, at
449
            //   least one of the following holds:
450
            //   - `T: Immutable` and `U: Immutable`, in which case it is
451
            //     trivially sound for shared code to operate on a `&T` and `&U`
452
            //     at the same time, as neither can perform interior mutation
453
            //   - It is directly guaranteed that it is sound for shared code to
454
            //     operate on these references simultaneously
455
            // - By `U: TransmuteFromPtr<T, I::Aliasing, I::Validity, C, V>`, it
456
            //   is sound to perform this transmute using `C`.
457
0
            unsafe { self.project_transmute_unchecked::<_, _, C>() }
458
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>>::transmute_with::<[u16], zerocopy::pointer::invariant::Initialized, zerocopy::layout::cast_from::CastFrom<[u16]>, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseImmutable))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::transmute_with::<[half::binary16::f16], zerocopy::pointer::invariant::Initialized, zerocopy::pointer::cast::IdCast, zerocopy::pointer::invariant::BecauseImmutable>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::transmute_with::<[u16], zerocopy::pointer::invariant::Valid, zerocopy::pointer::cast::IdCast, zerocopy::pointer::invariant::BecauseImmutable>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>>::transmute_with::<[u16], zerocopy::pointer::invariant::Initialized, zerocopy::layout::cast_from::CastFrom<[u16]>, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::transmute_with::<[half::binary16::f16], zerocopy::pointer::invariant::Initialized, zerocopy::pointer::cast::IdCast, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::transmute_with::<[u16], zerocopy::pointer::invariant::Valid, zerocopy::pointer::cast::IdCast, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, _>>::transmute_with::<_, _, _, _>
459
460
        #[doc(hidden)]
461
        #[inline(always)]
462
        #[must_use]
463
0
        pub fn recall_validity<V, R>(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, V)>
464
0
        where
465
0
            V: Validity,
466
0
            T: TransmuteFromPtr<T, I::Aliasing, I::Validity, V, IdCast, R>,
467
        {
468
0
            let ptr = self.transmute_with::<T, V, IdCast, R>();
469
            // SAFETY: `self` and `ptr` have the same address and referent type.
470
            // Therefore, if `self` satisfies `I::Alignment`, then so does
471
            // `ptr`.
472
0
            unsafe { ptr.assume_alignment::<I::Alignment>() }
473
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::recall_validity::<zerocopy::pointer::invariant::Initialized, zerocopy::pointer::invariant::BecauseImmutable>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::recall_validity::<zerocopy::pointer::invariant::Initialized, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::recall_validity::<zerocopy::pointer::invariant::Valid, zerocopy::pointer::invariant::BecauseImmutable>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::recall_validity::<zerocopy::pointer::invariant::Valid, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::recall_validity::<zerocopy::pointer::invariant::Initialized, zerocopy::pointer::invariant::BecauseImmutable>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::recall_validity::<zerocopy::pointer::invariant::Initialized, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::recall_validity::<zerocopy::pointer::invariant::Valid, zerocopy::pointer::invariant::BecauseImmutable>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::recall_validity::<zerocopy::pointer::invariant::Valid, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::recall_validity::<zerocopy::pointer::invariant::Initialized, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::recall_validity::<zerocopy::pointer::invariant::Valid, (zerocopy::pointer::transmute::BecauseMutationCompatible, (zerocopy::pointer::transmute::BecauseRead, zerocopy::pointer::invariant::BecauseExclusive))>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, _>>::recall_validity::<_, _>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::recall_validity::<zerocopy::pointer::invariant::Initialized, zerocopy::pointer::invariant::BecauseImmutable>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::recall_validity::<zerocopy::pointer::invariant::Valid, zerocopy::pointer::invariant::BecauseImmutable>
474
475
        /// Projects and/or transmutes to a different (unsized) referent type
476
        /// without checking interior mutability.
477
        ///
478
        /// Callers should prefer [`cast`] or [`project`] where possible.
479
        ///
480
        /// [`cast`]: Ptr::cast
481
        /// [`project`]: Ptr::project
482
        ///
483
        /// # Safety
484
        ///
485
        /// The caller promises that:
486
        /// - If `I::Aliasing` is [`Shared`], it must not be possible for safe
487
        ///   code, operating on a `&T` and `&U`, with the referents of `self`
488
        ///   and `self.project_transmute_unchecked()`, respectively, to cause
489
        ///   undefined behavior.
490
        /// - It is sound to project and/or transmute a pointer of type `T` with
491
        ///   aliasing `I::Aliasing` and validity `I::Validity` to a pointer of
492
        ///   type `U` with aliasing `I::Aliasing` and validity `V`. This is a
493
        ///   subtle soundness requirement that is a function of `T`, `U`,
494
        ///   `I::Aliasing`, `I::Validity`, and `V`, and may depend upon the
495
        ///   presence, absence, or specific location of `UnsafeCell`s in `T`
496
        ///   and/or `U`, and on whether interior mutation is ever permitted via
497
        ///   those `UnsafeCell`s. See [`Validity`] for more details.
498
        #[doc(hidden)]
499
        #[inline(always)]
500
        #[must_use]
501
0
        pub unsafe fn project_transmute_unchecked<U: ?Sized, V, P>(
502
0
            self,
503
0
        ) -> Ptr<'a, U, (I::Aliasing, Unaligned, V)>
504
0
        where
505
0
            V: Validity,
506
0
            P: crate::pointer::cast::Project<T, U>,
507
        {
508
0
            let ptr = self.as_inner().project::<_, P>();
509
510
            // SAFETY:
511
            //
512
            // The following safety arguments rely on the fact that `P: Project`
513
            // guarantees that `P` is a referent-preserving or -shrinking
514
            // projection. Thus, `ptr` addresses a subset of the bytes of
515
            // `*self`, and so certain properties that hold of `*self` also hold
516
            // of `*ptr`.
517
            //
518
            // 0. `ptr` conforms to the aliasing invariant of `I::Aliasing`:
519
            //    - `Exclusive`: `self` is the only `Ptr` or reference which is
520
            //      permitted to read or modify the referent for the lifetime
521
            //      `'a`. Since we consume `self` by value, the returned pointer
522
            //      remains the only `Ptr` or reference which is permitted to
523
            //      read or modify the referent for the lifetime `'a`.
524
            //    - `Shared`: Since `self` has aliasing `Shared`, we know that
525
            //      no other code may mutate the referent during the lifetime
526
            //      `'a`, except via `UnsafeCell`s, and except as permitted by
527
            //      `T`'s library safety invariants. The caller promises that
528
            //      any safe operations which can be permitted on a `&T` and a
529
            //      `&U` simultaneously must be sound. Thus, no operations on a
530
            //      `&U` could violate `&T`'s library safety invariants, and
531
            //      vice-versa. Since any mutation via shared references outside
532
            //      of `UnsafeCell`s is unsound, this must be impossible using
533
            //      `&T` and `&U`.
534
            //    - `Inaccessible`: There are no restrictions we need to uphold.
535
            // 1. `ptr` trivially satisfies the alignment invariant `Unaligned`.
536
            // 2. The caller promises that the returned pointer satisfies the
537
            //    validity invariant `V` with respect to its referent type, `U`.
538
0
            unsafe { Ptr::from_inner(ptr) }
539
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>>::project_transmute_unchecked::<[u16], zerocopy::pointer::invariant::Initialized, zerocopy::layout::cast_from::CastFrom<[u16]>>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::project_transmute_unchecked::<[half::binary16::f16], zerocopy::pointer::invariant::Initialized, zerocopy::pointer::cast::IdCast>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::project_transmute_unchecked::<[u16], zerocopy::pointer::invariant::Valid, zerocopy::pointer::cast::IdCast>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>>::project_transmute_unchecked::<[u16], zerocopy::pointer::invariant::Initialized, zerocopy::layout::cast_from::CastFrom<[u16]>>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>>::project_transmute_unchecked::<[half::binary16::f16], zerocopy::pointer::invariant::Initialized, zerocopy::pointer::cast::IdCast>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::project_transmute_unchecked::<[u16], zerocopy::pointer::invariant::Valid, zerocopy::pointer::cast::IdCast>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, _>>::project_transmute_unchecked::<_, _, _>
540
    }
541
542
    /// `Ptr<'a, T, (_, _, _)>` → `Ptr<'a, Unalign<T>, (_, Aligned, _)>`
543
    impl<'a, T, I> Ptr<'a, T, I>
544
    where
545
        I: Invariants,
546
    {
547
        /// Converts a `Ptr` an unaligned `T` into a `Ptr` to an aligned
548
        /// `Unalign<T>`.
549
0
        pub(crate) fn into_unalign(
550
0
            self,
551
0
        ) -> Ptr<'a, crate::Unalign<T>, (I::Aliasing, Aligned, I::Validity)> {
552
            // FIXME(#1359): This should be a `transmute_with` call.
553
            // Unfortunately, to avoid blanket impl conflicts, we only implement
554
            // `TransmuteFrom<T>` for `Unalign<T>` (and vice versa) specifically
555
            // for `Valid` validity, not for all validity types.
556
557
            // SAFETY:
558
            // - By `CastSized: Cast`, `CastSized` preserves referent address,
559
            //   and so we don't need to consider projections in the following
560
            //   safety arguments.
561
            // - Since `Unalign<T>` has the same layout as `T`, the returned
562
            //   pointer refers to `UnsafeCell`s at the same locations as
563
            //   `self`.
564
            // - `Unalign<T>` promises to have the same bit validity as `T`. By
565
            //   invariant on `Validity`, the set of bit patterns allowed in the
566
            //   referent of a `Ptr<X, (_, _, V)>` is only a function of the
567
            //   validity of `X` and of `V`. Thus, the set of bit patterns
568
            //   allowed in the referent of a `Ptr<T, (_, _, I::Validity)>` is
569
            //   the same as the set of bit patterns allowed in the referent of
570
            //   a `Ptr<Unalign<T>, (_, _, I::Validity)>`. As a result, `self`
571
            //   and the returned `Ptr` permit the same set of bit patterns in
572
            //   their referents, and so neither can be used to violate the
573
            //   validity of the other.
574
0
            let ptr = unsafe { self.project_transmute_unchecked::<_, _, CastSized>() };
575
0
            ptr.bikeshed_recall_aligned()
576
0
        }
577
    }
578
579
    impl<'a, T, I> Ptr<'a, T, I>
580
    where
581
        T: ?Sized,
582
        I: Invariants<Validity = Valid>,
583
        I::Aliasing: Reference,
584
    {
585
        /// Reads the referent.
586
        #[must_use]
587
        #[inline(always)]
588
0
        pub fn read<R>(self) -> T
589
0
        where
590
0
            T: Copy,
591
0
            T: Read<I::Aliasing, R>,
592
        {
593
0
            <I::Alignment as Alignment>::read(self)
594
0
        }
595
596
        /// Views the value as an aligned reference.
597
        ///
598
        /// This is only available if `T` is [`Unaligned`].
599
        #[must_use]
600
        #[inline]
601
0
        pub fn unaligned_as_ref(self) -> &'a T
602
0
        where
603
0
            T: crate::Unaligned,
604
        {
605
0
            self.bikeshed_recall_aligned().as_ref()
606
0
        }
607
    }
608
}
609
610
/// State transitions between invariants.
611
mod _transitions {
612
    use super::*;
613
    use crate::{
614
        pointer::{cast::IdCast, transmute::TryTransmuteFromPtr},
615
        ReadOnly,
616
    };
617
618
    impl<'a, T, I> Ptr<'a, T, I>
619
    where
620
        T: 'a + ?Sized,
621
        I: Invariants,
622
    {
623
        /// Assumes that `self` satisfies the invariants `H`.
624
        ///
625
        /// # Safety
626
        ///
627
        /// The caller promises that `self` satisfies the invariants `H`.
628
0
        unsafe fn assume_invariants<H: Invariants>(self) -> Ptr<'a, T, H> {
629
            // SAFETY: The caller has promised to satisfy all parameterized
630
            // invariants of `Ptr`. `Ptr`'s other invariants are satisfied
631
            // by-contract by the source `Ptr`.
632
0
            unsafe { Ptr::from_inner(self.as_inner()) }
633
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::assume_invariants::<(zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::assume_invariants::<(zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::assume_invariants::<(zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::assume_invariants::<(zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Initialized)>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::assume_invariants::<(zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::assume_invariants::<(zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Aligned, zerocopy::pointer::invariant::Valid)>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, _>>::assume_invariants::<_>
634
635
        /// Helps the type system unify two distinct invariant types which are
636
        /// actually the same.
637
0
        pub(crate) fn unify_invariants<
638
0
            H: Invariants<Aliasing = I::Aliasing, Alignment = I::Alignment, Validity = I::Validity>,
639
0
        >(
640
0
            self,
641
0
        ) -> Ptr<'a, T, H> {
642
            // SAFETY: The associated type bounds on `H` ensure that the
643
            // invariants are unchanged.
644
0
            unsafe { self.assume_invariants::<H>() }
645
0
        }
646
647
        /// Assumes that `self`'s referent is validly-aligned for `T` if
648
        /// required by `A`.
649
        ///
650
        /// # Safety
651
        ///
652
        /// The caller promises that `self`'s referent conforms to the alignment
653
        /// invariant of `T` if required by `A`.
654
        #[inline]
655
0
        pub(crate) unsafe fn assume_alignment<A: Alignment>(
656
0
            self,
657
0
        ) -> Ptr<'a, T, (I::Aliasing, A, I::Validity)> {
658
            // SAFETY: The caller promises that `self`'s referent is
659
            // well-aligned for `T` if required by `A` .
660
0
            unsafe { self.assume_invariants() }
661
0
        }
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::assume_alignment::<zerocopy::pointer::invariant::Aligned>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::assume_alignment::<zerocopy::pointer::invariant::Unaligned>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Shared, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::assume_alignment::<zerocopy::pointer::invariant::Aligned>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[half::binary16::f16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Initialized)>>::assume_alignment::<zerocopy::pointer::invariant::Aligned>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::assume_alignment::<zerocopy::pointer::invariant::Unaligned>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<[u16], (zerocopy::pointer::invariant::Exclusive, zerocopy::pointer::invariant::Unaligned, zerocopy::pointer::invariant::Valid)>>::assume_alignment::<zerocopy::pointer::invariant::Aligned>
Unexecuted instantiation: <zerocopy::pointer::ptr::def::Ptr<_, _>>::assume_alignment::<_>
662
663
        /// Checks the `self`'s alignment at runtime, returning an aligned `Ptr`
664
        /// on success.
665
0
        pub(crate) fn try_into_aligned(
666
0
            self,
667
0
        ) -> Result<Ptr<'a, T, (I::Aliasing, Aligned, I::Validity)>, AlignmentError<Self, T>>
668
0
        where
669
0
            T: Sized,
670
        {
671
0
            if let Err(err) =
672
0
                crate::util::validate_aligned_to::<_, T>(self.as_inner().as_non_null())
673
            {
674
0
                return Err(err.with_src(self));
675
0
            }
676
677
            // SAFETY: We just checked the alignment.
678
0
            Ok(unsafe { self.assume_alignment::<Aligned>() })
679
0
        }
680
681
        /// Recalls that `self`'s referent is validly-aligned for `T`.
682
        #[inline]
683
        // FIXME(#859): Reconsider the name of this method before making it
684
        // public.
685
0
        pub(crate) fn bikeshed_recall_aligned(
686
0
            self,
687
0
        ) -> Ptr<'a, T, (I::Aliasing, Aligned, I::Validity)>
688
0
        where
689
0
            T: crate::Unaligned,
690
        {
691
            // SAFETY: The bound `T: Unaligned` ensures that `T` has no
692
            // non-trivial alignment requirement.
693
0
            unsafe { self.assume_alignment::<Aligned>() }
694
0
        }
695
696
        /// Assumes that `self`'s referent conforms to the validity requirement
697
        /// of `V`.
698
        ///
699
        /// # Safety
700
        ///
701
        /// The caller promises that `self`'s referent conforms to the validity
702
        /// requirement of `V`.
703
        #[doc(hidden)]
704
        #[must_use]
705
        #[inline]
706
0
        pub unsafe fn assume_validity<V: Validity>(
707
0
            self,
708
0
        ) -> Ptr<'a, T, (I::Aliasing, I::Alignment, V)> {
709
            // SAFETY: The caller promises that `self`'s referent conforms to
710
            // the validity requirement of `V`.
711
0
            unsafe { self.assume_invariants() }
712
0
        }
713
714
        /// A shorthand for `self.assume_validity<invariant::Initialized>()`.
715
        ///
716
        /// # Safety
717
        ///
718
        /// The caller promises to uphold the safety preconditions of
719
        /// `self.assume_validity<invariant::Initialized>()`.
720
        #[doc(hidden)]
721
        #[must_use]
722
        #[inline]
723
0
        pub unsafe fn assume_initialized(
724
0
            self,
725
0
        ) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Initialized)> {
726
            // SAFETY: The caller has promised to uphold the safety
727
            // preconditions.
728
0
            unsafe { self.assume_validity::<Initialized>() }
729
0
        }
730
731
        /// A shorthand for `self.assume_validity<Valid>()`.
732
        ///
733
        /// # Safety
734
        ///
735
        /// The caller promises to uphold the safety preconditions of
736
        /// `self.assume_validity<Valid>()`.
737
        #[doc(hidden)]
738
        #[must_use]
739
        #[inline]
740
0
        pub unsafe fn assume_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)> {
741
            // SAFETY: The caller has promised to uphold the safety
742
            // preconditions.
743
0
            unsafe { self.assume_validity::<Valid>() }
744
0
        }
745
746
        /// Checks that `self`'s referent is validly initialized for `T`,
747
        /// returning a `Ptr` with `Valid` on success.
748
        ///
749
        /// # Panics
750
        ///
751
        /// This method will panic if
752
        /// [`T::is_bit_valid`][TryFromBytes::is_bit_valid] panics.
753
        ///
754
        /// # Safety
755
        ///
756
        /// On error, unsafe code may rely on this method's returned
757
        /// `ValidityError` containing `self`.
758
        #[inline]
759
0
        pub(crate) fn try_into_valid<R, S>(
760
0
            mut self,
761
0
        ) -> Result<Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)>, ValidityError<Self, T>>
762
0
        where
763
0
            T: TryFromBytes
764
0
                + Read<I::Aliasing, R>
765
0
                + TryTransmuteFromPtr<T, I::Aliasing, I::Validity, Valid, IdCast, S>,
766
0
            ReadOnly<T>: Read<I::Aliasing, R>,
767
0
            I::Aliasing: Reference,
768
0
            I: Invariants<Validity = Initialized>,
769
        {
770
            // This call may panic. If that happens, it doesn't cause any
771
            // soundness issues, as we have not generated any invalid state
772
            // which we need to fix before returning.
773
0
            if T::is_bit_valid(self.reborrow().transmute::<_, _, _>().reborrow_shared()) {
774
                // SAFETY: If `T::is_bit_valid`, code may assume that `self`
775
                // contains a bit-valid instance of `T`. By `T:
776
                // TryTransmuteFromPtr<T, I::Aliasing, I::Validity, Valid>`, so
777
                // long as `self`'s referent conforms to the `Valid` validity
778
                // for `T` (which we just confirmed), then this transmute is
779
                // sound.
780
0
                Ok(unsafe { self.assume_valid() })
781
            } else {
782
0
                Err(ValidityError::new(self))
783
            }
784
0
        }
785
786
        /// Forgets that `self`'s referent is validly-aligned for `T`.
787
        #[doc(hidden)]
788
        #[must_use]
789
        #[inline]
790
0
        pub fn forget_aligned(self) -> Ptr<'a, T, (I::Aliasing, Unaligned, I::Validity)> {
791
            // SAFETY: `Unaligned` is less restrictive than `Aligned`.
792
0
            unsafe { self.assume_invariants() }
793
0
        }
794
    }
795
}
796
797
/// Casts of the referent type.
798
pub(crate) use _casts::TryWithError;
799
mod _casts {
800
    use core::cell::UnsafeCell;
801
802
    use super::*;
803
    use crate::{
804
        pointer::cast::{AsBytesCast, Cast},
805
        HasTag, ProjectField,
806
    };
807
808
    impl<'a, T, I> Ptr<'a, T, I>
809
    where
810
        T: 'a + ?Sized,
811
        I: Invariants,
812
    {
813
        /// Casts to a different referent type without checking interior
814
        /// mutability.
815
        ///
816
        /// Callers should prefer [`cast`][Ptr::cast] where possible.
817
        ///
818
        /// # Safety
819
        ///
820
        /// If `I::Aliasing` is [`Shared`], it must not be possible for safe
821
        /// code, operating on a `&T` and `&U` with the same referent
822
        /// simultaneously, to cause undefined behavior.
823
        #[doc(hidden)]
824
        #[inline(always)]
825
        #[must_use]
826
0
        pub unsafe fn cast_unchecked<U, C: Cast<T, U>>(
827
0
            self,
828
0
        ) -> Ptr<'a, U, (I::Aliasing, Unaligned, I::Validity)>
829
0
        where
830
0
            U: 'a + CastableFrom<T, I::Validity, I::Validity> + ?Sized,
831
        {
832
            // SAFETY:
833
            // - By `C: Cast`, `C` preserves the address of the referent.
834
            // - If `I::Aliasing` is [`Shared`], the caller promises that it
835
            //   is not possible for safe code, operating on a `&T` and `&U`
836
            //   with the same referent simultaneously, to cause undefined
837
            //   behavior.
838
            // - By `U: CastableFrom<T, I::Validity, I::Validity>`,
839
            //   `I::Validity` is either `Uninit` or `Initialized`. In both
840
            //   cases, the bit validity `I::Validity` has the same semantics
841
            //   regardless of referent type. In other words, the set of allowed
842
            //   referent values for `Ptr<T, (_, _, I::Validity)>` and `Ptr<U,
843
            //   (_, _, I::Validity)>` are identical. As a consequence, neither
844
            //   `self` nor the returned `Ptr` can be used to write values which
845
            //   are invalid for the other.
846
0
            unsafe { self.project_transmute_unchecked::<_, _, C>() }
847
0
        }
848
849
        /// Casts to a different referent type.
850
        #[doc(hidden)]
851
        #[inline(always)]
852
        #[must_use]
853
0
        pub fn cast<U, C, R>(self) -> Ptr<'a, U, (I::Aliasing, Unaligned, I::Validity)>
854
0
        where
855
0
            T: MutationCompatible<U, I::Aliasing, I::Validity, I::Validity, R>,
856
0
            U: 'a + ?Sized + CastableFrom<T, I::Validity, I::Validity>,
857
0
            C: Cast<T, U>,
858
        {
859
            // SAFETY: Because `T: MutationCompatible<U, I::Aliasing, R>`, one
860
            // of the following holds:
861
            // - `T: Read<I::Aliasing>` and `U: Read<I::Aliasing>`, in which
862
            //   case one of the following holds:
863
            //   - `I::Aliasing` is `Exclusive`
864
            //   - `T` and `U` are both `Immutable`
865
            // - It is sound for safe code to operate on `&T` and `&U` with the
866
            //   same referent simultaneously.
867
0
            unsafe { self.cast_unchecked::<_, C>() }
868
0
        }
869
870
        #[inline(always)]
871
0
        pub fn project<F, const VARIANT_ID: i128, const FIELD_ID: i128>(
872
0
            mut self,
873
0
        ) -> Result<Ptr<'a, T::Type, T::Invariants>, T::Error>
874
0
        where
875
0
            T: ProjectField<F, I, VARIANT_ID, FIELD_ID>,
876
0
            I::Aliasing: Reference,
877
        {
878
            use crate::pointer::cast::Projection;
879
0
            match T::is_projectable(self.reborrow().project_tag()) {
880
                Ok(()) => {
881
0
                    let inner = self.as_inner();
882
0
                    let projected = inner.project::<_, Projection<F, VARIANT_ID, FIELD_ID>>();
883
                    // SAFETY: By `T: ProjectField<F, I, VARIANT_ID, FIELD_ID>`,
884
                    // for `self: Ptr<'_, T, I>` such that `T::is_projectable`
885
                    // (which we've verified in this match arm),
886
                    // `T::project(self.as_inner())` conforms to
887
                    // `T::Invariants`. The `projected` pointer satisfies these
888
                    // invariants because it is produced by way of an
889
                    // abstraction that is equivalent to
890
                    // `T::project(ptr.as_inner())`: by invariant on
891
                    // `PtrInner::project`, `projected` is guaranteed to address
892
                    // the subset of the bytes of `inner`'s referent addressed
893
                    // by `Projection::project(inner)`, and by invariant on
894
                    // `Projection`, `Projection::project` is implemented by
895
                    // delegating to an implementation of `HasField::project`.
896
0
                    Ok(unsafe { Ptr::from_inner(projected) })
897
                }
898
0
                Err(err) => Err(err),
899
            }
900
0
        }
901
902
        #[must_use]
903
        #[inline(always)]
904
0
        pub(crate) fn project_tag(self) -> Ptr<'a, T::Tag, I>
905
0
        where
906
0
            T: HasTag,
907
        {
908
            // SAFETY: By invariant on `Self::ProjectToTag`, this is a sound
909
            // projection.
910
0
            let tag = unsafe { self.project_transmute_unchecked::<_, _, T::ProjectToTag>() };
911
            // SAFETY: By invariant on `Self::ProjectToTag`, the projected
912
            // pointer has the same alignment as `ptr`.
913
0
            let tag = unsafe { tag.assume_alignment() };
914
0
            tag.unify_invariants()
915
0
        }
916
917
        /// Attempts to transform the pointer, restoring the original on
918
        /// failure.
919
        ///
920
        /// # Safety
921
        ///
922
        /// If `I::Aliasing != Shared`, then if `f` returns `Err(err)`, no copy
923
        /// of `f`'s argument must exist outside of `err`.
924
        #[inline(always)]
925
0
        pub(crate) unsafe fn try_with_unchecked<U, J, E, F>(
926
0
            self,
927
0
            f: F,
928
0
        ) -> Result<Ptr<'a, U, J>, E::Mapped>
929
0
        where
930
0
            U: 'a + ?Sized,
931
0
            J: Invariants<Aliasing = I::Aliasing>,
932
0
            E: TryWithError<Self>,
933
0
            F: FnOnce(Ptr<'a, T, I>) -> Result<Ptr<'a, U, J>, E>,
934
        {
935
0
            let old_inner = self.as_inner();
936
            #[rustfmt::skip]
937
0
            let res = f(self).map_err(#[inline(always)] move |err: E| {
938
0
                err.map(#[inline(always)] |src| {
939
0
                    drop(src);
940
941
                    // SAFETY:
942
                    // 0. Aliasing is either `Shared` or `Exclusive`:
943
                    //    - If aliasing is `Shared`, then it cannot violate
944
                    //      aliasing make another copy of this pointer (in fact,
945
                    //      using `I::Aliasing = Shared`, we could have just
946
                    //      cloned `self`).
947
                    //    - If aliasing is `Exclusive`, then `f` is not allowed
948
                    //      to make another copy of `self`. In `map_err`, we are
949
                    //      consuming the only value in the returned `Result`.
950
                    //      By invariant on `E: TryWithError<Self>`, that `err:
951
                    //      E` only contains a single `Self` and no other
952
                    //      non-ZST fields which could be `Ptr`s or references
953
                    //      to `self`'s referent. By the same invariant, `map`
954
                    //      consumes this single `Self` and passes it to this
955
                    //      closure. Since `self` was, by invariant on
956
                    //      `Exclusive`, the only `Ptr` or reference live for
957
                    //      `'a` with this referent, and since we `drop(src)`
958
                    //      above, there are no copies left, and so we are
959
                    //      creating the only copy.
960
                    // 1. `self` conforms to `I::Aliasing` by invariant on
961
                    //    `Ptr`, and `old_inner` has the same address, so it
962
                    //    does too.
963
                    // 2. `f` could not have violated `self`'s validity without
964
                    //    itself being unsound. Assuming that `f` is sound, the
965
                    //    referent of `self` is still valid for `T`.
966
0
                    unsafe { Ptr::from_inner(old_inner) }
967
0
                })
968
0
            });
969
0
            res
970
0
        }
971
972
        /// Attempts to transform the pointer, restoring the original on
973
        /// failure.
974
0
        pub(crate) fn try_with<U, J, E, F>(self, f: F) -> Result<Ptr<'a, U, J>, E::Mapped>
975
0
        where
976
0
            U: 'a + ?Sized,
977
0
            J: Invariants<Aliasing = I::Aliasing>,
978
0
            E: TryWithError<Self>,
979
0
            F: FnOnce(Ptr<'a, T, I>) -> Result<Ptr<'a, U, J>, E>,
980
0
            I: Invariants<Aliasing = Shared>,
981
        {
982
            // SAFETY: `I::Aliasing = Shared`, so the safety condition does not
983
            // apply.
984
0
            unsafe { self.try_with_unchecked(f) }
985
0
        }
986
    }
987
988
    /// # Safety
989
    ///
990
    /// `Self` only contains a single `Self::Inner`, and `Self::Mapped` only
991
    /// contains a single `MappedInner`. Other than that, `Self` and
992
    /// `Self::Mapped` contain no non-ZST fields.
993
    ///
994
    /// `map` must pass ownership of `self`'s sole `Self::Inner` to `f`.
995
    pub(crate) unsafe trait TryWithError<MappedInner> {
996
        type Inner;
997
        type Mapped;
998
        fn map<F: FnOnce(Self::Inner) -> MappedInner>(self, f: F) -> Self::Mapped;
999
    }
1000
1001
    impl<'a, T, I> Ptr<'a, T, I>
1002
    where
1003
        T: 'a + KnownLayout + ?Sized,
1004
        I: Invariants,
1005
    {
1006
        /// Casts this pointer-to-initialized into a pointer-to-bytes.
1007
        #[allow(clippy::wrong_self_convention)]
1008
        #[must_use]
1009
        #[inline]
1010
0
        pub fn as_bytes<R>(self) -> Ptr<'a, [u8], (I::Aliasing, Aligned, Valid)>
1011
0
        where
1012
0
            [u8]: TransmuteFromPtr<T, I::Aliasing, I::Validity, Valid, AsBytesCast, R>,
1013
        {
1014
0
            self.transmute_with::<[u8], Valid, AsBytesCast, _>().bikeshed_recall_aligned()
1015
0
        }
1016
    }
1017
1018
    impl<'a, T, I, const N: usize> Ptr<'a, [T; N], I>
1019
    where
1020
        T: 'a,
1021
        I: Invariants,
1022
    {
1023
        /// Casts this pointer-to-array into a slice.
1024
        #[allow(clippy::wrong_self_convention)]
1025
0
        pub(crate) fn as_slice(self) -> Ptr<'a, [T], I> {
1026
0
            let slice = self.as_inner().as_slice();
1027
            // SAFETY: Note that, by post-condition on `PtrInner::as_slice`,
1028
            // `slice` refers to the same byte range as `self.as_inner()`.
1029
            //
1030
            // 0. Thus, `slice` conforms to the aliasing invariant of
1031
            //    `I::Aliasing` because `self` does.
1032
            // 1. By the above lemma, `slice` conforms to the alignment
1033
            //    invariant of `I::Alignment` because `self` does.
1034
            // 2. Since `[T; N]` and `[T]` have the same bit validity [1][2],
1035
            //    and since `self` and the returned `Ptr` have the same validity
1036
            //    invariant, neither `self` nor the returned `Ptr` can be used
1037
            //    to write a value to the referent which violates the other's
1038
            //    validity invariant.
1039
            //
1040
            // [1] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#array-layout:
1041
            //
1042
            //   An array of `[T; N]` has a size of `size_of::<T>() * N` and the
1043
            //   same alignment of `T`. Arrays are laid out so that the
1044
            //   zero-based `nth` element of the array is offset from the start
1045
            //   of the array by `n * size_of::<T>()` bytes.
1046
            //
1047
            //   ...
1048
            //
1049
            //   Slices have the same layout as the section of the array they
1050
            //   slice.
1051
            //
1052
            // [2] Per https://doc.rust-lang.org/1.81.0/reference/types/array.html#array-types:
1053
            //
1054
            //   All elements of arrays are always initialized
1055
0
            unsafe { Ptr::from_inner(slice) }
1056
0
        }
1057
    }
1058
1059
    /// For caller convenience, these methods are generic over alignment
1060
    /// invariant. In practice, the referent is always well-aligned, because the
1061
    /// alignment of `[u8]` is 1.
1062
    impl<'a, I> Ptr<'a, [u8], I>
1063
    where
1064
        I: Invariants<Validity = Valid>,
1065
    {
1066
        /// Attempts to cast `self` to a `U` using the given cast type.
1067
        ///
1068
        /// If `U` is a slice DST and pointer metadata (`meta`) is provided,
1069
        /// then the cast will only succeed if it would produce an object with
1070
        /// the given metadata.
1071
        ///
1072
        /// Returns `None` if the resulting `U` would be invalidly-aligned, if
1073
        /// no `U` can fit in `self`, or if the provided pointer metadata
1074
        /// describes an invalid instance of `U`. On success, returns a pointer
1075
        /// to the largest-possible `U` which fits in `self`.
1076
        ///
1077
        /// # Safety
1078
        ///
1079
        /// The caller may assume that this implementation is correct, and may
1080
        /// rely on that assumption for the soundness of their code. In
1081
        /// particular, the caller may assume that, if `try_cast_into` returns
1082
        /// `Some((ptr, remainder))`, then `ptr` and `remainder` refer to
1083
        /// non-overlapping byte ranges within `self`, and that `ptr` and
1084
        /// `remainder` entirely cover `self`. Finally:
1085
        /// - If this is a prefix cast, `ptr` has the same address as `self`.
1086
        /// - If this is a suffix cast, `remainder` has the same address as
1087
        ///   `self`.
1088
        #[inline(always)]
1089
0
        pub(crate) fn try_cast_into<U, R>(
1090
0
            self,
1091
0
            cast_type: CastType,
1092
0
            meta: Option<U::PointerMetadata>,
1093
0
        ) -> Result<
1094
0
            (Ptr<'a, U, (I::Aliasing, Aligned, Initialized)>, Ptr<'a, [u8], I>),
1095
0
            CastError<Self, U>,
1096
0
        >
1097
0
        where
1098
0
            I::Aliasing: Reference,
1099
0
            U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
1100
        {
1101
0
            let (inner, remainder) =
1102
0
                self.as_inner().try_cast_into(cast_type, meta).map_err(|err| {
1103
0
                    err.map_src(|inner|
1104
                    // SAFETY: `PtrInner::try_cast_into` promises to return its
1105
                    // original argument on error, which was originally produced
1106
                    // by `self.as_inner()`, which is guaranteed to satisfy
1107
                    // `Ptr`'s invariants.
1108
0
                    unsafe { Ptr::from_inner(inner) })
1109
0
                })?;
1110
1111
            // SAFETY:
1112
            // 0. Since `U: Read<I::Aliasing, _>`, either:
1113
            //    - `I::Aliasing` is `Exclusive`, in which case both `src` and
1114
            //      `ptr` conform to `Exclusive`
1115
            //    - `I::Aliasing` is `Shared` and `U` is `Immutable` (we already
1116
            //      know that `[u8]: Immutable`). In this case, neither `U` nor
1117
            //      `[u8]` permit mutation, and so `Shared` aliasing is
1118
            //      satisfied.
1119
            // 1. `ptr` conforms to the alignment invariant of `Aligned` because
1120
            //    it is derived from `try_cast_into`, which promises that the
1121
            //    object described by `target` is validly aligned for `U`.
1122
            // 2. By trait bound, `self` - and thus `target` - is a bit-valid
1123
            //    `[u8]`. `Ptr<[u8], (_, _, Valid)>` and `Ptr<_, (_, _,
1124
            //    Initialized)>` have the same bit validity, and so neither
1125
            //    `self` nor `res` can be used to write a value to the referent
1126
            //    which violates the other's validity invariant.
1127
0
            let res = unsafe { Ptr::from_inner(inner) };
1128
1129
            // SAFETY:
1130
            // 0. `self` and `remainder` both have the type `[u8]`. Thus, they
1131
            //    have `UnsafeCell`s at the same locations. Type casting does
1132
            //    not affect aliasing.
1133
            // 1. `[u8]` has no alignment requirement.
1134
            // 2. `self` has validity `Valid` and has type `[u8]`. Since
1135
            //    `remainder` references a subset of `self`'s referent, it is
1136
            //    also a bit-valid `[u8]`. Thus, neither `self` nor `remainder`
1137
            //    can be used to write a value to the referent which violates
1138
            //    the other's validity invariant.
1139
0
            let remainder = unsafe { Ptr::from_inner(remainder) };
1140
1141
0
            Ok((res, remainder))
1142
0
        }
1143
1144
        /// Attempts to cast `self` into a `U`, failing if all of the bytes of
1145
        /// `self` cannot be treated as a `U`.
1146
        ///
1147
        /// In particular, this method fails if `self` is not validly-aligned
1148
        /// for `U` or if `self`'s size is not a valid size for `U`.
1149
        ///
1150
        /// # Safety
1151
        ///
1152
        /// On success, the caller may assume that the returned pointer
1153
        /// references the same byte range as `self`.
1154
        #[allow(unused)]
1155
        #[inline(always)]
1156
0
        pub(crate) fn try_cast_into_no_leftover<U, R>(
1157
0
            self,
1158
0
            meta: Option<U::PointerMetadata>,
1159
0
        ) -> Result<Ptr<'a, U, (I::Aliasing, Aligned, Initialized)>, CastError<Self, U>>
1160
0
        where
1161
0
            I::Aliasing: Reference,
1162
0
            U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
1163
0
            [u8]: Read<I::Aliasing, R>,
1164
        {
1165
            // SAFETY: The provided closure returns the only copy of `slf`.
1166
            unsafe {
1167
0
                self.try_with_unchecked(|slf| match slf.try_cast_into(CastType::Prefix, meta) {
1168
0
                    Ok((slf, remainder)) => {
1169
0
                        if remainder.len() == 0 {
1170
0
                            Ok(slf)
1171
                        } else {
1172
0
                            Err(CastError::Size(SizeError::<_, U>::new(())))
1173
                        }
1174
                    }
1175
0
                    Err(err) => Err(err.map_src(|_slf| ())),
1176
0
                })
1177
            }
1178
0
        }
1179
    }
1180
1181
    impl<'a, T, I> Ptr<'a, UnsafeCell<T>, I>
1182
    where
1183
        T: 'a + ?Sized,
1184
        I: Invariants<Aliasing = Exclusive>,
1185
    {
1186
        /// Converts this `Ptr` into a pointer to the underlying data.
1187
        ///
1188
        /// This call borrows the `UnsafeCell` mutably (at compile-time) which
1189
        /// guarantees that we possess the only reference.
1190
        ///
1191
        /// This is like [`UnsafeCell::get_mut`], but for `Ptr`.
1192
        ///
1193
        /// [`UnsafeCell::get_mut`]: core::cell::UnsafeCell::get_mut
1194
        #[must_use]
1195
        #[inline(always)]
1196
0
        pub fn get_mut(self) -> Ptr<'a, T, I> {
1197
            // SAFETY: As described below, `UnsafeCell<T>` has the same size
1198
            // as `T: ?Sized` (same static size or same DST layout). Thus,
1199
            // `*const UnsafeCell<T> as *const T` is a size-preserving cast.
1200
            define_cast!(unsafe { Cast<T: ?Sized> = UnsafeCell<T> => T });
1201
1202
            // SAFETY:
1203
            // - Aliasing is `Exclusive`, and so we are not required to promise
1204
            //   anything about the locations of `UnsafeCell`s.
1205
            // - `UnsafeCell<T>` has the same bit validity as `T` [1].
1206
            //   Technically the term "representation" doesn't guarantee this,
1207
            //   but the subsequent sentence in the documentation makes it clear
1208
            //   that this is the intention.
1209
            //
1210
            //   By invariant on `Validity`, since `T` and `UnsafeCell<T>` have
1211
            //   the same bit validity, then the set of values which may appear
1212
            //   in the referent of a `Ptr<T, (_, _, V)>` is the same as the set
1213
            //   which may appear in the referent of a `Ptr<UnsafeCell<T>, (_,
1214
            //   _, V)>`. Thus, neither `self` nor `ptr` may be used to write a
1215
            //   value to the referent which would violate the other's validity
1216
            //   invariant.
1217
            //
1218
            // [1] Per https://doc.rust-lang.org/1.81.0/core/cell/struct.UnsafeCell.html#memory-layout:
1219
            //
1220
            //   `UnsafeCell<T>` has the same in-memory representation as its
1221
            //   inner type `T`. A consequence of this guarantee is that it is
1222
            //   possible to convert between `T` and `UnsafeCell<T>`.
1223
0
            let ptr = unsafe { self.project_transmute_unchecked::<_, _, Cast>() };
1224
1225
            // SAFETY: `UnsafeCell<T>` has the same alignment as `T` [1],
1226
            // and so if `self` is guaranteed to be aligned, then so is the
1227
            // returned `Ptr`.
1228
            //
1229
            // [1] Per https://doc.rust-lang.org/1.81.0/core/cell/struct.UnsafeCell.html#memory-layout:
1230
            //
1231
            //   `UnsafeCell<T>` has the same in-memory representation as
1232
            //   its inner type `T`. A consequence of this guarantee is that
1233
            //   it is possible to convert between `T` and `UnsafeCell<T>`.
1234
0
            let ptr = unsafe { ptr.assume_alignment::<I::Alignment>() };
1235
0
            ptr.unify_invariants()
1236
0
        }
1237
    }
1238
}
1239
1240
/// Projections through the referent.
1241
mod _project {
1242
    use super::*;
1243
1244
    impl<'a, T, I> Ptr<'a, [T], I>
1245
    where
1246
        T: 'a,
1247
        I: Invariants,
1248
        I::Aliasing: Reference,
1249
    {
1250
        /// Iteratively projects the elements `Ptr<T>` from `Ptr<[T]>`.
1251
0
        pub(crate) fn iter(&self) -> impl Iterator<Item = Ptr<'a, T, I>> {
1252
            // SAFETY:
1253
            // 0. `elem` conforms to the aliasing invariant of `I::Aliasing`
1254
            //    because projection does not impact the aliasing invariant.
1255
            // 1. `elem`, conditionally, conforms to the validity invariant of
1256
            //    `I::Alignment`. If `elem` is projected from data well-aligned
1257
            //    for `[T]`, `elem` will be valid for `T`.
1258
            // 2. `elem` conforms to the validity invariant of `I::Validity`.
1259
            //    Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#array-layout:
1260
            //
1261
            //      Slices have the same layout as the section of the array they
1262
            //      slice.
1263
            //
1264
            //    Arrays are laid out so that the zero-based `nth` element of
1265
            //    the array is offset from the start of the array by `n *
1266
            //    size_of::<T>()` bytes. Thus, `elem` addresses a valid `T`
1267
            //    within the slice. Since `self` satisfies `I::Validity`, `elem`
1268
            //    also satisfies `I::Validity`.
1269
0
            self.as_inner().iter().map(|elem| unsafe { Ptr::from_inner(elem) })
1270
0
        }
1271
    }
1272
1273
    #[allow(clippy::needless_lifetimes)]
1274
    impl<'a, T, I> Ptr<'a, T, I>
1275
    where
1276
        T: 'a + ?Sized + KnownLayout<PointerMetadata = usize>,
1277
        I: Invariants,
1278
    {
1279
        /// The number of slice elements in the object referenced by `self`.
1280
0
        pub(crate) fn len(&self) -> usize {
1281
0
            self.as_inner().meta().get()
1282
0
        }
1283
    }
1284
}
1285
1286
#[cfg(test)]
1287
mod tests {
1288
    use core::mem::{self, MaybeUninit};
1289
1290
    use super::*;
1291
    #[allow(unused)] // Needed on our MSRV, but considered unused on later toolchains.
1292
    use crate::util::AsAddress;
1293
    use crate::{pointer::BecauseImmutable, util::testutil::AU64, FromBytes, Immutable};
1294
1295
    mod test_ptr_try_cast_into_soundness {
1296
        use super::*;
1297
1298
        // This test is designed so that if `Ptr::try_cast_into_xxx` are
1299
        // buggy, it will manifest as unsoundness that Miri can detect.
1300
1301
        // - If `size_of::<T>() == 0`, `N == 4`
1302
        // - Else, `N == 4 * size_of::<T>()`
1303
        //
1304
        // Each test will be run for each metadata in `metas`.
1305
        fn test<T, I, const N: usize>(metas: I)
1306
        where
1307
            T: ?Sized + KnownLayout + Immutable + FromBytes,
1308
            I: IntoIterator<Item = Option<T::PointerMetadata>> + Clone,
1309
        {
1310
            let mut bytes = [MaybeUninit::<u8>::uninit(); N];
1311
            let initialized = [MaybeUninit::new(0u8); N];
1312
            for start in 0..=bytes.len() {
1313
                for end in start..=bytes.len() {
1314
                    // Set all bytes to uninitialized other than those in
1315
                    // the range we're going to pass to `try_cast_from`.
1316
                    // This allows Miri to detect out-of-bounds reads
1317
                    // because they read uninitialized memory. Without this,
1318
                    // some out-of-bounds reads would still be in-bounds of
1319
                    // `bytes`, and so might spuriously be accepted.
1320
                    bytes = [MaybeUninit::<u8>::uninit(); N];
1321
                    let bytes = &mut bytes[start..end];
1322
                    // Initialize only the byte range we're going to pass to
1323
                    // `try_cast_from`.
1324
                    bytes.copy_from_slice(&initialized[start..end]);
1325
1326
                    let bytes = {
1327
                        let bytes: *const [MaybeUninit<u8>] = bytes;
1328
                        #[allow(clippy::as_conversions)]
1329
                        let bytes = bytes as *const [u8];
1330
                        // SAFETY: We just initialized these bytes to valid
1331
                        // `u8`s.
1332
                        unsafe { &*bytes }
1333
                    };
1334
1335
                    // SAFETY: The bytes in `slf` must be initialized.
1336
                    unsafe fn validate_and_get_len<
1337
                        T: ?Sized + KnownLayout + FromBytes + Immutable,
1338
                    >(
1339
                        slf: Ptr<'_, T, (Shared, Aligned, Initialized)>,
1340
                    ) -> usize {
1341
                        let t = slf.recall_validity().as_ref();
1342
1343
                        let bytes = {
1344
                            let len = mem::size_of_val(t);
1345
                            let t: *const T = t;
1346
                            // SAFETY:
1347
                            // - We know `t`'s bytes are all initialized
1348
                            //   because we just read it from `slf`, which
1349
                            //   points to an initialized range of bytes. If
1350
                            //   there's a bug and this doesn't hold, then
1351
                            //   that's exactly what we're hoping Miri will
1352
                            //   catch!
1353
                            // - Since `T: FromBytes`, `T` doesn't contain
1354
                            //   any `UnsafeCell`s, so it's okay for `t: T`
1355
                            //   and a `&[u8]` to the same memory to be
1356
                            //   alive concurrently.
1357
                            unsafe { core::slice::from_raw_parts(t.cast::<u8>(), len) }
1358
                        };
1359
1360
                        // This assertion ensures that `t`'s bytes are read
1361
                        // and compared to another value, which in turn
1362
                        // ensures that Miri gets a chance to notice if any
1363
                        // of `t`'s bytes are uninitialized, which they
1364
                        // shouldn't be (see the comment above).
1365
                        assert_eq!(bytes, vec![0u8; bytes.len()]);
1366
1367
                        mem::size_of_val(t)
1368
                    }
1369
1370
                    for meta in metas.clone().into_iter() {
1371
                        for cast_type in [CastType::Prefix, CastType::Suffix] {
1372
                            if let Ok((slf, remaining)) = Ptr::from_ref(bytes)
1373
                                .try_cast_into::<T, BecauseImmutable>(cast_type, meta)
1374
                            {
1375
                                // SAFETY: All bytes in `bytes` have been
1376
                                // initialized.
1377
                                let len = unsafe { validate_and_get_len(slf) };
1378
                                assert_eq!(remaining.len(), bytes.len() - len);
1379
                                #[allow(unstable_name_collisions)]
1380
                                let bytes_addr = bytes.as_ptr().addr();
1381
                                #[allow(unstable_name_collisions)]
1382
                                let remaining_addr = remaining.as_inner().as_ptr().addr();
1383
                                match cast_type {
1384
                                    CastType::Prefix => {
1385
                                        assert_eq!(remaining_addr, bytes_addr + len)
1386
                                    }
1387
                                    CastType::Suffix => assert_eq!(remaining_addr, bytes_addr),
1388
                                }
1389
1390
                                if let Some(want) = meta {
1391
                                    let got =
1392
                                        KnownLayout::pointer_to_metadata(slf.as_inner().as_ptr());
1393
                                    assert_eq!(got, want);
1394
                                }
1395
                            }
1396
                        }
1397
1398
                        if let Ok(slf) = Ptr::from_ref(bytes)
1399
                            .try_cast_into_no_leftover::<T, BecauseImmutable>(meta)
1400
                        {
1401
                            // SAFETY: All bytes in `bytes` have been
1402
                            // initialized.
1403
                            let len = unsafe { validate_and_get_len(slf) };
1404
                            assert_eq!(len, bytes.len());
1405
1406
                            if let Some(want) = meta {
1407
                                let got = KnownLayout::pointer_to_metadata(slf.as_inner().as_ptr());
1408
                                assert_eq!(got, want);
1409
                            }
1410
                        }
1411
                    }
1412
                }
1413
            }
1414
        }
1415
1416
        #[derive(FromBytes, KnownLayout, Immutable)]
1417
        #[repr(C)]
1418
        struct SliceDst<T> {
1419
            a: u8,
1420
            trailing: [T],
1421
        }
1422
1423
        // Each test case becomes its own `#[test]` function. We do this because
1424
        // this test in particular takes far, far longer to execute under Miri
1425
        // than all of our other tests combined. Previously, we had these
1426
        // execute sequentially in a single test function. We run Miri tests in
1427
        // parallel in CI, but this test being sequential meant that most of
1428
        // that parallelism was wasted, as all other tests would finish in a
1429
        // fraction of the total execution time, leaving this test to execute on
1430
        // a single thread for the remainder of the test. By putting each test
1431
        // case in its own function, we permit better use of available
1432
        // parallelism.
1433
        macro_rules! test {
1434
            ($test_name:ident: $ty:ty) => {
1435
                #[test]
1436
                #[allow(non_snake_case)]
1437
                fn $test_name() {
1438
                    const S: usize = core::mem::size_of::<$ty>();
1439
                    const N: usize = if S == 0 { 4 } else { S * 4 };
1440
                    test::<$ty, _, N>([None]);
1441
1442
                    // If `$ty` is a ZST, then we can't pass `None` as the
1443
                    // pointer metadata, or else computing the correct trailing
1444
                    // slice length will panic.
1445
                    if S == 0 {
1446
                        test::<[$ty], _, N>([Some(0), Some(1), Some(2), Some(3)]);
1447
                        test::<SliceDst<$ty>, _, N>([Some(0), Some(1), Some(2), Some(3)]);
1448
                    } else {
1449
                        test::<[$ty], _, N>([None, Some(0), Some(1), Some(2), Some(3)]);
1450
                        test::<SliceDst<$ty>, _, N>([None, Some(0), Some(1), Some(2), Some(3)]);
1451
                    }
1452
                }
1453
            };
1454
            ($ty:ident) => {
1455
                test!($ty: $ty);
1456
            };
1457
            ($($ty:ident),*) => { $(test!($ty);)* }
1458
        }
1459
1460
        test!(empty_tuple: ());
1461
        test!(u8, u16, u32, u64, u128, usize, AU64);
1462
        test!(i8, i16, i32, i64, i128, isize);
1463
        test!(f32, f64);
1464
    }
1465
1466
    #[test]
1467
    fn test_try_cast_into_explicit_count() {
1468
        macro_rules! test {
1469
            ($ty:ty, $bytes:expr, $elems:expr, $expect:expr) => {{
1470
                let bytes = [0u8; $bytes];
1471
                let ptr = Ptr::from_ref(&bytes[..]);
1472
                let res =
1473
                    ptr.try_cast_into::<$ty, BecauseImmutable>(CastType::Prefix, Some($elems));
1474
                if let Some(expect) = $expect {
1475
                    let (ptr, _) = res.unwrap();
1476
                    assert_eq!(KnownLayout::pointer_to_metadata(ptr.as_inner().as_ptr()), expect);
1477
                } else {
1478
                    let _ = res.unwrap_err();
1479
                }
1480
            }};
1481
        }
1482
1483
        #[derive(KnownLayout, Immutable)]
1484
        #[repr(C)]
1485
        struct ZstDst {
1486
            u: [u8; 8],
1487
            slc: [()],
1488
        }
1489
1490
        test!(ZstDst, 8, 0, Some(0));
1491
        test!(ZstDst, 7, 0, None);
1492
1493
        test!(ZstDst, 8, usize::MAX, Some(usize::MAX));
1494
        test!(ZstDst, 7, usize::MAX, None);
1495
1496
        #[derive(KnownLayout, Immutable)]
1497
        #[repr(C)]
1498
        struct Dst {
1499
            u: [u8; 8],
1500
            slc: [u8],
1501
        }
1502
1503
        test!(Dst, 8, 0, Some(0));
1504
        test!(Dst, 7, 0, None);
1505
1506
        test!(Dst, 9, 1, Some(1));
1507
        test!(Dst, 8, 1, None);
1508
1509
        // If we didn't properly check for overflow, this would cause the
1510
        // metadata to overflow to 0, and thus the cast would spuriously
1511
        // succeed.
1512
        test!(Dst, 8, usize::MAX - 8 + 1, None);
1513
    }
1514
1515
    #[test]
1516
    fn test_try_cast_into_no_leftover_restores_original_slice() {
1517
        let bytes = [0u8; 4];
1518
        let ptr = Ptr::from_ref(&bytes[..]);
1519
        let res = ptr.try_cast_into_no_leftover::<[u8; 2], BecauseImmutable>(None);
1520
        match res {
1521
            Ok(_) => panic!("should have failed due to leftover bytes"),
1522
            Err(CastError::Size(e)) => {
1523
                assert_eq!(e.into_src().len(), 4, "Should return original slice length");
1524
            }
1525
            Err(e) => panic!("wrong error type: {:?}", e),
1526
        }
1527
    }
1528
}