Skip to content

Commit 3a9dd82

Browse files
authored
Rollup merge of #128306 - WiktorPrzetacznik:WiktorPrzetacznik-nonnull-alignoffset-update, r=Amanieu
Update NonNull::align_offset quarantees This PR proposes to update [`NonNull::align_offset`](https://doc.rust-lang.org/stable/std/ptr/struct.NonNull.html#method.align_offset) guarantees, which should to be matched with [`ptr::align_offset`](https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.align_offset-1) (as `NonNull::align_offset` delegates to `ptr::align_offset`). [PR #121201](#121201) updated only `ptr::align_offset` docs.
2 parents d3a3939 + 3d7aa16 commit 3a9dd82

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Diff for: library/core/src/ptr/non_null.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1169,16 +1169,23 @@ impl<T: ?Sized> NonNull<T> {
11691169
/// `align`.
11701170
///
11711171
/// If it is not possible to align the pointer, the implementation returns
1172-
/// `usize::MAX`. It is permissible for the implementation to *always*
1173-
/// return `usize::MAX`. Only your algorithm's performance can depend
1174-
/// on getting a usable offset here, not its correctness.
1172+
/// `usize::MAX`.
11751173
///
11761174
/// The offset is expressed in number of `T` elements, and not bytes.
11771175
///
11781176
/// There are no guarantees whatsoever that offsetting the pointer will not overflow or go
11791177
/// beyond the allocation that the pointer points into. It is up to the caller to ensure that
11801178
/// the returned offset is correct in all terms other than alignment.
11811179
///
1180+
/// When this is called during compile-time evaluation (which is unstable), the implementation
1181+
/// may return `usize::MAX` in cases where that can never happen at runtime. This is because the
1182+
/// actual alignment of pointers is not known yet during compile-time, so an offset with
1183+
/// guaranteed alignment can sometimes not be computed. For example, a buffer declared as `[u8;
1184+
/// N]` might be allocated at an odd or an even address, but at compile-time this is not yet
1185+
/// known, so the execution has to be correct for either choice. It is therefore impossible to
1186+
/// find an offset that is guaranteed to be 2-aligned. (This behavior is subject to change, as usual
1187+
/// for unstable APIs.)
1188+
///
11821189
/// # Panics
11831190
///
11841191
/// The function panics if `align` is not a power-of-two.

0 commit comments

Comments
 (0)