core/ptr/mod.rs
1//! Manually manage memory through raw pointers.
2//!
3//! *[See also the pointer primitive types](pointer).*
4//!
5//! # Safety
6//!
7//! Many functions in this module take raw pointers as arguments and read from or write to them. For
8//! this to be safe, these pointers must be *valid* for the given access. Whether a pointer is valid
9//! depends on the operation it is used for (read or write), and the extent of the memory that is
10//! accessed (i.e., how many bytes are read/written) -- it makes no sense to ask "is this pointer
11//! valid"; one has to ask "is this pointer valid for a given access". Most functions use `*mut T`
12//! and `*const T` to access only a single value, in which case the documentation omits the size and
13//! implicitly assumes it to be `size_of::<T>()` bytes.
14//!
15//! The precise rules for validity are not determined yet. The guarantees that are
16//! provided at this point are very minimal:
17//!
18//! * For memory accesses of [size zero][zst], *every* pointer is valid, including the [null]
19//! pointer. The following points are only concerned with non-zero-sized accesses.
20//! * A [null] pointer is *never* valid.
21//! * For a pointer to be valid, it is necessary, but not always sufficient, that the pointer be
22//! *dereferenceable*. The [provenance] of the pointer is used to determine which [allocation]
23//! it is derived from; a pointer is dereferenceable if the memory range of the given size
24//! starting at the pointer is entirely contained within the bounds of that allocation. Note
25//! that in Rust, every (stack-allocated) variable is considered a separate allocation.
26//! * All accesses performed by functions in this module are *non-atomic* in the sense
27//! of [atomic operations] used to synchronize between threads. This means it is
28//! undefined behavior to perform two concurrent accesses to the same location from different
29//! threads unless both accesses only read from memory. Notice that this explicitly
30//! includes [`read_volatile`] and [`write_volatile`]: Volatile accesses cannot
31//! be used for inter-thread synchronization, regardless of whether they are acting on
32//! Rust memory or not.
33//! * The result of casting a reference to a pointer is valid for as long as the
34//! underlying allocation is live and no reference (just raw pointers) is used to
35//! access the same memory. That is, reference and pointer accesses cannot be
36//! interleaved.
37//!
38//! These axioms, along with careful use of [`offset`] for pointer arithmetic,
39//! are enough to correctly implement many useful things in unsafe code. Stronger guarantees
40//! will be provided eventually, as the [aliasing] rules are being determined. For more
41//! information, see the [book] as well as the section in the reference devoted
42//! to [undefined behavior][ub].
43//!
44//! We say that a pointer is "dangling" if it is not valid for any non-zero-sized accesses. This
45//! means out-of-bounds pointers, pointers to freed memory, null pointers, and pointers created with
46//! [`NonNull::dangling`] are all dangling.
47//!
48//! ## Alignment
49//!
50//! Valid raw pointers as defined above are not necessarily properly aligned (where
51//! "proper" alignment is defined by the pointee type, i.e., `*const T` must be
52//! aligned to `align_of::<T>()`). However, most functions require their
53//! arguments to be properly aligned, and will explicitly state
54//! this requirement in their documentation. Notable exceptions to this are
55//! [`read_unaligned`] and [`write_unaligned`].
56//!
57//! When a function requires proper alignment, it does so even if the access
58//! has size 0, i.e., even if memory is not actually touched. Consider using
59//! [`NonNull::dangling`] in such cases.
60//!
61//! ## Pointer to reference conversion
62//!
63//! When converting a pointer to a reference (e.g. via `&*ptr` or `&mut *ptr`),
64//! there are several rules that must be followed:
65//!
66//! * The pointer must be properly aligned.
67//!
68//! * It must be non-null.
69//!
70//! * It must be "dereferenceable" in the sense defined above.
71//!
72//! * The pointer must point to a [valid value] of type `T`.
73//!
74//! * You must enforce Rust's aliasing rules. The exact aliasing rules are not decided yet, so we
75//! only give a rough overview here. The rules also depend on whether a mutable or a shared
76//! reference is being created.
77//! * When creating a mutable reference, then while this reference exists, the memory it points to
78//! must not get accessed (read or written) through any other pointer or reference not derived
79//! from this reference.
80//! * When creating a shared reference, then while this reference exists, the memory it points to
81//! must not get mutated (except inside `UnsafeCell`).
82//!
83//! If a pointer follows all of these rules, it is said to be
84//! *convertible to a (mutable or shared) reference*.
85// ^ we use this term instead of saying that the produced reference must
86// be valid, as the validity of a reference is easily confused for the
87// validity of the thing it refers to, and while the two concepts are
88// closely related, they are not identical.
89//!
90//! These rules apply even if the result is unused!
91//! (The part about being initialized is not yet fully decided, but until
92//! it is, the only safe approach is to ensure that they are indeed initialized.)
93//!
94//! An example of the implications of the above rules is that an expression such
95//! as `unsafe { &*(0 as *const u8) }` is Immediate Undefined Behavior.
96//!
97//! [valid value]: ../../reference/behavior-considered-undefined.html#invalid-values
98//!
99//! ## Allocation
100//!
101//! <a id="allocated-object"></a> <!-- keep old URLs working -->
102//!
103//! An *allocation* is a subset of program memory which is addressable
104//! from Rust, and within which pointer arithmetic is possible. Examples of
105//! allocations include heap allocations, stack-allocated variables,
106//! statics, and consts. The safety preconditions of some Rust operations -
107//! such as `offset` and field projections (`expr.field`) - are defined in
108//! terms of the allocations on which they operate.
109//!
110//! An allocation has a base address, a size, and a set of memory
111//! addresses. It is possible for an allocation to have zero size, but
112//! such an allocation will still have a base address. The base address
113//! of an allocation is not necessarily unique. While it is currently the
114//! case that an allocation always has a set of memory addresses which is
115//! fully contiguous (i.e., has no "holes"), there is no guarantee that this
116//! will not change in the future.
117//!
118//! Allocations must behave like "normal" memory: in particular, reads must not have
119//! side-effects, and writes must become visible to other threads using the usual synchronization
120//! primitives.
121//!
122//! For any allocation with `base` address, `size`, and a set of
123//! `addresses`, the following are guaranteed:
124//! - For all addresses `a` in `addresses`, `a` is in the range `base .. (base +
125//! size)` (note that this requires `a < base + size`, not `a <= base + size`)
126//! - `base` is not equal to [`null()`] (i.e., the address with the numerical
127//! value 0)
128//! - `base + size <= usize::MAX`
129//! - `size <= isize::MAX`
130//!
131//! As a consequence of these guarantees, given any address `a` within the set
132//! of addresses of an allocation:
133//! - It is guaranteed that `a - base` does not overflow `isize`
134//! - It is guaranteed that `a - base` is non-negative
135//! - It is guaranteed that, given `o = a - base` (i.e., the offset of `a` within
136//! the allocation), `base + o` will not wrap around the address space (in
137//! other words, will not overflow `usize`)
138//!
139//! [`null()`]: null
140//!
141//! # Provenance
142//!
143//! Pointers are not *simply* an "integer" or "address". For instance, it's uncontroversial
144//! to say that a Use After Free is clearly Undefined Behavior, even if you "get lucky"
145//! and the freed memory gets reallocated before your read/write (in fact this is the
146//! worst-case scenario, UAFs would be much less concerning if this didn't happen!).
147//! As another example, consider that [`wrapping_offset`] is documented to "remember"
148//! the allocation that the original pointer points to, even if it is offset far
149//! outside the memory range occupied by that allocation.
150//! To rationalize claims like this, pointers need to somehow be *more* than just their addresses:
151//! they must have **provenance**.
152//!
153//! A pointer value in Rust semantically contains the following information:
154//!
155//! * The **address** it points to, which can be represented by a `usize`.
156//! * The **provenance** it has, defining the memory it has permission to access. Provenance can be
157//! absent, in which case the pointer does not have permission to access any memory.
158//!
159//! The exact structure of provenance is not yet specified, but the permission defined by a
160//! pointer's provenance have a *spatial* component, a *temporal* component, and a *mutability*
161//! component:
162//!
163//! * Spatial: The set of memory addresses that the pointer is allowed to access.
164//! * Temporal: The timespan during which the pointer is allowed to access those memory addresses.
165//! * Mutability: Whether the pointer may only access the memory for reads, or also access it for
166//! writes. Note that this can interact with the other components, e.g. a pointer might permit
167//! mutation only for a subset of addresses, or only for a subset of its maximal timespan.
168//!
169//! When an [allocation] is created, it has a unique Original Pointer. For alloc
170//! APIs this is literally the pointer the call returns, and for local variables and statics,
171//! this is the name of the variable/static. (This is mildly overloading the term "pointer"
172//! for the sake of brevity/exposition.)
173//!
174//! The Original Pointer for an allocation has provenance that constrains the *spatial*
175//! permissions of this pointer to the memory range of the allocation, and the *temporal*
176//! permissions to the lifetime of the allocation. Provenance is implicitly inherited by all
177//! pointers transitively derived from the Original Pointer through operations like [`offset`],
178//! borrowing, and pointer casts. Some operations may *shrink* the permissions of the derived
179//! provenance, limiting how much memory it can access or how long it's valid for (i.e. borrowing a
180//! subfield and subslicing can shrink the spatial component of provenance, and all borrowing can
181//! shrink the temporal component of provenance). However, no operation can ever *grow* the
182//! permissions of the derived provenance: even if you "know" there is a larger allocation, you
183//! can't derive a pointer with a larger provenance. Similarly, you cannot "recombine" two
184//! contiguous provenances back into one (i.e. with a `fn merge(&[T], &[T]) -> &[T]`).
185//!
186//! A reference to a place always has provenance over at least the memory that place occupies.
187//! A reference to a slice always has provenance over at least the range that slice describes.
188//! Whether and when exactly the provenance of a reference gets "shrunk" to *exactly* fit
189//! the memory it points to is not yet determined.
190//!
191//! A *shared* reference only ever has provenance that permits reading from memory,
192//! and never permits writes, except inside [`UnsafeCell`].
193//!
194//! Provenance can affect whether a program has undefined behavior:
195//!
196//! * It is undefined behavior to access memory through a pointer that does not have provenance over
197//! that memory. Note that a pointer "at the end" of its provenance is not actually outside its
198//! provenance, it just has 0 bytes it can load/store. Zero-sized accesses do not require any
199//! provenance since they access an empty range of memory.
200//!
201//! * It is undefined behavior to [`offset`] a pointer across a memory range that is not contained
202//! in the allocation it is derived from, or to [`offset_from`] two pointers not derived
203//! from the same allocation. Provenance is used to say what exactly "derived from" even
204//! means: the lineage of a pointer is traced back to the Original Pointer it descends from, and
205//! that identifies the relevant allocation. In particular, it's always UB to offset a
206//! pointer derived from something that is now deallocated, except if the offset is 0.
207//!
208//! But it *is* still sound to:
209//!
210//! * Create a pointer without provenance from just an address (see [`without_provenance`]). Such a
211//! pointer cannot be used for memory accesses (except for zero-sized accesses). This can still be
212//! useful for sentinel values like `null` *or* to represent a tagged pointer that will never be
213//! dereferenceable. In general, it is always sound for an integer to pretend to be a pointer "for
214//! fun" as long as you don't use operations on it which require it to be valid (non-zero-sized
215//! offset, read, write, etc).
216//!
217//! * Forge an allocation of size zero at any sufficiently aligned non-null address.
218//! i.e. the usual "ZSTs are fake, do what you want" rules apply.
219//!
220//! * [`wrapping_offset`] a pointer outside its provenance. This includes pointers
221//! which have "no" provenance. In particular, this makes it sound to do pointer tagging tricks.
222//!
223//! * Compare arbitrary pointers by address. Pointer comparison ignores provenance and addresses
224//! *are* just integers, so there is always a coherent answer, even if the pointers are dangling
225//! or from different provenances. Note that if you get "lucky" and notice that a pointer at the
226//! end of one allocation is the "same" address as the start of another allocation,
227//! anything you do with that fact is *probably* going to be gibberish. The scope of that
228//! gibberish is kept under control by the fact that the two pointers *still* aren't allowed to
229//! access the other's allocation (bytes), because they still have different provenance.
230//!
231//! Note that the full definition of provenance in Rust is not decided yet, as this interacts
232//! with the as-yet undecided [aliasing] rules.
233//!
234//! ## Pointers Vs Integers
235//!
236//! From this discussion, it becomes very clear that a `usize` *cannot* accurately represent a pointer,
237//! and converting from a pointer to a `usize` is generally an operation which *only* extracts the
238//! address. Converting this address back into pointer requires somehow answering the question:
239//! which provenance should the resulting pointer have?
240//!
241//! Rust provides two ways of dealing with this situation: *Strict Provenance* and *Exposed Provenance*.
242//!
243//! Note that a pointer *can* represent a `usize` (via [`without_provenance`]), so the right type to
244//! use in situations where a value is "sometimes a pointer and sometimes a bare `usize`" is a
245//! pointer type.
246//!
247//! ## Strict Provenance
248//!
249//! "Strict Provenance" refers to a set of APIs designed to make working with provenance more
250//! explicit. They are intended as substitutes for casting a pointer to an integer and back.
251//!
252//! Entirely avoiding integer-to-pointer casts successfully side-steps the inherent ambiguity of
253//! that operation. This benefits compiler optimizations, and it is pretty much a requirement for
254//! using tools like [Miri] and architectures like [CHERI] that aim to detect and diagnose pointer
255//! misuse.
256//!
257//! The key insight to making programming without integer-to-pointer casts *at all* viable is the
258//! [`with_addr`] method:
259//!
260//! ```text
261//! /// Creates a new pointer with the given address.
262//! ///
263//! /// This performs the same operation as an `addr as ptr` cast, but copies
264//! /// the *provenance* of `self` to the new pointer.
265//! /// This allows us to dynamically preserve and propagate this important
266//! /// information in a way that is otherwise impossible with a unary cast.
267//! ///
268//! /// This is equivalent to using `wrapping_offset` to offset `self` to the
269//! /// given address, and therefore has all the same capabilities and restrictions.
270//! pub fn with_addr(self, addr: usize) -> Self;
271//! ```
272//!
273//! So you're still able to drop down to the address representation and do whatever
274//! clever bit tricks you want *as long as* you're able to keep around a pointer
275//! into the allocation you care about that can "reconstitute" the provenance.
276//! Usually this is very easy, because you only are taking a pointer, messing with the address,
277//! and then immediately converting back to a pointer. To make this use case more ergonomic,
278//! we provide the [`map_addr`] method.
279//!
280//! To help make it clear that code is "following" Strict Provenance semantics, we also provide an
281//! [`addr`] method which promises that the returned address is not part of a
282//! pointer-integer-pointer roundtrip. In the future we may provide a lint for pointer<->integer
283//! casts to help you audit if your code conforms to strict provenance.
284//!
285//! ### Using Strict Provenance
286//!
287//! Most code needs no changes to conform to strict provenance, as the only really concerning
288//! operation is casts from `usize` to a pointer. For code which *does* cast a `usize` to a pointer,
289//! the scope of the change depends on exactly what you're doing.
290//!
291//! In general, you just need to make sure that if you want to convert a `usize` address to a
292//! pointer and then use that pointer to read/write memory, you need to keep around a pointer
293//! that has sufficient provenance to perform that read/write itself. In this way all of your
294//! casts from an address to a pointer are essentially just applying offsets/indexing.
295//!
296//! This is generally trivial to do for simple cases like tagged pointers *as long as you
297//! represent the tagged pointer as an actual pointer and not a `usize`*. For instance:
298//!
299//! ```
300//! unsafe {
301//! // A flag we want to pack into our pointer
302//! static HAS_DATA: usize = 0x1;
303//! static FLAG_MASK: usize = !HAS_DATA;
304//!
305//! // Our value, which must have enough alignment to have spare least-significant-bits.
306//! let my_precious_data: u32 = 17;
307//! assert!(align_of::<u32>() > 1);
308//!
309//! // Create a tagged pointer
310//! let ptr = &my_precious_data as *const u32;
311//! let tagged = ptr.map_addr(|addr| addr | HAS_DATA);
312//!
313//! // Check the flag:
314//! if tagged.addr() & HAS_DATA != 0 {
315//! // Untag and read the pointer
316//! let data = *tagged.map_addr(|addr| addr & FLAG_MASK);
317//! assert_eq!(data, 17);
318//! } else {
319//! unreachable!()
320//! }
321//! }
322//! ```
323//!
324//! (Yes, if you've been using [`AtomicUsize`] for pointers in concurrent datastructures, you should
325//! be using [`AtomicPtr`] instead. If that messes up the way you atomically manipulate pointers,
326//! we would like to know why, and what needs to be done to fix it.)
327//!
328//! Situations where a valid pointer *must* be created from just an address, such as baremetal code
329//! accessing a memory-mapped interface at a fixed address, cannot currently be handled with strict
330//! provenance APIs and should use [exposed provenance](#exposed-provenance).
331//!
332//! ## Exposed Provenance
333//!
334//! As discussed above, integer-to-pointer casts are not possible with Strict Provenance APIs.
335//! This is by design: the goal of Strict Provenance is to provide a clear specification that we are
336//! confident can be formalized unambiguously and can be subject to precise formal reasoning.
337//! Integer-to-pointer casts do not (currently) have such a clear specification.
338//!
339//! However, there exist situations where integer-to-pointer casts cannot be avoided, or
340//! where avoiding them would require major refactoring. Legacy platform APIs also regularly assume
341//! that `usize` can capture all the information that makes up a pointer.
342//! Bare-metal platforms can also require the synthesis of a pointer "out of thin air" without
343//! anywhere to obtain proper provenance from.
344//!
345//! Rust's model for dealing with integer-to-pointer casts is called *Exposed Provenance*. However,
346//! the semantics of Exposed Provenance are on much less solid footing than Strict Provenance, and
347//! at this point it is not yet clear whether a satisfying unambiguous semantics can be defined for
348//! Exposed Provenance. (If that sounds bad, be reassured that other popular languages that provide
349//! integer-to-pointer casts are not faring any better.) Furthermore, Exposed Provenance will not
350//! work (well) with tools like [Miri] and [CHERI].
351//!
352//! Exposed Provenance is provided by the [`expose_provenance`] and [`with_exposed_provenance`] methods,
353//! which are equivalent to `as` casts between pointers and integers.
354//! - [`expose_provenance`] is a lot like [`addr`], but additionally adds the provenance of the
355//! pointer to a global list of 'exposed' provenances. (This list is purely conceptual, it exists
356//! for the purpose of specifying Rust but is not materialized in actual executions, except in
357//! tools like [Miri].)
358//! Memory which is outside the control of the Rust abstract machine (MMIO registers, for example)
359//! is always considered to be exposed, so long as this memory is disjoint from memory that will
360//! be used by the abstract machine such as the stack, heap, and statics.
361//! - [`with_exposed_provenance`] can be used to construct a pointer with one of these previously
362//! 'exposed' provenances. [`with_exposed_provenance`] takes only `addr: usize` as arguments, so
363//! unlike in [`with_addr`] there is no indication of what the correct provenance for the returned
364//! pointer is -- and that is exactly what makes integer-to-pointer casts so tricky to rigorously
365//! specify! The compiler will do its best to pick the right provenance for you, but currently we
366//! cannot provide any guarantees about which provenance the resulting pointer will have. Only one
367//! thing is clear: if there is *no* previously 'exposed' provenance that justifies the way the
368//! returned pointer will be used, the program has undefined behavior.
369//!
370//! If at all possible, we encourage code to be ported to [Strict Provenance] APIs, thus avoiding
371//! the need for Exposed Provenance. Maximizing the amount of such code is a major win for avoiding
372//! specification complexity and to facilitate adoption of tools like [CHERI] and [Miri] that can be
373//! a big help in increasing the confidence in (unsafe) Rust code. However, we acknowledge that this
374//! is not always possible, and offer Exposed Provenance as a way to explicit "opt out" of the
375//! well-defined semantics of Strict Provenance, and "opt in" to the unclear semantics of
376//! integer-to-pointer casts.
377//!
378//! [aliasing]: ../../nomicon/aliasing.html
379//! [allocation]: #allocation
380//! [provenance]: #provenance
381//! [book]: ../../book/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer
382//! [ub]: ../../reference/behavior-considered-undefined.html
383//! [zst]: ../../nomicon/exotic-sizes.html#zero-sized-types-zsts
384//! [atomic operations]: crate::sync::atomic
385//! [`offset`]: pointer::offset
386//! [`offset_from`]: pointer::offset_from
387//! [`wrapping_offset`]: pointer::wrapping_offset
388//! [`with_addr`]: pointer::with_addr
389//! [`map_addr`]: pointer::map_addr
390//! [`addr`]: pointer::addr
391//! [`AtomicUsize`]: crate::sync::atomic::AtomicUsize
392//! [`AtomicPtr`]: crate::sync::atomic::AtomicPtr
393//! [`expose_provenance`]: pointer::expose_provenance
394//! [`with_exposed_provenance`]: with_exposed_provenance
395//! [Miri]: https://github.com/rust-lang/miri
396//! [CHERI]: https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/
397//! [Strict Provenance]: #strict-provenance
398//! [`UnsafeCell`]: core::cell::UnsafeCell
399
400#![stable(feature = "rust1", since = "1.0.0")]
401// There are many unsafe functions taking pointers that don't dereference them.
402#![allow(clippy::not_unsafe_ptr_arg_deref)]
403
404use crate::cmp::Ordering;
405use crate::intrinsics::const_eval_select;
406use crate::marker::{FnPtr, PointeeSized};
407use crate::mem::{self, MaybeUninit, SizedTypeProperties};
408use crate::num::NonZero;
409use crate::{fmt, hash, intrinsics, ub_checks};
410
411mod alignment;
412#[unstable(feature = "ptr_alignment_type", issue = "102070")]
413pub use alignment::Alignment;
414
415mod metadata;
416#[unstable(feature = "ptr_metadata", issue = "81513")]
417pub use metadata::{DynMetadata, Pointee, Thin, from_raw_parts, from_raw_parts_mut, metadata};
418
419mod non_null;
420#[stable(feature = "nonnull", since = "1.25.0")]
421pub use non_null::NonNull;
422
423mod unique;
424#[unstable(feature = "ptr_internals", issue = "none")]
425pub use unique::Unique;
426
427mod const_ptr;
428mod mut_ptr;
429
430// Some functions are defined here because they accidentally got made
431// available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
432// (`transmute` also falls into this category, but it cannot be wrapped due to the
433// check that `T` and `U` have the same size.)
434
435/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
436/// and destination must *not* overlap.
437///
438/// For regions of memory which might overlap, use [`copy`] instead.
439///
440/// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
441/// with the source and destination arguments swapped,
442/// and `count` counting the number of `T`s instead of bytes.
443///
444/// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
445/// requirements of `T`. The initialization state is preserved exactly.
446///
447/// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
448///
449/// # Safety
450///
451/// Behavior is undefined if any of the following conditions are violated:
452///
453/// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
454///
455/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
456///
457/// * Both `src` and `dst` must be properly aligned.
458///
459/// * The region of memory beginning at `src` with a size of `count *
460/// size_of::<T>()` bytes must *not* overlap with the region of memory
461/// beginning at `dst` with the same size.
462///
463/// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
464/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
465/// in the region beginning at `*src` and the region beginning at `*dst` can
466/// [violate memory safety][read-ownership].
467///
468/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
469/// `0`, the pointers must be properly aligned.
470///
471/// [`read`]: crate::ptr::read
472/// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
473/// [valid]: crate::ptr#safety
474///
475/// # Examples
476///
477/// Manually implement [`Vec::append`]:
478///
479/// ```
480/// use std::ptr;
481///
482/// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
483/// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
484/// let src_len = src.len();
485/// let dst_len = dst.len();
486///
487/// // Ensure that `dst` has enough capacity to hold all of `src`.
488/// dst.reserve(src_len);
489///
490/// unsafe {
491/// // The call to add is always safe because `Vec` will never
492/// // allocate more than `isize::MAX` bytes.
493/// let dst_ptr = dst.as_mut_ptr().add(dst_len);
494/// let src_ptr = src.as_ptr();
495///
496/// // Truncate `src` without dropping its contents. We do this first,
497/// // to avoid problems in case something further down panics.
498/// src.set_len(0);
499///
500/// // The two regions cannot overlap because mutable references do
501/// // not alias, and two different vectors cannot own the same
502/// // memory.
503/// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
504///
505/// // Notify `dst` that it now holds the contents of `src`.
506/// dst.set_len(dst_len + src_len);
507/// }
508/// }
509///
510/// let mut a = vec!['r'];
511/// let mut b = vec!['u', 's', 't'];
512///
513/// append(&mut a, &mut b);
514///
515/// assert_eq!(a, &['r', 'u', 's', 't']);
516/// assert!(b.is_empty());
517/// ```
518///
519/// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
520#[doc(alias = "memcpy")]
521#[stable(feature = "rust1", since = "1.0.0")]
522#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
523#[inline(always)]
524#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
525#[rustc_diagnostic_item = "ptr_copy_nonoverlapping"]
526pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
527 ub_checks::assert_unsafe_precondition!(
528 check_language_ub,
529 "ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
530 and the specified memory ranges do not overlap",
531 (
532 src: *const () = src as *const (),
533 dst: *mut () = dst as *mut (),
534 size: usize = size_of::<T>(),
535 align: usize = align_of::<T>(),
536 count: usize = count,
537 ) => {
538 let zero_size = count == 0 || size == 0;
539 ub_checks::maybe_is_aligned_and_not_null(src, align, zero_size)
540 && ub_checks::maybe_is_aligned_and_not_null(dst, align, zero_size)
541 && ub_checks::maybe_is_nonoverlapping(src, dst, size, count)
542 }
543 );
544
545 // SAFETY: the safety contract for `copy_nonoverlapping` must be
546 // upheld by the caller.
547 unsafe { crate::intrinsics::copy_nonoverlapping(src, dst, count) }
548}
549
550/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
551/// and destination may overlap.
552///
553/// If the source and destination will *never* overlap,
554/// [`copy_nonoverlapping`] can be used instead.
555///
556/// `copy` is semantically equivalent to C's [`memmove`], but
557/// with the source and destination arguments swapped,
558/// and `count` counting the number of `T`s instead of bytes.
559/// Copying takes place as if the bytes were copied from `src`
560/// to a temporary array and then copied from the array to `dst`.
561///
562/// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
563/// requirements of `T`. The initialization state is preserved exactly.
564///
565/// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
566///
567/// # Safety
568///
569/// Behavior is undefined if any of the following conditions are violated:
570///
571/// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
572///
573/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes, and must remain valid even
574/// when `src` is read for `count * size_of::<T>()` bytes. (This means if the memory ranges
575/// overlap, the `dst` pointer must not be invalidated by `src` reads.)
576///
577/// * Both `src` and `dst` must be properly aligned.
578///
579/// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
580/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
581/// in the region beginning at `*src` and the region beginning at `*dst` can
582/// [violate memory safety][read-ownership].
583///
584/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
585/// `0`, the pointers must be properly aligned.
586///
587/// [`read`]: crate::ptr::read
588/// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
589/// [valid]: crate::ptr#safety
590///
591/// # Examples
592///
593/// Efficiently create a Rust vector from an unsafe buffer:
594///
595/// ```
596/// use std::ptr;
597///
598/// /// # Safety
599/// ///
600/// /// * `ptr` must be correctly aligned for its type and non-zero.
601/// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
602/// /// * Those elements must not be used after calling this function unless `T: Copy`.
603/// # #[allow(dead_code)]
604/// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
605/// let mut dst = Vec::with_capacity(elts);
606///
607/// // SAFETY: Our precondition ensures the source is aligned and valid,
608/// // and `Vec::with_capacity` ensures that we have usable space to write them.
609/// unsafe { ptr::copy(ptr, dst.as_mut_ptr(), elts); }
610///
611/// // SAFETY: We created it with this much capacity earlier,
612/// // and the previous `copy` has initialized these elements.
613/// unsafe { dst.set_len(elts); }
614/// dst
615/// }
616/// ```
617#[doc(alias = "memmove")]
618#[stable(feature = "rust1", since = "1.0.0")]
619#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
620#[inline(always)]
621#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
622#[rustc_diagnostic_item = "ptr_copy"]
623pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
624 // SAFETY: the safety contract for `copy` must be upheld by the caller.
625 unsafe {
626 ub_checks::assert_unsafe_precondition!(
627 check_language_ub,
628 "ptr::copy requires that both pointer arguments are aligned and non-null",
629 (
630 src: *const () = src as *const (),
631 dst: *mut () = dst as *mut (),
632 align: usize = align_of::<T>(),
633 zero_size: bool = T::IS_ZST || count == 0,
634 ) =>
635 ub_checks::maybe_is_aligned_and_not_null(src, align, zero_size)
636 && ub_checks::maybe_is_aligned_and_not_null(dst, align, zero_size)
637 );
638 crate::intrinsics::copy(src, dst, count)
639 }
640}
641
642/// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
643/// `val`.
644///
645/// `write_bytes` is similar to C's [`memset`], but sets `count *
646/// size_of::<T>()` bytes to `val`.
647///
648/// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
649///
650/// # Safety
651///
652/// Behavior is undefined if any of the following conditions are violated:
653///
654/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
655///
656/// * `dst` must be properly aligned.
657///
658/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
659/// `0`, the pointer must be properly aligned.
660///
661/// Additionally, note that changing `*dst` in this way can easily lead to undefined behavior (UB)
662/// later if the written bytes are not a valid representation of some `T`. For instance, the
663/// following is an **incorrect** use of this function:
664///
665/// ```rust,no_run
666/// unsafe {
667/// let mut value: u8 = 0;
668/// let ptr: *mut bool = &mut value as *mut u8 as *mut bool;
669/// let _bool = ptr.read(); // This is fine, `ptr` points to a valid `bool`.
670/// ptr.write_bytes(42u8, 1); // This function itself does not cause UB...
671/// let _bool = ptr.read(); // ...but it makes this operation UB! ⚠️
672/// }
673/// ```
674///
675/// [valid]: crate::ptr#safety
676///
677/// # Examples
678///
679/// Basic usage:
680///
681/// ```
682/// use std::ptr;
683///
684/// let mut vec = vec![0u32; 4];
685/// unsafe {
686/// let vec_ptr = vec.as_mut_ptr();
687/// ptr::write_bytes(vec_ptr, 0xfe, 2);
688/// }
689/// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
690/// ```
691#[doc(alias = "memset")]
692#[stable(feature = "rust1", since = "1.0.0")]
693#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
694#[inline(always)]
695#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
696#[rustc_diagnostic_item = "ptr_write_bytes"]
697pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
698 // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
699 unsafe {
700 ub_checks::assert_unsafe_precondition!(
701 check_language_ub,
702 "ptr::write_bytes requires that the destination pointer is aligned and non-null",
703 (
704 addr: *const () = dst as *const (),
705 align: usize = align_of::<T>(),
706 zero_size: bool = T::IS_ZST || count == 0,
707 ) => ub_checks::maybe_is_aligned_and_not_null(addr, align, zero_size)
708 );
709 crate::intrinsics::write_bytes(dst, val, count)
710 }
711}
712
713/// Executes the destructor (if any) of the pointed-to value.
714///
715/// This is almost the same as calling [`ptr::read`] and discarding
716/// the result, but has the following advantages:
717// FIXME: say something more useful than "almost the same"?
718// There are open questions here: `read` requires the value to be fully valid, e.g. if `T` is a
719// `bool` it must be 0 or 1, if it is a reference then it must be dereferenceable. `drop_in_place`
720// only requires that `*to_drop` be "valid for dropping" and we have not defined what that means. In
721// Miri it currently (May 2024) requires nothing at all for types without drop glue.
722///
723/// * It is *required* to use `drop_in_place` to drop unsized types like
724/// trait objects, because they can't be read out onto the stack and
725/// dropped normally.
726///
727/// * It is friendlier to the optimizer to do this over [`ptr::read`] when
728/// dropping manually allocated memory (e.g., in the implementations of
729/// `Box`/`Rc`/`Vec`), as the compiler doesn't need to prove that it's
730/// sound to elide the copy.
731///
732/// * It can be used to drop [pinned] data when `T` is not `repr(packed)`
733/// (pinned data must not be moved before it is dropped).
734///
735/// Unaligned values cannot be dropped in place, they must be copied to an aligned
736/// location first using [`ptr::read_unaligned`]. For packed structs, this move is
737/// done automatically by the compiler. This means the fields of packed structs
738/// are not dropped in-place.
739///
740/// [`ptr::read`]: self::read
741/// [`ptr::read_unaligned`]: self::read_unaligned
742/// [pinned]: crate::pin
743///
744/// # Safety
745///
746/// Behavior is undefined if any of the following conditions are violated:
747///
748/// * `to_drop` must be [valid] for both reads and writes.
749///
750/// * `to_drop` must be properly aligned, even if `T` has size 0.
751///
752/// * `to_drop` must be nonnull, even if `T` has size 0.
753///
754/// * The value `to_drop` points to must be valid for dropping, which may mean
755/// it must uphold additional invariants. These invariants depend on the type
756/// of the value being dropped. For instance, when dropping a Box, the box's
757/// pointer to the heap must be valid.
758///
759/// * While `drop_in_place` is executing, the only way to access parts of
760/// `to_drop` is through the `&mut self` references supplied to the
761/// `Drop::drop` methods that `drop_in_place` invokes.
762///
763/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
764/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
765/// foo` counts as a use because it will cause the value to be dropped
766/// again. [`write()`] can be used to overwrite data without causing it to be
767/// dropped.
768///
769/// [valid]: self#safety
770///
771/// # Examples
772///
773/// Manually remove the last item from a vector:
774///
775/// ```
776/// use std::ptr;
777/// use std::rc::Rc;
778///
779/// let last = Rc::new(1);
780/// let weak = Rc::downgrade(&last);
781///
782/// let mut v = vec![Rc::new(0), last];
783///
784/// unsafe {
785/// // Get a raw pointer to the last element in `v`.
786/// let ptr = &mut v[1] as *mut _;
787/// // Shorten `v` to prevent the last item from being dropped. We do that first,
788/// // to prevent issues if the `drop_in_place` below panics.
789/// v.set_len(1);
790/// // Without a call `drop_in_place`, the last item would never be dropped,
791/// // and the memory it manages would be leaked.
792/// ptr::drop_in_place(ptr);
793/// }
794///
795/// assert_eq!(v, &[0.into()]);
796///
797/// // Ensure that the last item was dropped.
798/// assert!(weak.upgrade().is_none());
799/// ```
800#[stable(feature = "drop_in_place", since = "1.8.0")]
801#[lang = "drop_in_place"]
802#[allow(unconditional_recursion)]
803#[rustc_diagnostic_item = "ptr_drop_in_place"]
804pub unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T) {
805 // Code here does not matter - this is replaced by the
806 // real drop glue by the compiler.
807
808 // SAFETY: see comment above
809 unsafe { drop_in_place(to_drop) }
810}
811
812/// Creates a null raw pointer.
813///
814/// This function is equivalent to zero-initializing the pointer:
815/// `MaybeUninit::<*const T>::zeroed().assume_init()`.
816/// The resulting pointer has the address 0.
817///
818/// # Examples
819///
820/// ```
821/// use std::ptr;
822///
823/// let p: *const i32 = ptr::null();
824/// assert!(p.is_null());
825/// assert_eq!(p as usize, 0); // this pointer has the address 0
826/// ```
827#[inline(always)]
828#[must_use]
829#[stable(feature = "rust1", since = "1.0.0")]
830#[rustc_promotable]
831#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
832#[rustc_diagnostic_item = "ptr_null"]
833pub const fn null<T: PointeeSized + Thin>() -> *const T {
834 from_raw_parts(without_provenance::<()>(0), ())
835}
836
837/// Creates a null mutable raw pointer.
838///
839/// This function is equivalent to zero-initializing the pointer:
840/// `MaybeUninit::<*mut T>::zeroed().assume_init()`.
841/// The resulting pointer has the address 0.
842///
843/// # Examples
844///
845/// ```
846/// use std::ptr;
847///
848/// let p: *mut i32 = ptr::null_mut();
849/// assert!(p.is_null());
850/// assert_eq!(p as usize, 0); // this pointer has the address 0
851/// ```
852#[inline(always)]
853#[must_use]
854#[stable(feature = "rust1", since = "1.0.0")]
855#[rustc_promotable]
856#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
857#[rustc_diagnostic_item = "ptr_null_mut"]
858pub const fn null_mut<T: PointeeSized + Thin>() -> *mut T {
859 from_raw_parts_mut(without_provenance_mut::<()>(0), ())
860}
861
862/// Creates a pointer with the given address and no [provenance][crate::ptr#provenance].
863///
864/// This is equivalent to `ptr::null().with_addr(addr)`.
865///
866/// Without provenance, this pointer is not associated with any actual allocation. Such a
867/// no-provenance pointer may be used for zero-sized memory accesses (if suitably aligned), but
868/// non-zero-sized memory accesses with a no-provenance pointer are UB. No-provenance pointers are
869/// little more than a `usize` address in disguise.
870///
871/// This is different from `addr as *const T`, which creates a pointer that picks up a previously
872/// exposed provenance. See [`with_exposed_provenance`] for more details on that operation.
873///
874/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
875#[inline(always)]
876#[must_use]
877#[stable(feature = "strict_provenance", since = "1.84.0")]
878#[rustc_const_stable(feature = "strict_provenance", since = "1.84.0")]
879pub const fn without_provenance<T>(addr: usize) -> *const T {
880 without_provenance_mut(addr)
881}
882
883/// Creates a new pointer that is dangling, but non-null and well-aligned.
884///
885/// This is useful for initializing types which lazily allocate, like
886/// `Vec::new` does.
887///
888/// Note that the address of the returned pointer may potentially
889/// be that of a valid pointer, which means this must not be used
890/// as a "not yet initialized" sentinel value.
891/// Types that lazily allocate must track initialization by some other means.
892#[inline(always)]
893#[must_use]
894#[stable(feature = "strict_provenance", since = "1.84.0")]
895#[rustc_const_stable(feature = "strict_provenance", since = "1.84.0")]
896pub const fn dangling<T>() -> *const T {
897 dangling_mut()
898}
899
900/// Creates a pointer with the given address and no [provenance][crate::ptr#provenance].
901///
902/// This is equivalent to `ptr::null_mut().with_addr(addr)`.
903///
904/// Without provenance, this pointer is not associated with any actual allocation. Such a
905/// no-provenance pointer may be used for zero-sized memory accesses (if suitably aligned), but
906/// non-zero-sized memory accesses with a no-provenance pointer are UB. No-provenance pointers are
907/// little more than a `usize` address in disguise.
908///
909/// This is different from `addr as *mut T`, which creates a pointer that picks up a previously
910/// exposed provenance. See [`with_exposed_provenance_mut`] for more details on that operation.
911///
912/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
913#[inline(always)]
914#[must_use]
915#[stable(feature = "strict_provenance", since = "1.84.0")]
916#[rustc_const_stable(feature = "strict_provenance", since = "1.84.0")]
917#[allow(integer_to_ptr_transmutes)] // Expected semantics here.
918pub const fn without_provenance_mut<T>(addr: usize) -> *mut T {
919 // An int-to-pointer transmute currently has exactly the intended semantics: it creates a
920 // pointer without provenance. Note that this is *not* a stable guarantee about transmute
921 // semantics, it relies on sysroot crates having special status.
922 // SAFETY: every valid integer is also a valid pointer (as long as you don't dereference that
923 // pointer).
924 unsafe { mem::transmute(addr) }
925}
926
927/// Creates a new pointer that is dangling, but non-null and well-aligned.
928///
929/// This is useful for initializing types which lazily allocate, like
930/// `Vec::new` does.
931///
932/// Note that the address of the returned pointer may potentially
933/// be that of a valid pointer, which means this must not be used
934/// as a "not yet initialized" sentinel value.
935/// Types that lazily allocate must track initialization by some other means.
936#[inline(always)]
937#[must_use]
938#[stable(feature = "strict_provenance", since = "1.84.0")]
939#[rustc_const_stable(feature = "strict_provenance", since = "1.84.0")]
940pub const fn dangling_mut<T>() -> *mut T {
941 NonNull::dangling().as_ptr()
942}
943
944/// Converts an address back to a pointer, picking up some previously 'exposed'
945/// [provenance][crate::ptr#provenance].
946///
947/// This is fully equivalent to `addr as *const T`. The provenance of the returned pointer is that
948/// of *some* pointer that was previously exposed by passing it to
949/// [`expose_provenance`][pointer::expose_provenance], or a `ptr as usize` cast. In addition, memory
950/// which is outside the control of the Rust abstract machine (MMIO registers, for example) is
951/// always considered to be accessible with an exposed provenance, so long as this memory is disjoint
952/// from memory that will be used by the abstract machine such as the stack, heap, and statics.
953///
954/// The exact provenance that gets picked is not specified. The compiler will do its best to pick
955/// the "right" provenance for you (whatever that may be), but currently we cannot provide any
956/// guarantees about which provenance the resulting pointer will have -- and therefore there
957/// is no definite specification for which memory the resulting pointer may access.
958///
959/// If there is *no* previously 'exposed' provenance that justifies the way the returned pointer
960/// will be used, the program has undefined behavior. In particular, the aliasing rules still apply:
961/// pointers and references that have been invalidated due to aliasing accesses cannot be used
962/// anymore, even if they have been exposed!
963///
964/// Due to its inherent ambiguity, this operation may not be supported by tools that help you to
965/// stay conformant with the Rust memory model. It is recommended to use [Strict
966/// Provenance][self#strict-provenance] APIs such as [`with_addr`][pointer::with_addr] wherever
967/// possible.
968///
969/// On most platforms this will produce a value with the same bytes as the address. Platforms
970/// which need to store additional information in a pointer may not support this operation,
971/// since it is generally not possible to actually *compute* which provenance the returned
972/// pointer has to pick up.
973///
974/// This is an [Exposed Provenance][crate::ptr#exposed-provenance] API.
975#[must_use]
976#[inline(always)]
977#[stable(feature = "exposed_provenance", since = "1.84.0")]
978#[rustc_const_stable(feature = "const_exposed_provenance", since = "CURRENT_RUSTC_VERSION")]
979#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
980#[allow(fuzzy_provenance_casts)] // this *is* the explicit provenance API one should use instead
981pub const fn with_exposed_provenance<T>(addr: usize) -> *const T {
982 addr as *const T
983}
984
985/// Converts an address back to a mutable pointer, picking up some previously 'exposed'
986/// [provenance][crate::ptr#provenance].
987///
988/// This is fully equivalent to `addr as *mut T`. The provenance of the returned pointer is that
989/// of *some* pointer that was previously exposed by passing it to
990/// [`expose_provenance`][pointer::expose_provenance], or a `ptr as usize` cast. In addition, memory
991/// which is outside the control of the Rust abstract machine (MMIO registers, for example) is
992/// always considered to be accessible with an exposed provenance, so long as this memory is disjoint
993/// from memory that will be used by the abstract machine such as the stack, heap, and statics.
994///
995/// The exact provenance that gets picked is not specified. The compiler will do its best to pick
996/// the "right" provenance for you (whatever that may be), but currently we cannot provide any
997/// guarantees about which provenance the resulting pointer will have -- and therefore there
998/// is no definite specification for which memory the resulting pointer may access.
999///
1000/// If there is *no* previously 'exposed' provenance that justifies the way the returned pointer
1001/// will be used, the program has undefined behavior. In particular, the aliasing rules still apply:
1002/// pointers and references that have been invalidated due to aliasing accesses cannot be used
1003/// anymore, even if they have been exposed!
1004///
1005/// Due to its inherent ambiguity, this operation may not be supported by tools that help you to
1006/// stay conformant with the Rust memory model. It is recommended to use [Strict
1007/// Provenance][self#strict-provenance] APIs such as [`with_addr`][pointer::with_addr] wherever
1008/// possible.
1009///
1010/// On most platforms this will produce a value with the same bytes as the address. Platforms
1011/// which need to store additional information in a pointer may not support this operation,
1012/// since it is generally not possible to actually *compute* which provenance the returned
1013/// pointer has to pick up.
1014///
1015/// This is an [Exposed Provenance][crate::ptr#exposed-provenance] API.
1016#[must_use]
1017#[inline(always)]
1018#[stable(feature = "exposed_provenance", since = "1.84.0")]
1019#[rustc_const_stable(feature = "const_exposed_provenance", since = "CURRENT_RUSTC_VERSION")]
1020#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
1021#[allow(fuzzy_provenance_casts)] // this *is* the explicit provenance API one should use instead
1022pub const fn with_exposed_provenance_mut<T>(addr: usize) -> *mut T {
1023 addr as *mut T
1024}
1025
1026/// Converts a reference to a raw pointer.
1027///
1028/// For `r: &T`, `from_ref(r)` is equivalent to `r as *const T` (except for the caveat noted below),
1029/// but is a bit safer since it will never silently change type or mutability, in particular if the
1030/// code is refactored.
1031///
1032/// The caller must ensure that the pointee outlives the pointer this function returns, or else it
1033/// will end up dangling.
1034///
1035/// The caller must also ensure that the memory the pointer (non-transitively) points to is never
1036/// written to (except inside an `UnsafeCell`) using this pointer or any pointer derived from it. If
1037/// you need to mutate the pointee, use [`from_mut`]. Specifically, to turn a mutable reference `m:
1038/// &mut T` into `*const T`, prefer `from_mut(m).cast_const()` to obtain a pointer that can later be
1039/// used for mutation.
1040///
1041/// ## Interaction with lifetime extension
1042///
1043/// Note that this has subtle interactions with the rules for lifetime extension of temporaries in
1044/// tail expressions. This code is valid, albeit in a non-obvious way:
1045/// ```rust
1046/// # type T = i32;
1047/// # fn foo() -> T { 42 }
1048/// // The temporary holding the return value of `foo` has its lifetime extended,
1049/// // because the surrounding expression involves no function call.
1050/// let p = &foo() as *const T;
1051/// unsafe { p.read() };
1052/// ```
1053/// Naively replacing the cast with `from_ref` is not valid:
1054/// ```rust,no_run
1055/// # use std::ptr;
1056/// # type T = i32;
1057/// # fn foo() -> T { 42 }
1058/// // The temporary holding the return value of `foo` does *not* have its lifetime extended,
1059/// // because the surrounding expression involves a function call.
1060/// let p = ptr::from_ref(&foo());
1061/// unsafe { p.read() }; // UB! Reading from a dangling pointer ⚠️
1062/// ```
1063/// The recommended way to write this code is to avoid relying on lifetime extension
1064/// when raw pointers are involved:
1065/// ```rust
1066/// # use std::ptr;
1067/// # type T = i32;
1068/// # fn foo() -> T { 42 }
1069/// let x = foo();
1070/// let p = ptr::from_ref(&x);
1071/// unsafe { p.read() };
1072/// ```
1073#[inline(always)]
1074#[must_use]
1075#[stable(feature = "ptr_from_ref", since = "1.76.0")]
1076#[rustc_const_stable(feature = "ptr_from_ref", since = "1.76.0")]
1077#[rustc_never_returns_null_ptr]
1078#[rustc_diagnostic_item = "ptr_from_ref"]
1079pub const fn from_ref<T: PointeeSized>(r: &T) -> *const T {
1080 r
1081}
1082
1083/// Converts a mutable reference to a raw pointer.
1084///
1085/// For `r: &mut T`, `from_mut(r)` is equivalent to `r as *mut T` (except for the caveat noted
1086/// below), but is a bit safer since it will never silently change type or mutability, in particular
1087/// if the code is refactored.
1088///
1089/// The caller must ensure that the pointee outlives the pointer this function returns, or else it
1090/// will end up dangling.
1091///
1092/// ## Interaction with lifetime extension
1093///
1094/// Note that this has subtle interactions with the rules for lifetime extension of temporaries in
1095/// tail expressions. This code is valid, albeit in a non-obvious way:
1096/// ```rust
1097/// # type T = i32;
1098/// # fn foo() -> T { 42 }
1099/// // The temporary holding the return value of `foo` has its lifetime extended,
1100/// // because the surrounding expression involves no function call.
1101/// let p = &mut foo() as *mut T;
1102/// unsafe { p.write(T::default()) };
1103/// ```
1104/// Naively replacing the cast with `from_mut` is not valid:
1105/// ```rust,no_run
1106/// # use std::ptr;
1107/// # type T = i32;
1108/// # fn foo() -> T { 42 }
1109/// // The temporary holding the return value of `foo` does *not* have its lifetime extended,
1110/// // because the surrounding expression involves a function call.
1111/// let p = ptr::from_mut(&mut foo());
1112/// unsafe { p.write(T::default()) }; // UB! Writing to a dangling pointer ⚠️
1113/// ```
1114/// The recommended way to write this code is to avoid relying on lifetime extension
1115/// when raw pointers are involved:
1116/// ```rust
1117/// # use std::ptr;
1118/// # type T = i32;
1119/// # fn foo() -> T { 42 }
1120/// let mut x = foo();
1121/// let p = ptr::from_mut(&mut x);
1122/// unsafe { p.write(T::default()) };
1123/// ```
1124#[inline(always)]
1125#[must_use]
1126#[stable(feature = "ptr_from_ref", since = "1.76.0")]
1127#[rustc_const_stable(feature = "ptr_from_ref", since = "1.76.0")]
1128#[rustc_never_returns_null_ptr]
1129pub const fn from_mut<T: PointeeSized>(r: &mut T) -> *mut T {
1130 r
1131}
1132
1133/// Forms a raw slice from a pointer and a length.
1134///
1135/// The `len` argument is the number of **elements**, not the number of bytes.
1136///
1137/// This function is safe, but actually using the return value is unsafe.
1138/// See the documentation of [`slice::from_raw_parts`] for slice safety requirements.
1139///
1140/// [`slice::from_raw_parts`]: crate::slice::from_raw_parts
1141///
1142/// # Examples
1143///
1144/// ```rust
1145/// use std::ptr;
1146///
1147/// // create a slice pointer when starting out with a pointer to the first element
1148/// let x = [5, 6, 7];
1149/// let raw_pointer = x.as_ptr();
1150/// let slice = ptr::slice_from_raw_parts(raw_pointer, 3);
1151/// assert_eq!(unsafe { &*slice }[2], 7);
1152/// ```
1153///
1154/// You must ensure that the pointer is valid and not null before dereferencing
1155/// the raw slice. A slice reference must never have a null pointer, even if it's empty.
1156///
1157/// ```rust,should_panic
1158/// use std::ptr;
1159/// let danger: *const [u8] = ptr::slice_from_raw_parts(ptr::null(), 0);
1160/// unsafe {
1161/// danger.as_ref().expect("references must not be null");
1162/// }
1163/// ```
1164#[inline]
1165#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
1166#[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")]
1167#[rustc_diagnostic_item = "ptr_slice_from_raw_parts"]
1168pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
1169 from_raw_parts(data, len)
1170}
1171
1172/// Forms a raw mutable slice from a pointer and a length.
1173///
1174/// The `len` argument is the number of **elements**, not the number of bytes.
1175///
1176/// Performs the same functionality as [`slice_from_raw_parts`], except that a
1177/// raw mutable slice is returned, as opposed to a raw immutable slice.
1178///
1179/// This function is safe, but actually using the return value is unsafe.
1180/// See the documentation of [`slice::from_raw_parts_mut`] for slice safety requirements.
1181///
1182/// [`slice::from_raw_parts_mut`]: crate::slice::from_raw_parts_mut
1183///
1184/// # Examples
1185///
1186/// ```rust
1187/// use std::ptr;
1188///
1189/// let x = &mut [5, 6, 7];
1190/// let raw_pointer = x.as_mut_ptr();
1191/// let slice = ptr::slice_from_raw_parts_mut(raw_pointer, 3);
1192///
1193/// unsafe {
1194/// (*slice)[2] = 99; // assign a value at an index in the slice
1195/// };
1196///
1197/// assert_eq!(unsafe { &*slice }[2], 99);
1198/// ```
1199///
1200/// You must ensure that the pointer is valid and not null before dereferencing
1201/// the raw slice. A slice reference must never have a null pointer, even if it's empty.
1202///
1203/// ```rust,should_panic
1204/// use std::ptr;
1205/// let danger: *mut [u8] = ptr::slice_from_raw_parts_mut(ptr::null_mut(), 0);
1206/// unsafe {
1207/// danger.as_mut().expect("references must not be null");
1208/// }
1209/// ```
1210#[inline]
1211#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
1212#[rustc_const_stable(feature = "const_slice_from_raw_parts_mut", since = "1.83.0")]
1213#[rustc_diagnostic_item = "ptr_slice_from_raw_parts_mut"]
1214pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
1215 from_raw_parts_mut(data, len)
1216}
1217
1218/// Swaps the values at two mutable locations of the same type, without
1219/// deinitializing either.
1220///
1221/// But for the following exceptions, this function is semantically
1222/// equivalent to [`mem::swap`]:
1223///
1224/// * It operates on raw pointers instead of references. When references are
1225/// available, [`mem::swap`] should be preferred.
1226///
1227/// * The two pointed-to values may overlap. If the values do overlap, then the
1228/// overlapping region of memory from `x` will be used. This is demonstrated
1229/// in the second example below.
1230///
1231/// * The operation is "untyped" in the sense that data may be uninitialized or otherwise violate
1232/// the requirements of `T`. The initialization state is preserved exactly.
1233///
1234/// # Safety
1235///
1236/// Behavior is undefined if any of the following conditions are violated:
1237///
1238/// * Both `x` and `y` must be [valid] for both reads and writes. They must remain valid even when the
1239/// other pointer is written. (This means if the memory ranges overlap, the two pointers must not
1240/// be subject to aliasing restrictions relative to each other.)
1241///
1242/// * Both `x` and `y` must be properly aligned.
1243///
1244/// Note that even if `T` has size `0`, the pointers must be properly aligned.
1245///
1246/// [valid]: self#safety
1247///
1248/// # Examples
1249///
1250/// Swapping two non-overlapping regions:
1251///
1252/// ```
1253/// use std::ptr;
1254///
1255/// let mut array = [0, 1, 2, 3];
1256///
1257/// let (x, y) = array.split_at_mut(2);
1258/// let x = x.as_mut_ptr().cast::<[u32; 2]>(); // this is `array[0..2]`
1259/// let y = y.as_mut_ptr().cast::<[u32; 2]>(); // this is `array[2..4]`
1260///
1261/// unsafe {
1262/// ptr::swap(x, y);
1263/// assert_eq!([2, 3, 0, 1], array);
1264/// }
1265/// ```
1266///
1267/// Swapping two overlapping regions:
1268///
1269/// ```
1270/// use std::ptr;
1271///
1272/// let mut array: [i32; 4] = [0, 1, 2, 3];
1273///
1274/// let array_ptr: *mut i32 = array.as_mut_ptr();
1275///
1276/// let x = array_ptr as *mut [i32; 3]; // this is `array[0..3]`
1277/// let y = unsafe { array_ptr.add(1) } as *mut [i32; 3]; // this is `array[1..4]`
1278///
1279/// unsafe {
1280/// ptr::swap(x, y);
1281/// // The indices `1..3` of the slice overlap between `x` and `y`.
1282/// // Reasonable results would be for to them be `[2, 3]`, so that indices `0..3` are
1283/// // `[1, 2, 3]` (matching `y` before the `swap`); or for them to be `[0, 1]`
1284/// // so that indices `1..4` are `[0, 1, 2]` (matching `x` before the `swap`).
1285/// // This implementation is defined to make the latter choice.
1286/// assert_eq!([1, 0, 1, 2], array);
1287/// }
1288/// ```
1289#[inline]
1290#[stable(feature = "rust1", since = "1.0.0")]
1291#[rustc_const_stable(feature = "const_swap", since = "1.85.0")]
1292#[rustc_diagnostic_item = "ptr_swap"]
1293pub const unsafe fn swap<T>(x: *mut T, y: *mut T) {
1294 // Give ourselves some scratch space to work with.
1295 // We do not have to worry about drops: `MaybeUninit` does nothing when dropped.
1296 let mut tmp = MaybeUninit::<T>::uninit();
1297
1298 // Perform the swap
1299 // SAFETY: the caller must guarantee that `x` and `y` are
1300 // valid for writes and properly aligned. `tmp` cannot be
1301 // overlapping either `x` or `y` because `tmp` was just allocated
1302 // on the stack as a separate allocation.
1303 unsafe {
1304 copy_nonoverlapping(x, tmp.as_mut_ptr(), 1);
1305 copy(y, x, 1); // `x` and `y` may overlap
1306 copy_nonoverlapping(tmp.as_ptr(), y, 1);
1307 }
1308}
1309
1310/// Swaps `count * size_of::<T>()` bytes between the two regions of memory
1311/// beginning at `x` and `y`. The two regions must *not* overlap.
1312///
1313/// The operation is "untyped" in the sense that data may be uninitialized or otherwise violate the
1314/// requirements of `T`. The initialization state is preserved exactly.
1315///
1316/// # Safety
1317///
1318/// Behavior is undefined if any of the following conditions are violated:
1319///
1320/// * Both `x` and `y` must be [valid] for both reads and writes of `count *
1321/// size_of::<T>()` bytes.
1322///
1323/// * Both `x` and `y` must be properly aligned.
1324///
1325/// * The region of memory beginning at `x` with a size of `count *
1326/// size_of::<T>()` bytes must *not* overlap with the region of memory
1327/// beginning at `y` with the same size.
1328///
1329/// Note that even if the effectively copied size (`count * size_of::<T>()`) is `0`,
1330/// the pointers must be properly aligned.
1331///
1332/// [valid]: self#safety
1333///
1334/// # Examples
1335///
1336/// Basic usage:
1337///
1338/// ```
1339/// use std::ptr;
1340///
1341/// let mut x = [1, 2, 3, 4];
1342/// let mut y = [7, 8, 9];
1343///
1344/// unsafe {
1345/// ptr::swap_nonoverlapping(x.as_mut_ptr(), y.as_mut_ptr(), 2);
1346/// }
1347///
1348/// assert_eq!(x, [7, 8, 3, 4]);
1349/// assert_eq!(y, [1, 2, 9]);
1350/// ```
1351#[inline]
1352#[stable(feature = "swap_nonoverlapping", since = "1.27.0")]
1353#[rustc_const_stable(feature = "const_swap_nonoverlapping", since = "1.88.0")]
1354#[rustc_diagnostic_item = "ptr_swap_nonoverlapping"]
1355#[rustc_allow_const_fn_unstable(const_eval_select)] // both implementations behave the same
1356#[track_caller]
1357pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
1358 ub_checks::assert_unsafe_precondition!(
1359 check_library_ub,
1360 "ptr::swap_nonoverlapping requires that both pointer arguments are aligned and non-null \
1361 and the specified memory ranges do not overlap",
1362 (
1363 x: *mut () = x as *mut (),
1364 y: *mut () = y as *mut (),
1365 size: usize = size_of::<T>(),
1366 align: usize = align_of::<T>(),
1367 count: usize = count,
1368 ) => {
1369 let zero_size = size == 0 || count == 0;
1370 ub_checks::maybe_is_aligned_and_not_null(x, align, zero_size)
1371 && ub_checks::maybe_is_aligned_and_not_null(y, align, zero_size)
1372 && ub_checks::maybe_is_nonoverlapping(x, y, size, count)
1373 }
1374 );
1375
1376 const_eval_select!(
1377 @capture[T] { x: *mut T, y: *mut T, count: usize }:
1378 if const {
1379 // At compile-time we don't need all the special code below.
1380 // SAFETY: Same preconditions as this function
1381 unsafe { swap_nonoverlapping_const(x, y, count) }
1382 } else {
1383 // Going though a slice here helps codegen know the size fits in `isize`
1384 let slice = slice_from_raw_parts_mut(x, count);
1385 // SAFETY: This is all readable from the pointer, meaning it's one
1386 // allocation, and thus cannot be more than isize::MAX bytes.
1387 let bytes = unsafe { mem::size_of_val_raw::<[T]>(slice) };
1388 if let Some(bytes) = NonZero::new(bytes) {
1389 // SAFETY: These are the same ranges, just expressed in a different
1390 // type, so they're still non-overlapping.
1391 unsafe { swap_nonoverlapping_bytes(x.cast(), y.cast(), bytes) };
1392 }
1393 }
1394 )
1395}
1396
1397/// Same behavior and safety conditions as [`swap_nonoverlapping`]
1398#[inline]
1399const unsafe fn swap_nonoverlapping_const<T>(x: *mut T, y: *mut T, count: usize) {
1400 let mut i = 0;
1401 while i < count {
1402 // SAFETY: By precondition, `i` is in-bounds because it's below `n`
1403 let x = unsafe { x.add(i) };
1404 // SAFETY: By precondition, `i` is in-bounds because it's below `n`
1405 // and it's distinct from `x` since the ranges are non-overlapping
1406 let y = unsafe { y.add(i) };
1407
1408 // SAFETY: we're only ever given pointers that are valid to read/write,
1409 // including being aligned, and nothing here panics so it's drop-safe.
1410 unsafe {
1411 // Note that it's critical that these use `copy_nonoverlapping`,
1412 // rather than `read`/`write`, to avoid #134713 if T has padding.
1413 let mut temp = MaybeUninit::<T>::uninit();
1414 copy_nonoverlapping(x, temp.as_mut_ptr(), 1);
1415 copy_nonoverlapping(y, x, 1);
1416 copy_nonoverlapping(temp.as_ptr(), y, 1);
1417 }
1418
1419 i += 1;
1420 }
1421}
1422
1423// Don't let MIR inline this, because we really want it to keep its noalias metadata
1424#[rustc_no_mir_inline]
1425#[inline]
1426fn swap_chunk<const N: usize>(x: &mut MaybeUninit<[u8; N]>, y: &mut MaybeUninit<[u8; N]>) {
1427 let a = *x;
1428 let b = *y;
1429 *x = b;
1430 *y = a;
1431}
1432
1433#[inline]
1434unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, bytes: NonZero<usize>) {
1435 // Same as `swap_nonoverlapping::<[u8; N]>`.
1436 unsafe fn swap_nonoverlapping_chunks<const N: usize>(
1437 x: *mut MaybeUninit<[u8; N]>,
1438 y: *mut MaybeUninit<[u8; N]>,
1439 chunks: NonZero<usize>,
1440 ) {
1441 let chunks = chunks.get();
1442 for i in 0..chunks {
1443 // SAFETY: i is in [0, chunks) so the adds and dereferences are in-bounds.
1444 unsafe { swap_chunk(&mut *x.add(i), &mut *y.add(i)) };
1445 }
1446 }
1447
1448 // Same as `swap_nonoverlapping_bytes`, but accepts at most 1+2+4=7 bytes
1449 #[inline]
1450 unsafe fn swap_nonoverlapping_short(x: *mut u8, y: *mut u8, bytes: NonZero<usize>) {
1451 // Tail handling for auto-vectorized code sometimes has element-at-a-time behaviour,
1452 // see <https://github.com/rust-lang/rust/issues/134946>.
1453 // By swapping as different sizes, rather than as a loop over bytes,
1454 // we make sure not to end up with, say, seven byte-at-a-time copies.
1455
1456 let bytes = bytes.get();
1457 let mut i = 0;
1458 macro_rules! swap_prefix {
1459 ($($n:literal)+) => {$(
1460 if (bytes & $n) != 0 {
1461 // SAFETY: `i` can only have the same bits set as those in bytes,
1462 // so these `add`s are in-bounds of `bytes`. But the bit for
1463 // `$n` hasn't been set yet, so the `$n` bytes that `swap_chunk`
1464 // will read and write are within the usable range.
1465 unsafe { swap_chunk::<$n>(&mut*x.add(i).cast(), &mut*y.add(i).cast()) };
1466 i |= $n;
1467 }
1468 )+};
1469 }
1470 swap_prefix!(4 2 1);
1471 debug_assert_eq!(i, bytes);
1472 }
1473
1474 const CHUNK_SIZE: usize = size_of::<*const ()>();
1475 let bytes = bytes.get();
1476
1477 let chunks = bytes / CHUNK_SIZE;
1478 let tail = bytes % CHUNK_SIZE;
1479 if let Some(chunks) = NonZero::new(chunks) {
1480 // SAFETY: this is bytes/CHUNK_SIZE*CHUNK_SIZE bytes, which is <= bytes,
1481 // so it's within the range of our non-overlapping bytes.
1482 unsafe { swap_nonoverlapping_chunks::<CHUNK_SIZE>(x.cast(), y.cast(), chunks) };
1483 }
1484 if let Some(tail) = NonZero::new(tail) {
1485 const { assert!(CHUNK_SIZE <= 8) };
1486 let delta = chunks * CHUNK_SIZE;
1487 // SAFETY: the tail length is below CHUNK SIZE because of the remainder,
1488 // and CHUNK_SIZE is at most 8 by the const assert, so tail <= 7
1489 unsafe { swap_nonoverlapping_short(x.add(delta), y.add(delta), tail) };
1490 }
1491}
1492
1493/// Moves `src` into the pointed `dst`, returning the previous `dst` value.
1494///
1495/// Neither value is dropped.
1496///
1497/// This function is semantically equivalent to [`mem::replace`] except that it
1498/// operates on raw pointers instead of references. When references are
1499/// available, [`mem::replace`] should be preferred.
1500///
1501/// # Safety
1502///
1503/// Behavior is undefined if any of the following conditions are violated:
1504///
1505/// * `dst` must be [valid] for both reads and writes.
1506///
1507/// * `dst` must be properly aligned.
1508///
1509/// * `dst` must point to a properly initialized value of type `T`.
1510///
1511/// Note that even if `T` has size `0`, the pointer must be properly aligned.
1512///
1513/// [valid]: self#safety
1514///
1515/// # Examples
1516///
1517/// ```
1518/// use std::ptr;
1519///
1520/// let mut rust = vec!['b', 'u', 's', 't'];
1521///
1522/// // `mem::replace` would have the same effect without requiring the unsafe
1523/// // block.
1524/// let b = unsafe {
1525/// ptr::replace(&mut rust[0], 'r')
1526/// };
1527///
1528/// assert_eq!(b, 'b');
1529/// assert_eq!(rust, &['r', 'u', 's', 't']);
1530/// ```
1531#[inline]
1532#[stable(feature = "rust1", since = "1.0.0")]
1533#[rustc_const_stable(feature = "const_replace", since = "1.83.0")]
1534#[rustc_diagnostic_item = "ptr_replace"]
1535#[track_caller]
1536pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
1537 // SAFETY: the caller must guarantee that `dst` is valid to be
1538 // cast to a mutable reference (valid for writes, aligned, initialized),
1539 // and cannot overlap `src` since `dst` must point to a distinct
1540 // allocation.
1541 unsafe {
1542 ub_checks::assert_unsafe_precondition!(
1543 check_language_ub,
1544 "ptr::replace requires that the pointer argument is aligned and non-null",
1545 (
1546 addr: *const () = dst as *const (),
1547 align: usize = align_of::<T>(),
1548 is_zst: bool = T::IS_ZST,
1549 ) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
1550 );
1551 mem::replace(&mut *dst, src)
1552 }
1553}
1554
1555/// Reads the value from `src` without moving it. This leaves the
1556/// memory in `src` unchanged.
1557///
1558/// # Safety
1559///
1560/// Behavior is undefined if any of the following conditions are violated:
1561///
1562/// * `src` must be [valid] for reads.
1563///
1564/// * `src` must be properly aligned. Use [`read_unaligned`] if this is not the
1565/// case.
1566///
1567/// * `src` must point to a properly initialized value of type `T`.
1568///
1569/// Note that even if `T` has size `0`, the pointer must be properly aligned.
1570///
1571/// # Examples
1572///
1573/// Basic usage:
1574///
1575/// ```
1576/// let x = 12;
1577/// let y = &x as *const i32;
1578///
1579/// unsafe {
1580/// assert_eq!(std::ptr::read(y), 12);
1581/// }
1582/// ```
1583///
1584/// Manually implement [`mem::swap`]:
1585///
1586/// ```
1587/// use std::ptr;
1588///
1589/// fn swap<T>(a: &mut T, b: &mut T) {
1590/// unsafe {
1591/// // Create a bitwise copy of the value at `a` in `tmp`.
1592/// let tmp = ptr::read(a);
1593///
1594/// // Exiting at this point (either by explicitly returning or by
1595/// // calling a function which panics) would cause the value in `tmp` to
1596/// // be dropped while the same value is still referenced by `a`. This
1597/// // could trigger undefined behavior if `T` is not `Copy`.
1598///
1599/// // Create a bitwise copy of the value at `b` in `a`.
1600/// // This is safe because mutable references cannot alias.
1601/// ptr::copy_nonoverlapping(b, a, 1);
1602///
1603/// // As above, exiting here could trigger undefined behavior because
1604/// // the same value is referenced by `a` and `b`.
1605///
1606/// // Move `tmp` into `b`.
1607/// ptr::write(b, tmp);
1608///
1609/// // `tmp` has been moved (`write` takes ownership of its second argument),
1610/// // so nothing is dropped implicitly here.
1611/// }
1612/// }
1613///
1614/// let mut foo = "foo".to_owned();
1615/// let mut bar = "bar".to_owned();
1616///
1617/// swap(&mut foo, &mut bar);
1618///
1619/// assert_eq!(foo, "bar");
1620/// assert_eq!(bar, "foo");
1621/// ```
1622///
1623/// ## Ownership of the Returned Value
1624///
1625/// `read` creates a bitwise copy of `T`, regardless of whether `T` is [`Copy`].
1626/// If `T` is not [`Copy`], using both the returned value and the value at
1627/// `*src` can violate memory safety. Note that assigning to `*src` counts as a
1628/// use because it will attempt to drop the value at `*src`.
1629///
1630/// [`write()`] can be used to overwrite data without causing it to be dropped.
1631///
1632/// ```
1633/// use std::ptr;
1634///
1635/// let mut s = String::from("foo");
1636/// unsafe {
1637/// // `s2` now points to the same underlying memory as `s`.
1638/// let mut s2: String = ptr::read(&s);
1639///
1640/// assert_eq!(s2, "foo");
1641///
1642/// // Assigning to `s2` causes its original value to be dropped. Beyond
1643/// // this point, `s` must no longer be used, as the underlying memory has
1644/// // been freed.
1645/// s2 = String::default();
1646/// assert_eq!(s2, "");
1647///
1648/// // Assigning to `s` would cause the old value to be dropped again,
1649/// // resulting in undefined behavior.
1650/// // s = String::from("bar"); // ERROR
1651///
1652/// // `ptr::write` can be used to overwrite a value without dropping it.
1653/// ptr::write(&mut s, String::from("bar"));
1654/// }
1655///
1656/// assert_eq!(s, "bar");
1657/// ```
1658///
1659/// [valid]: self#safety
1660#[inline]
1661#[stable(feature = "rust1", since = "1.0.0")]
1662#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
1663#[track_caller]
1664#[rustc_diagnostic_item = "ptr_read"]
1665pub const unsafe fn read<T>(src: *const T) -> T {
1666 // It would be semantically correct to implement this via `copy_nonoverlapping`
1667 // and `MaybeUninit`, as was done before PR #109035. Calling `assume_init`
1668 // provides enough information to know that this is a typed operation.
1669
1670 // However, as of March 2023 the compiler was not capable of taking advantage
1671 // of that information. Thus, the implementation here switched to an intrinsic,
1672 // which lowers to `_0 = *src` in MIR, to address a few issues:
1673 //
1674 // - Using `MaybeUninit::assume_init` after a `copy_nonoverlapping` was not
1675 // turning the untyped copy into a typed load. As such, the generated
1676 // `load` in LLVM didn't get various metadata, such as `!range` (#73258),
1677 // `!nonnull`, and `!noundef`, resulting in poorer optimization.
1678 // - Going through the extra local resulted in multiple extra copies, even
1679 // in optimized MIR. (Ignoring StorageLive/Dead, the intrinsic is one
1680 // MIR statement, while the previous implementation was eight.) LLVM
1681 // could sometimes optimize them away, but because `read` is at the core
1682 // of so many things, not having them in the first place improves what we
1683 // hand off to the backend. For example, `mem::replace::<Big>` previously
1684 // emitted 4 `alloca` and 6 `memcpy`s, but is now 1 `alloc` and 3 `memcpy`s.
1685 // - In general, this approach keeps us from getting any more bugs (like
1686 // #106369) that boil down to "`read(p)` is worse than `*p`", as this
1687 // makes them look identical to the backend (or other MIR consumers).
1688 //
1689 // Future enhancements to MIR optimizations might well allow this to return
1690 // to the previous implementation, rather than using an intrinsic.
1691
1692 // SAFETY: the caller must guarantee that `src` is valid for reads.
1693 unsafe {
1694 #[cfg(debug_assertions)] // Too expensive to always enable (for now?)
1695 ub_checks::assert_unsafe_precondition!(
1696 check_language_ub,
1697 "ptr::read requires that the pointer argument is aligned and non-null",
1698 (
1699 addr: *const () = src as *const (),
1700 align: usize = align_of::<T>(),
1701 is_zst: bool = T::IS_ZST,
1702 ) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
1703 );
1704 crate::intrinsics::read_via_copy(src)
1705 }
1706}
1707
1708/// Reads the value from `src` without moving it. This leaves the
1709/// memory in `src` unchanged.
1710///
1711/// Unlike [`read`], `read_unaligned` works with unaligned pointers.
1712///
1713/// # Safety
1714///
1715/// Behavior is undefined if any of the following conditions are violated:
1716///
1717/// * `src` must be [valid] for reads.
1718///
1719/// * `src` must point to a properly initialized value of type `T`.
1720///
1721/// Like [`read`], `read_unaligned` creates a bitwise copy of `T`, regardless of
1722/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
1723/// value and the value at `*src` can [violate memory safety][read-ownership].
1724///
1725/// [read-ownership]: read#ownership-of-the-returned-value
1726/// [valid]: self#safety
1727///
1728/// ## On `packed` structs
1729///
1730/// Attempting to create a raw pointer to an `unaligned` struct field with
1731/// an expression such as `&packed.unaligned as *const FieldType` creates an
1732/// intermediate unaligned reference before converting that to a raw pointer.
1733/// That this reference is temporary and immediately cast is inconsequential
1734/// as the compiler always expects references to be properly aligned.
1735/// As a result, using `&packed.unaligned as *const FieldType` causes immediate
1736/// *undefined behavior* in your program.
1737///
1738/// Instead you must use the `&raw const` syntax to create the pointer.
1739/// You may use that constructed pointer together with this function.
1740///
1741/// An example of what not to do and how this relates to `read_unaligned` is:
1742///
1743/// ```
1744/// #[repr(packed, C)]
1745/// struct Packed {
1746/// _padding: u8,
1747/// unaligned: u32,
1748/// }
1749///
1750/// let packed = Packed {
1751/// _padding: 0x00,
1752/// unaligned: 0x01020304,
1753/// };
1754///
1755/// // Take the address of a 32-bit integer which is not aligned.
1756/// // In contrast to `&packed.unaligned as *const _`, this has no undefined behavior.
1757/// let unaligned = &raw const packed.unaligned;
1758///
1759/// let v = unsafe { std::ptr::read_unaligned(unaligned) };
1760/// assert_eq!(v, 0x01020304);
1761/// ```
1762///
1763/// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however.
1764///
1765/// # Examples
1766///
1767/// Read a `usize` value from a byte buffer:
1768///
1769/// ```
1770/// fn read_usize(x: &[u8]) -> usize {
1771/// assert!(x.len() >= size_of::<usize>());
1772///
1773/// let ptr = x.as_ptr() as *const usize;
1774///
1775/// unsafe { ptr.read_unaligned() }
1776/// }
1777/// ```
1778#[inline]
1779#[stable(feature = "ptr_unaligned", since = "1.17.0")]
1780#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
1781#[track_caller]
1782#[rustc_diagnostic_item = "ptr_read_unaligned"]
1783pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
1784 let mut tmp = MaybeUninit::<T>::uninit();
1785 // SAFETY: the caller must guarantee that `src` is valid for reads.
1786 // `src` cannot overlap `tmp` because `tmp` was just allocated on
1787 // the stack as a separate allocation.
1788 //
1789 // Also, since we just wrote a valid value into `tmp`, it is guaranteed
1790 // to be properly initialized.
1791 unsafe {
1792 copy_nonoverlapping(src as *const u8, tmp.as_mut_ptr() as *mut u8, size_of::<T>());
1793 tmp.assume_init()
1794 }
1795}
1796
1797/// Overwrites a memory location with the given value without reading or
1798/// dropping the old value.
1799///
1800/// `write` does not drop the contents of `dst`. This is safe, but it could leak
1801/// allocations or resources, so care should be taken not to overwrite an object
1802/// that should be dropped.
1803///
1804/// Additionally, it does not drop `src`. Semantically, `src` is moved into the
1805/// location pointed to by `dst`.
1806///
1807/// This is appropriate for initializing uninitialized memory, or overwriting
1808/// memory that has previously been [`read`] from.
1809///
1810/// # Safety
1811///
1812/// Behavior is undefined if any of the following conditions are violated:
1813///
1814/// * `dst` must be [valid] for writes.
1815///
1816/// * `dst` must be properly aligned. Use [`write_unaligned`] if this is not the
1817/// case.
1818///
1819/// Note that even if `T` has size `0`, the pointer must be properly aligned.
1820///
1821/// [valid]: self#safety
1822///
1823/// # Examples
1824///
1825/// Basic usage:
1826///
1827/// ```
1828/// let mut x = 0;
1829/// let y = &mut x as *mut i32;
1830/// let z = 12;
1831///
1832/// unsafe {
1833/// std::ptr::write(y, z);
1834/// assert_eq!(std::ptr::read(y), 12);
1835/// }
1836/// ```
1837///
1838/// Manually implement [`mem::swap`]:
1839///
1840/// ```
1841/// use std::ptr;
1842///
1843/// fn swap<T>(a: &mut T, b: &mut T) {
1844/// unsafe {
1845/// // Create a bitwise copy of the value at `a` in `tmp`.
1846/// let tmp = ptr::read(a);
1847///
1848/// // Exiting at this point (either by explicitly returning or by
1849/// // calling a function which panics) would cause the value in `tmp` to
1850/// // be dropped while the same value is still referenced by `a`. This
1851/// // could trigger undefined behavior if `T` is not `Copy`.
1852///
1853/// // Create a bitwise copy of the value at `b` in `a`.
1854/// // This is safe because mutable references cannot alias.
1855/// ptr::copy_nonoverlapping(b, a, 1);
1856///
1857/// // As above, exiting here could trigger undefined behavior because
1858/// // the same value is referenced by `a` and `b`.
1859///
1860/// // Move `tmp` into `b`.
1861/// ptr::write(b, tmp);
1862///
1863/// // `tmp` has been moved (`write` takes ownership of its second argument),
1864/// // so nothing is dropped implicitly here.
1865/// }
1866/// }
1867///
1868/// let mut foo = "foo".to_owned();
1869/// let mut bar = "bar".to_owned();
1870///
1871/// swap(&mut foo, &mut bar);
1872///
1873/// assert_eq!(foo, "bar");
1874/// assert_eq!(bar, "foo");
1875/// ```
1876#[inline]
1877#[stable(feature = "rust1", since = "1.0.0")]
1878#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
1879#[rustc_diagnostic_item = "ptr_write"]
1880#[track_caller]
1881pub const unsafe fn write<T>(dst: *mut T, src: T) {
1882 // Semantically, it would be fine for this to be implemented as a
1883 // `copy_nonoverlapping` and appropriate drop suppression of `src`.
1884
1885 // However, implementing via that currently produces more MIR than is ideal.
1886 // Using an intrinsic keeps it down to just the simple `*dst = move src` in
1887 // MIR (11 statements shorter, at the time of writing), and also allows
1888 // `src` to stay an SSA value in codegen_ssa, rather than a memory one.
1889
1890 // SAFETY: the caller must guarantee that `dst` is valid for writes.
1891 // `dst` cannot overlap `src` because the caller has mutable access
1892 // to `dst` while `src` is owned by this function.
1893 unsafe {
1894 #[cfg(debug_assertions)] // Too expensive to always enable (for now?)
1895 ub_checks::assert_unsafe_precondition!(
1896 check_language_ub,
1897 "ptr::write requires that the pointer argument is aligned and non-null",
1898 (
1899 addr: *mut () = dst as *mut (),
1900 align: usize = align_of::<T>(),
1901 is_zst: bool = T::IS_ZST,
1902 ) => ub_checks::maybe_is_aligned_and_not_null(addr, align, is_zst)
1903 );
1904 intrinsics::write_via_move(dst, src)
1905 }
1906}
1907
1908/// Overwrites a memory location with the given value without reading or
1909/// dropping the old value.
1910///
1911/// Unlike [`write()`], the pointer may be unaligned.
1912///
1913/// `write_unaligned` does not drop the contents of `dst`. This is safe, but it
1914/// could leak allocations or resources, so care should be taken not to overwrite
1915/// an object that should be dropped.
1916///
1917/// Additionally, it does not drop `src`. Semantically, `src` is moved into the
1918/// location pointed to by `dst`.
1919///
1920/// This is appropriate for initializing uninitialized memory, or overwriting
1921/// memory that has previously been read with [`read_unaligned`].
1922///
1923/// # Safety
1924///
1925/// Behavior is undefined if any of the following conditions are violated:
1926///
1927/// * `dst` must be [valid] for writes.
1928///
1929/// [valid]: self#safety
1930///
1931/// ## On `packed` structs
1932///
1933/// Attempting to create a raw pointer to an `unaligned` struct field with
1934/// an expression such as `&packed.unaligned as *const FieldType` creates an
1935/// intermediate unaligned reference before converting that to a raw pointer.
1936/// That this reference is temporary and immediately cast is inconsequential
1937/// as the compiler always expects references to be properly aligned.
1938/// As a result, using `&packed.unaligned as *const FieldType` causes immediate
1939/// *undefined behavior* in your program.
1940///
1941/// Instead, you must use the `&raw mut` syntax to create the pointer.
1942/// You may use that constructed pointer together with this function.
1943///
1944/// An example of how to do it and how this relates to `write_unaligned` is:
1945///
1946/// ```
1947/// #[repr(packed, C)]
1948/// struct Packed {
1949/// _padding: u8,
1950/// unaligned: u32,
1951/// }
1952///
1953/// let mut packed: Packed = unsafe { std::mem::zeroed() };
1954///
1955/// // Take the address of a 32-bit integer which is not aligned.
1956/// // In contrast to `&packed.unaligned as *mut _`, this has no undefined behavior.
1957/// let unaligned = &raw mut packed.unaligned;
1958///
1959/// unsafe { std::ptr::write_unaligned(unaligned, 42) };
1960///
1961/// assert_eq!({packed.unaligned}, 42); // `{...}` forces copying the field instead of creating a reference.
1962/// ```
1963///
1964/// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however
1965/// (as can be seen in the `assert_eq!` above).
1966///
1967/// # Examples
1968///
1969/// Write a `usize` value to a byte buffer:
1970///
1971/// ```
1972/// fn write_usize(x: &mut [u8], val: usize) {
1973/// assert!(x.len() >= size_of::<usize>());
1974///
1975/// let ptr = x.as_mut_ptr() as *mut usize;
1976///
1977/// unsafe { ptr.write_unaligned(val) }
1978/// }
1979/// ```
1980#[inline]
1981#[stable(feature = "ptr_unaligned", since = "1.17.0")]
1982#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
1983#[rustc_diagnostic_item = "ptr_write_unaligned"]
1984#[track_caller]
1985pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
1986 // SAFETY: the caller must guarantee that `dst` is valid for writes.
1987 // `dst` cannot overlap `src` because the caller has mutable access
1988 // to `dst` while `src` is owned by this function.
1989 unsafe {
1990 copy_nonoverlapping((&raw const src) as *const u8, dst as *mut u8, size_of::<T>());
1991 // We are calling the intrinsic directly to avoid function calls in the generated code.
1992 intrinsics::forget(src);
1993 }
1994}
1995
1996/// Performs a volatile read of the value from `src` without moving it.
1997///
1998/// Volatile operations are intended to act on I/O memory. As such, they are considered externally
1999/// observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or
2000/// reordered by the compiler across other externally observable events. With this in mind, there
2001/// are two cases of usage that need to be distinguished:
2002///
2003/// - When a volatile operation is used for memory inside an [allocation], it behaves exactly like
2004/// [`read`], except for the additional guarantee that it won't be elided or reordered (see
2005/// above). This implies that the operation will actually access memory and not e.g. be lowered to
2006/// reusing data from a previous read. Other than that, all the usual rules for memory accesses
2007/// apply (including provenance). In particular, just like in C, whether an operation is volatile
2008/// has no bearing whatsoever on questions involving concurrent accesses from multiple threads.
2009/// Volatile accesses behave exactly like non-atomic accesses in that regard.
2010///
2011/// - Volatile operations, however, may also be used to access memory that is _outside_ of any Rust
2012/// allocation. In this use-case, the pointer does *not* have to be [valid] for reads. This is
2013/// typically used for CPU and peripheral registers that must be accessed via an I/O memory
2014/// mapping, most commonly at fixed addresses reserved by the hardware. These often have special
2015/// semantics associated to their manipulation, and cannot be used as general purpose memory.
2016/// Here, any address value is possible, including 0 and [`usize::MAX`], so long as the semantics
2017/// of such a read are well-defined by the target hardware. The provenance of the pointer is
2018/// irrelevant, and it can be created with [`without_provenance`]. The access must not trap. It
2019/// can cause side-effects, but those must not affect Rust-allocated memory in any way. This
2020/// access is still not considered [atomic], and as such it cannot be used for inter-thread
2021/// synchronization.
2022///
2023/// Note that volatile memory operations where T is a zero-sized type are noops and may be ignored.
2024///
2025/// [allocation]: crate::ptr#allocated-object
2026/// [atomic]: crate::sync::atomic#memory-model-for-atomic-accesses
2027///
2028/// # Safety
2029///
2030/// Like [`read`], `read_volatile` creates a bitwise copy of `T`, regardless of whether `T` is
2031/// [`Copy`]. If `T` is not [`Copy`], using both the returned value and the value at `*src` can
2032/// [violate memory safety][read-ownership]. However, storing non-[`Copy`] types in volatile memory
2033/// is almost certainly incorrect.
2034///
2035/// Behavior is undefined if any of the following conditions are violated:
2036///
2037/// * `src` must be either [valid] for reads, or it must point to memory outside of all Rust
2038/// allocations and reading from that memory must:
2039/// - not trap, and
2040/// - not cause any memory inside a Rust allocation to be modified.
2041///
2042/// * `src` must be properly aligned.
2043///
2044/// * Reading from `src` must produce a properly initialized value of type `T`.
2045///
2046/// Note that even if `T` has size `0`, the pointer must be properly aligned.
2047///
2048/// [valid]: self#safety
2049/// [read-ownership]: read#ownership-of-the-returned-value
2050///
2051/// # Examples
2052///
2053/// Basic usage:
2054///
2055/// ```
2056/// let x = 12;
2057/// let y = &x as *const i32;
2058///
2059/// unsafe {
2060/// assert_eq!(std::ptr::read_volatile(y), 12);
2061/// }
2062/// ```
2063#[inline]
2064#[stable(feature = "volatile", since = "1.9.0")]
2065#[track_caller]
2066#[rustc_diagnostic_item = "ptr_read_volatile"]
2067pub unsafe fn read_volatile<T>(src: *const T) -> T {
2068 // SAFETY: the caller must uphold the safety contract for `volatile_load`.
2069 unsafe {
2070 ub_checks::assert_unsafe_precondition!(
2071 check_language_ub,
2072 "ptr::read_volatile requires that the pointer argument is aligned",
2073 (
2074 addr: *const () = src as *const (),
2075 align: usize = align_of::<T>(),
2076 ) => ub_checks::maybe_is_aligned(addr, align)
2077 );
2078 intrinsics::volatile_load(src)
2079 }
2080}
2081
2082/// Performs a volatile write of a memory location with the given value without reading or dropping
2083/// the old value.
2084///
2085/// Volatile operations are intended to act on I/O memory. As such, they are considered externally
2086/// observable events (just like syscalls), and are guaranteed to not be elided or reordered by the
2087/// compiler across other externally observable events. With this in mind, there are two cases of
2088/// usage that need to be distinguished:
2089///
2090/// - When a volatile operation is used for memory inside an [allocation], it behaves exactly like
2091/// [`write`][write()], except for the additional guarantee that it won't be elided or reordered
2092/// (see above). This implies that the operation will actually access memory and not e.g. be
2093/// lowered to a register access. Other than that, all the usual rules for memory accesses apply
2094/// (including provenance). In particular, just like in C, whether an operation is volatile has no
2095/// bearing whatsoever on questions involving concurrent access from multiple threads. Volatile
2096/// accesses behave exactly like non-atomic accesses in that regard.
2097///
2098/// - Volatile operations, however, may also be used to access memory that is _outside_ of any Rust
2099/// allocation. In this use-case, the pointer does *not* have to be [valid] for writes. This is
2100/// typically used for CPU and peripheral registers that must be accessed via an I/O memory
2101/// mapping, most commonly at fixed addresses reserved by the hardware. These often have special
2102/// semantics associated to their manipulation, and cannot be used as general purpose memory.
2103/// Here, any address value is possible, including 0 and [`usize::MAX`], so long as the semantics
2104/// of such a write are well-defined by the target hardware. The provenance of the pointer is
2105/// irrelevant, and it can be created with [`without_provenance`]. The access must not trap. It
2106/// can cause side-effects, but those must not affect Rust-allocated memory in any way. This
2107/// access is still not considered [atomic], and as such it cannot be used for inter-thread
2108/// synchronization.
2109///
2110/// Note that volatile memory operations on zero-sized types (e.g., if a zero-sized type is passed
2111/// to `write_volatile`) are noops and may be ignored.
2112///
2113/// `write_volatile` does not drop the contents of `dst`. This is safe, but it could leak
2114/// allocations or resources, so care should be taken not to overwrite an object that should be
2115/// dropped when operating on Rust memory. Additionally, it does not drop `src`. Semantically, `src`
2116/// is moved into the location pointed to by `dst`.
2117///
2118/// [allocation]: crate::ptr#allocated-object
2119/// [atomic]: crate::sync::atomic#memory-model-for-atomic-accesses
2120///
2121/// # Safety
2122///
2123/// Behavior is undefined if any of the following conditions are violated:
2124///
2125/// * `dst` must be either [valid] for writes, or it must point to memory outside of all Rust
2126/// allocations and writing to that memory must:
2127/// - not trap, and
2128/// - not cause any memory inside a Rust allocation to be modified.
2129///
2130/// * `dst` must be properly aligned.
2131///
2132/// Note that even if `T` has size `0`, the pointer must be properly aligned.
2133///
2134/// [valid]: self#safety
2135///
2136/// # Examples
2137///
2138/// Basic usage:
2139///
2140/// ```
2141/// let mut x = 0;
2142/// let y = &mut x as *mut i32;
2143/// let z = 12;
2144///
2145/// unsafe {
2146/// std::ptr::write_volatile(y, z);
2147/// assert_eq!(std::ptr::read_volatile(y), 12);
2148/// }
2149/// ```
2150#[inline]
2151#[stable(feature = "volatile", since = "1.9.0")]
2152#[rustc_diagnostic_item = "ptr_write_volatile"]
2153#[track_caller]
2154pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
2155 // SAFETY: the caller must uphold the safety contract for `volatile_store`.
2156 unsafe {
2157 ub_checks::assert_unsafe_precondition!(
2158 check_language_ub,
2159 "ptr::write_volatile requires that the pointer argument is aligned",
2160 (
2161 addr: *mut () = dst as *mut (),
2162 align: usize = align_of::<T>(),
2163 ) => ub_checks::maybe_is_aligned(addr, align)
2164 );
2165 intrinsics::volatile_store(dst, src);
2166 }
2167}
2168
2169/// Align pointer `p`.
2170///
2171/// Calculate offset (in terms of elements of `size_of::<T>()` stride) that has to be applied
2172/// to pointer `p` so that pointer `p` would get aligned to `a`.
2173///
2174/// # Safety
2175/// `a` must be a power of two.
2176///
2177/// # Notes
2178/// This implementation has been carefully tailored to not panic. It is UB for this to panic.
2179/// The only real change that can be made here is change of `INV_TABLE_MOD_16` and associated
2180/// constants.
2181///
2182/// If we ever decide to make it possible to call the intrinsic with `a` that is not a
2183/// power-of-two, it will probably be more prudent to just change to a naive implementation rather
2184/// than trying to adapt this to accommodate that change.
2185///
2186/// Any questions go to @nagisa.
2187#[allow(ptr_to_integer_transmute_in_consts)]
2188pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
2189 // FIXME(#75598): Direct use of these intrinsics improves codegen significantly at opt-level <=
2190 // 1, where the method versions of these operations are not inlined.
2191 use intrinsics::{
2192 assume, cttz_nonzero, exact_div, mul_with_overflow, unchecked_rem, unchecked_shl,
2193 unchecked_shr, unchecked_sub, wrapping_add, wrapping_mul, wrapping_sub,
2194 };
2195
2196 /// Calculate multiplicative modular inverse of `x` modulo `m`.
2197 ///
2198 /// This implementation is tailored for `align_offset` and has following preconditions:
2199 ///
2200 /// * `m` is a power-of-two;
2201 /// * `x < m`; (if `x ≥ m`, pass in `x % m` instead)
2202 ///
2203 /// Implementation of this function shall not panic. Ever.
2204 #[inline]
2205 const unsafe fn mod_inv(x: usize, m: usize) -> usize {
2206 /// Multiplicative modular inverse table modulo 2⁴ = 16.
2207 ///
2208 /// Note, that this table does not contain values where inverse does not exist (i.e., for
2209 /// `0⁻¹ mod 16`, `2⁻¹ mod 16`, etc.)
2210 const INV_TABLE_MOD_16: [u8; 8] = [1, 11, 13, 7, 9, 3, 5, 15];
2211 /// Modulo for which the `INV_TABLE_MOD_16` is intended.
2212 const INV_TABLE_MOD: usize = 16;
2213
2214 // SAFETY: `m` is required to be a power-of-two, hence non-zero.
2215 let m_minus_one = unsafe { unchecked_sub(m, 1) };
2216 let mut inverse = INV_TABLE_MOD_16[(x & (INV_TABLE_MOD - 1)) >> 1] as usize;
2217 let mut mod_gate = INV_TABLE_MOD;
2218 // We iterate "up" using the following formula:
2219 //
2220 // $$ xy ≡ 1 (mod 2ⁿ) → xy (2 - xy) ≡ 1 (mod 2²ⁿ) $$
2221 //
2222 // This application needs to be applied at least until `2²ⁿ ≥ m`, at which point we can
2223 // finally reduce the computation to our desired `m` by taking `inverse mod m`.
2224 //
2225 // This computation is `O(log log m)`, which is to say, that on 64-bit machines this loop
2226 // will always finish in at most 4 iterations.
2227 loop {
2228 // y = y * (2 - xy) mod n
2229 //
2230 // Note, that we use wrapping operations here intentionally – the original formula
2231 // uses e.g., subtraction `mod n`. It is entirely fine to do them `mod
2232 // usize::MAX` instead, because we take the result `mod n` at the end
2233 // anyway.
2234 if mod_gate >= m {
2235 break;
2236 }
2237 inverse = wrapping_mul(inverse, wrapping_sub(2usize, wrapping_mul(x, inverse)));
2238 let (new_gate, overflow) = mul_with_overflow(mod_gate, mod_gate);
2239 if overflow {
2240 break;
2241 }
2242 mod_gate = new_gate;
2243 }
2244 inverse & m_minus_one
2245 }
2246
2247 let stride = size_of::<T>();
2248
2249 let addr: usize = p.addr();
2250
2251 // SAFETY: `a` is a power-of-two, therefore non-zero.
2252 let a_minus_one = unsafe { unchecked_sub(a, 1) };
2253
2254 if stride == 0 {
2255 // SPECIAL_CASE: handle 0-sized types. No matter how many times we step, the address will
2256 // stay the same, so no offset will be able to align the pointer unless it is already
2257 // aligned. This branch _will_ be optimized out as `stride` is known at compile-time.
2258 let p_mod_a = addr & a_minus_one;
2259 return if p_mod_a == 0 { 0 } else { usize::MAX };
2260 }
2261
2262 // SAFETY: `stride == 0` case has been handled by the special case above.
2263 let a_mod_stride = unsafe { unchecked_rem(a, stride) };
2264 if a_mod_stride == 0 {
2265 // SPECIAL_CASE: In cases where the `a` is divisible by `stride`, byte offset to align a
2266 // pointer can be computed more simply through `-p (mod a)`. In the off-chance the byte
2267 // offset is not a multiple of `stride`, the input pointer was misaligned and no pointer
2268 // offset will be able to produce a `p` aligned to the specified `a`.
2269 //
2270 // The naive `-p (mod a)` equation inhibits LLVM's ability to select instructions
2271 // like `lea`. We compute `(round_up_to_next_alignment(p, a) - p)` instead. This
2272 // redistributes operations around the load-bearing, but pessimizing `and` instruction
2273 // sufficiently for LLVM to be able to utilize the various optimizations it knows about.
2274 //
2275 // LLVM handles the branch here particularly nicely. If this branch needs to be evaluated
2276 // at runtime, it will produce a mask `if addr_mod_stride == 0 { 0 } else { usize::MAX }`
2277 // in a branch-free way and then bitwise-OR it with whatever result the `-p mod a`
2278 // computation produces.
2279
2280 let aligned_address = wrapping_add(addr, a_minus_one) & wrapping_sub(0, a);
2281 let byte_offset = wrapping_sub(aligned_address, addr);
2282 // FIXME: Remove the assume after <https://github.com/llvm/llvm-project/issues/62502>
2283 // SAFETY: Masking by `-a` can only affect the low bits, and thus cannot have reduced
2284 // the value by more than `a-1`, so even though the intermediate values might have
2285 // wrapped, the byte_offset is always in `[0, a)`.
2286 unsafe { assume(byte_offset < a) };
2287
2288 // SAFETY: `stride == 0` case has been handled by the special case above.
2289 let addr_mod_stride = unsafe { unchecked_rem(addr, stride) };
2290
2291 return if addr_mod_stride == 0 {
2292 // SAFETY: `stride` is non-zero. This is guaranteed to divide exactly as well, because
2293 // addr has been verified to be aligned to the original type’s alignment requirements.
2294 unsafe { exact_div(byte_offset, stride) }
2295 } else {
2296 usize::MAX
2297 };
2298 }
2299
2300 // GENERAL_CASE: From here on we’re handling the very general case where `addr` may be
2301 // misaligned, there isn’t an obvious relationship between `stride` and `a` that we can take an
2302 // advantage of, etc. This case produces machine code that isn’t particularly high quality,
2303 // compared to the special cases above. The code produced here is still within the realm of
2304 // miracles, given the situations this case has to deal with.
2305
2306 // SAFETY: a is power-of-two hence non-zero. stride == 0 case is handled above.
2307 // FIXME(const-hack) replace with min
2308 let gcdpow = unsafe {
2309 let x = cttz_nonzero(stride);
2310 let y = cttz_nonzero(a);
2311 if x < y { x } else { y }
2312 };
2313 // SAFETY: gcdpow has an upper-bound that’s at most the number of bits in a `usize`.
2314 let gcd = unsafe { unchecked_shl(1usize, gcdpow) };
2315 // SAFETY: gcd is always greater or equal to 1.
2316 if addr & unsafe { unchecked_sub(gcd, 1) } == 0 {
2317 // This branch solves for the following linear congruence equation:
2318 //
2319 // ` p + so = 0 mod a `
2320 //
2321 // `p` here is the pointer value, `s` - stride of `T`, `o` offset in `T`s, and `a` - the
2322 // requested alignment.
2323 //
2324 // With `g = gcd(a, s)`, and the above condition asserting that `p` is also divisible by
2325 // `g`, we can denote `a' = a/g`, `s' = s/g`, `p' = p/g`, then this becomes equivalent to:
2326 //
2327 // ` p' + s'o = 0 mod a' `
2328 // ` o = (a' - (p' mod a')) * (s'^-1 mod a') `
2329 //
2330 // The first term is "the relative alignment of `p` to `a`" (divided by the `g`), the
2331 // second term is "how does incrementing `p` by `s` bytes change the relative alignment of
2332 // `p`" (again divided by `g`). Division by `g` is necessary to make the inverse well
2333 // formed if `a` and `s` are not co-prime.
2334 //
2335 // Furthermore, the result produced by this solution is not "minimal", so it is necessary
2336 // to take the result `o mod lcm(s, a)`. This `lcm(s, a)` is the same as `a'`.
2337
2338 // SAFETY: `gcdpow` has an upper-bound not greater than the number of trailing 0-bits in
2339 // `a`.
2340 let a2 = unsafe { unchecked_shr(a, gcdpow) };
2341 // SAFETY: `a2` is non-zero. Shifting `a` by `gcdpow` cannot shift out any of the set bits
2342 // in `a` (of which it has exactly one).
2343 let a2minus1 = unsafe { unchecked_sub(a2, 1) };
2344 // SAFETY: `gcdpow` has an upper-bound not greater than the number of trailing 0-bits in
2345 // `a`.
2346 let s2 = unsafe { unchecked_shr(stride & a_minus_one, gcdpow) };
2347 // SAFETY: `gcdpow` has an upper-bound not greater than the number of trailing 0-bits in
2348 // `a`. Furthermore, the subtraction cannot overflow, because `a2 = a >> gcdpow` will
2349 // always be strictly greater than `(p % a) >> gcdpow`.
2350 let minusp2 = unsafe { unchecked_sub(a2, unchecked_shr(addr & a_minus_one, gcdpow)) };
2351 // SAFETY: `a2` is a power-of-two, as proven above. `s2` is strictly less than `a2`
2352 // because `(s % a) >> gcdpow` is strictly less than `a >> gcdpow`.
2353 return wrapping_mul(minusp2, unsafe { mod_inv(s2, a2) }) & a2minus1;
2354 }
2355
2356 // Cannot be aligned at all.
2357 usize::MAX
2358}
2359
2360/// Compares raw pointers for equality.
2361///
2362/// This is the same as using the `==` operator, but less generic:
2363/// the arguments have to be `*const T` raw pointers,
2364/// not anything that implements `PartialEq`.
2365///
2366/// This can be used to compare `&T` references (which coerce to `*const T` implicitly)
2367/// by their address rather than comparing the values they point to
2368/// (which is what the `PartialEq for &T` implementation does).
2369///
2370/// When comparing wide pointers, both the address and the metadata are tested for equality.
2371/// However, note that comparing trait object pointers (`*const dyn Trait`) is unreliable: pointers
2372/// to values of the same underlying type can compare inequal (because vtables are duplicated in
2373/// multiple codegen units), and pointers to values of *different* underlying type can compare equal
2374/// (since identical vtables can be deduplicated within a codegen unit).
2375///
2376/// # Examples
2377///
2378/// ```
2379/// use std::ptr;
2380///
2381/// let five = 5;
2382/// let other_five = 5;
2383/// let five_ref = &five;
2384/// let same_five_ref = &five;
2385/// let other_five_ref = &other_five;
2386///
2387/// assert!(five_ref == same_five_ref);
2388/// assert!(ptr::eq(five_ref, same_five_ref));
2389///
2390/// assert!(five_ref == other_five_ref);
2391/// assert!(!ptr::eq(five_ref, other_five_ref));
2392/// ```
2393///
2394/// Slices are also compared by their length (fat pointers):
2395///
2396/// ```
2397/// let a = [1, 2, 3];
2398/// assert!(std::ptr::eq(&a[..3], &a[..3]));
2399/// assert!(!std::ptr::eq(&a[..2], &a[..3]));
2400/// assert!(!std::ptr::eq(&a[0..2], &a[1..3]));
2401/// ```
2402#[stable(feature = "ptr_eq", since = "1.17.0")]
2403#[inline(always)]
2404#[must_use = "pointer comparison produces a value"]
2405#[rustc_diagnostic_item = "ptr_eq"]
2406#[allow(ambiguous_wide_pointer_comparisons)] // it's actually clear here
2407pub fn eq<T: PointeeSized>(a: *const T, b: *const T) -> bool {
2408 a == b
2409}
2410
2411/// Compares the *addresses* of the two pointers for equality,
2412/// ignoring any metadata in fat pointers.
2413///
2414/// If the arguments are thin pointers of the same type,
2415/// then this is the same as [`eq`].
2416///
2417/// # Examples
2418///
2419/// ```
2420/// use std::ptr;
2421///
2422/// let whole: &[i32; 3] = &[1, 2, 3];
2423/// let first: &i32 = &whole[0];
2424///
2425/// assert!(ptr::addr_eq(whole, first));
2426/// assert!(!ptr::eq::<dyn std::fmt::Debug>(whole, first));
2427/// ```
2428#[stable(feature = "ptr_addr_eq", since = "1.76.0")]
2429#[inline(always)]
2430#[must_use = "pointer comparison produces a value"]
2431pub fn addr_eq<T: PointeeSized, U: PointeeSized>(p: *const T, q: *const U) -> bool {
2432 (p as *const ()) == (q as *const ())
2433}
2434
2435/// Compares the *addresses* of the two function pointers for equality.
2436///
2437/// This is the same as `f == g`, but using this function makes clear that the potentially
2438/// surprising semantics of function pointer comparison are involved.
2439///
2440/// There are **very few guarantees** about how functions are compiled and they have no intrinsic
2441/// “identity”; in particular, this comparison:
2442///
2443/// * May return `true` unexpectedly, in cases where functions are equivalent.
2444///
2445/// For example, the following program is likely (but not guaranteed) to print `(true, true)`
2446/// when compiled with optimization:
2447///
2448/// ```
2449/// let f: fn(i32) -> i32 = |x| x;
2450/// let g: fn(i32) -> i32 = |x| x + 0; // different closure, different body
2451/// let h: fn(u32) -> u32 = |x| x + 0; // different signature too
2452/// dbg!(std::ptr::fn_addr_eq(f, g), std::ptr::fn_addr_eq(f, h)); // not guaranteed to be equal
2453/// ```
2454///
2455/// * May return `false` in any case.
2456///
2457/// This is particularly likely with generic functions but may happen with any function.
2458/// (From an implementation perspective, this is possible because functions may sometimes be
2459/// processed more than once by the compiler, resulting in duplicate machine code.)
2460///
2461/// Despite these false positives and false negatives, this comparison can still be useful.
2462/// Specifically, if
2463///
2464/// * `T` is the same type as `U`, `T` is a [subtype] of `U`, or `U` is a [subtype] of `T`, and
2465/// * `ptr::fn_addr_eq(f, g)` returns true,
2466///
2467/// then calling `f` and calling `g` will be equivalent.
2468///
2469///
2470/// # Examples
2471///
2472/// ```
2473/// use std::ptr;
2474///
2475/// fn a() { println!("a"); }
2476/// fn b() { println!("b"); }
2477/// assert!(!ptr::fn_addr_eq(a as fn(), b as fn()));
2478/// ```
2479///
2480/// [subtype]: https://doc.rust-lang.org/reference/subtyping.html
2481#[stable(feature = "ptr_fn_addr_eq", since = "1.85.0")]
2482#[inline(always)]
2483#[must_use = "function pointer comparison produces a value"]
2484pub fn fn_addr_eq<T: FnPtr, U: FnPtr>(f: T, g: U) -> bool {
2485 f.addr() == g.addr()
2486}
2487
2488/// Hash a raw pointer.
2489///
2490/// This can be used to hash a `&T` reference (which coerces to `*const T` implicitly)
2491/// by its address rather than the value it points to
2492/// (which is what the `Hash for &T` implementation does).
2493///
2494/// # Examples
2495///
2496/// ```
2497/// use std::hash::{DefaultHasher, Hash, Hasher};
2498/// use std::ptr;
2499///
2500/// let five = 5;
2501/// let five_ref = &five;
2502///
2503/// let mut hasher = DefaultHasher::new();
2504/// ptr::hash(five_ref, &mut hasher);
2505/// let actual = hasher.finish();
2506///
2507/// let mut hasher = DefaultHasher::new();
2508/// (five_ref as *const i32).hash(&mut hasher);
2509/// let expected = hasher.finish();
2510///
2511/// assert_eq!(actual, expected);
2512/// ```
2513#[stable(feature = "ptr_hash", since = "1.35.0")]
2514pub fn hash<T: PointeeSized, S: hash::Hasher>(hashee: *const T, into: &mut S) {
2515 use crate::hash::Hash;
2516 hashee.hash(into);
2517}
2518
2519#[stable(feature = "fnptr_impls", since = "1.4.0")]
2520impl<F: FnPtr> PartialEq for F {
2521 #[inline]
2522 fn eq(&self, other: &Self) -> bool {
2523 self.addr() == other.addr()
2524 }
2525}
2526#[stable(feature = "fnptr_impls", since = "1.4.0")]
2527impl<F: FnPtr> Eq for F {}
2528
2529#[stable(feature = "fnptr_impls", since = "1.4.0")]
2530impl<F: FnPtr> PartialOrd for F {
2531 #[inline]
2532 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
2533 self.addr().partial_cmp(&other.addr())
2534 }
2535}
2536#[stable(feature = "fnptr_impls", since = "1.4.0")]
2537impl<F: FnPtr> Ord for F {
2538 #[inline]
2539 fn cmp(&self, other: &Self) -> Ordering {
2540 self.addr().cmp(&other.addr())
2541 }
2542}
2543
2544#[stable(feature = "fnptr_impls", since = "1.4.0")]
2545impl<F: FnPtr> hash::Hash for F {
2546 fn hash<HH: hash::Hasher>(&self, state: &mut HH) {
2547 state.write_usize(self.addr() as _)
2548 }
2549}
2550
2551#[stable(feature = "fnptr_impls", since = "1.4.0")]
2552impl<F: FnPtr> fmt::Pointer for F {
2553 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2554 fmt::pointer_fmt_inner(self.addr() as _, f)
2555 }
2556}
2557
2558#[stable(feature = "fnptr_impls", since = "1.4.0")]
2559impl<F: FnPtr> fmt::Debug for F {
2560 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2561 fmt::pointer_fmt_inner(self.addr() as _, f)
2562 }
2563}
2564
2565/// Creates a `const` raw pointer to a place, without creating an intermediate reference.
2566///
2567/// `addr_of!(expr)` is equivalent to `&raw const expr`. The macro is *soft-deprecated*;
2568/// use `&raw const` instead.
2569///
2570/// It is still an open question under which conditions writing through an `addr_of!`-created
2571/// pointer is permitted. If the place `expr` evaluates to is based on a raw pointer, then the
2572/// result of `addr_of!` inherits all permissions from that raw pointer. However, if the place is
2573/// based on a reference, local variable, or `static`, then until all details are decided, the same
2574/// rules as for shared references apply: it is UB to write through a pointer created with this
2575/// operation, except for bytes located inside an `UnsafeCell`. Use `&raw mut` (or [`addr_of_mut`])
2576/// to create a raw pointer that definitely permits mutation.
2577///
2578/// Creating a reference with `&`/`&mut` is only allowed if the pointer is properly aligned
2579/// and points to initialized data. For cases where those requirements do not hold,
2580/// raw pointers should be used instead. However, `&expr as *const _` creates a reference
2581/// before casting it to a raw pointer, and that reference is subject to the same rules
2582/// as all other references. This macro can create a raw pointer *without* creating
2583/// a reference first.
2584///
2585/// See [`addr_of_mut`] for how to create a pointer to uninitialized data.
2586/// Doing that with `addr_of` would not make much sense since one could only
2587/// read the data, and that would be Undefined Behavior.
2588///
2589/// # Safety
2590///
2591/// The `expr` in `addr_of!(expr)` is evaluated as a place expression, but never loads from the
2592/// place or requires the place to be dereferenceable. This means that `addr_of!((*ptr).field)`
2593/// still requires the projection to `field` to be in-bounds, using the same rules as [`offset`].
2594/// However, `addr_of!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
2595///
2596/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside
2597/// `addr_of!` like everywhere else, in which case a reference is created to call `Deref::deref` or
2598/// `Index::index`, respectively. The statements above only apply when no such coercions are
2599/// applied.
2600///
2601/// [`offset`]: pointer::offset
2602///
2603/// # Example
2604///
2605/// **Correct usage: Creating a pointer to unaligned data**
2606///
2607/// ```
2608/// use std::ptr;
2609///
2610/// #[repr(packed)]
2611/// struct Packed {
2612/// f1: u8,
2613/// f2: u16,
2614/// }
2615///
2616/// let packed = Packed { f1: 1, f2: 2 };
2617/// // `&packed.f2` would create an unaligned reference, and thus be Undefined Behavior!
2618/// let raw_f2 = ptr::addr_of!(packed.f2);
2619/// assert_eq!(unsafe { raw_f2.read_unaligned() }, 2);
2620/// ```
2621///
2622/// **Incorrect usage: Out-of-bounds fields projection**
2623///
2624/// ```rust,no_run
2625/// use std::ptr;
2626///
2627/// #[repr(C)]
2628/// struct MyStruct {
2629/// field1: i32,
2630/// field2: i32,
2631/// }
2632///
2633/// let ptr: *const MyStruct = ptr::null();
2634/// let fieldptr = unsafe { ptr::addr_of!((*ptr).field2) }; // Undefined Behavior ⚠️
2635/// ```
2636///
2637/// The field projection `.field2` would offset the pointer by 4 bytes,
2638/// but the pointer is not in-bounds of an allocation for 4 bytes,
2639/// so this offset is Undefined Behavior.
2640/// See the [`offset`] docs for a full list of requirements for inbounds pointer arithmetic; the
2641/// same requirements apply to field projections, even inside `addr_of!`. (In particular, it makes
2642/// no difference whether the pointer is null or dangling.)
2643#[stable(feature = "raw_ref_macros", since = "1.51.0")]
2644#[rustc_macro_transparency = "semitransparent"]
2645pub macro addr_of($place:expr) {
2646 &raw const $place
2647}
2648
2649/// Creates a `mut` raw pointer to a place, without creating an intermediate reference.
2650///
2651/// `addr_of_mut!(expr)` is equivalent to `&raw mut expr`. The macro is *soft-deprecated*;
2652/// use `&raw mut` instead.
2653///
2654/// Creating a reference with `&`/`&mut` is only allowed if the pointer is properly aligned
2655/// and points to initialized data. For cases where those requirements do not hold,
2656/// raw pointers should be used instead. However, `&mut expr as *mut _` creates a reference
2657/// before casting it to a raw pointer, and that reference is subject to the same rules
2658/// as all other references. This macro can create a raw pointer *without* creating
2659/// a reference first.
2660///
2661/// # Safety
2662///
2663/// The `expr` in `addr_of_mut!(expr)` is evaluated as a place expression, but never loads from the
2664/// place or requires the place to be dereferenceable. This means that `addr_of_mut!((*ptr).field)`
2665/// still requires the projection to `field` to be in-bounds, using the same rules as [`offset`].
2666/// However, `addr_of_mut!(*ptr)` is defined behavior even if `ptr` is null, dangling, or misaligned.
2667///
2668/// Note that `Deref`/`Index` coercions (and their mutable counterparts) are applied inside
2669/// `addr_of_mut!` like everywhere else, in which case a reference is created to call `Deref::deref`
2670/// or `Index::index`, respectively. The statements above only apply when no such coercions are
2671/// applied.
2672///
2673/// [`offset`]: pointer::offset
2674///
2675/// # Examples
2676///
2677/// **Correct usage: Creating a pointer to unaligned data**
2678///
2679/// ```
2680/// use std::ptr;
2681///
2682/// #[repr(packed)]
2683/// struct Packed {
2684/// f1: u8,
2685/// f2: u16,
2686/// }
2687///
2688/// let mut packed = Packed { f1: 1, f2: 2 };
2689/// // `&mut packed.f2` would create an unaligned reference, and thus be Undefined Behavior!
2690/// let raw_f2 = ptr::addr_of_mut!(packed.f2);
2691/// unsafe { raw_f2.write_unaligned(42); }
2692/// assert_eq!({packed.f2}, 42); // `{...}` forces copying the field instead of creating a reference.
2693/// ```
2694///
2695/// **Correct usage: Creating a pointer to uninitialized data**
2696///
2697/// ```rust
2698/// use std::{ptr, mem::MaybeUninit};
2699///
2700/// struct Demo {
2701/// field: bool,
2702/// }
2703///
2704/// let mut uninit = MaybeUninit::<Demo>::uninit();
2705/// // `&uninit.as_mut().field` would create a reference to an uninitialized `bool`,
2706/// // and thus be Undefined Behavior!
2707/// let f1_ptr = unsafe { ptr::addr_of_mut!((*uninit.as_mut_ptr()).field) };
2708/// unsafe { f1_ptr.write(true); }
2709/// let init = unsafe { uninit.assume_init() };
2710/// ```
2711///
2712/// **Incorrect usage: Out-of-bounds fields projection**
2713///
2714/// ```rust,no_run
2715/// use std::ptr;
2716///
2717/// #[repr(C)]
2718/// struct MyStruct {
2719/// field1: i32,
2720/// field2: i32,
2721/// }
2722///
2723/// let ptr: *mut MyStruct = ptr::null_mut();
2724/// let fieldptr = unsafe { ptr::addr_of_mut!((*ptr).field2) }; // Undefined Behavior ⚠️
2725/// ```
2726///
2727/// The field projection `.field2` would offset the pointer by 4 bytes,
2728/// but the pointer is not in-bounds of an allocation for 4 bytes,
2729/// so this offset is Undefined Behavior.
2730/// See the [`offset`] docs for a full list of requirements for inbounds pointer arithmetic; the
2731/// same requirements apply to field projections, even inside `addr_of_mut!`. (In particular, it
2732/// makes no difference whether the pointer is null or dangling.)
2733#[stable(feature = "raw_ref_macros", since = "1.51.0")]
2734#[rustc_macro_transparency = "semitransparent"]
2735pub macro addr_of_mut($place:expr) {
2736 &raw mut $place
2737}