Skip to content

Commit f4adb1e

Browse files
committed
add notes on how to store 'ptr or int'
1 parent d3299af commit f4adb1e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

library/core/src/intrinsics.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ extern "rust-intrinsic" {
11861186
///
11871187
/// Transmuting *pointers to integers* in a `const` context is [undefined behavior][ub], unless
11881188
/// the pointer was originally created *from* an integer. (That includes this function
1189-
/// specifically, integer-to-pointer casts, and helpers like [`invalid`][crate::ptr::dangling],
1189+
/// specifically, integer-to-pointer casts, and helpers like [`dangling`][crate::ptr::dangling],
11901190
/// but also semantically-equivalent conversions such as punning through `repr(C)` union
11911191
/// fields.) Any attempt to use the resulting value for integer operations will abort
11921192
/// const-evaluation. (And even outside `const`, such transmutation is touching on many
@@ -1206,7 +1206,11 @@ extern "rust-intrinsic" {
12061206
/// In particular, doing a pointer-to-integer-to-pointer roundtrip via `transmute` is *not* a
12071207
/// lossless process. If you want to round-trip a pointer through an integer in a way that you
12081208
/// can get back the original pointer, you need to use `as` casts, or replace the integer type
1209-
/// by `MaybeUninit<$int>` (and never call `assume_init()`).
1209+
/// by `MaybeUninit<$int>` (and never call `assume_init()`). If you are looking for a way to
1210+
/// store data of arbitrary type, also use `MaybeUninit<T>` (that will also handle uninitialized
1211+
/// memory due to padding). If you specifically need to store something that is "either an
1212+
/// integer or a pointer", use `*mut ()`: integers can be converted to pointers and back without
1213+
/// any loss (via `as` casts or via `transmute`).
12101214
///
12111215
/// # Examples
12121216
///

0 commit comments

Comments
 (0)