about summary refs log tree commit diff
path: root/library/core/src/ptr/alignment.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-09-18 07:05:14 +0000
committerbors <bors@rust-lang.org>2024-09-18 07:05:14 +0000
commitf68c28b6cefb9e1f9c258f20a3b1b7b7cddbc84f (patch)
tree3248b65a7ab241925efc55ae3172c0a5fef70193 /library/core/src/ptr/alignment.rs
parentf6bcd094abe174a218f7cf406e75521be4199f88 (diff)
parent18ca8bf8ee77deb13967620b1f4829f22c16dae1 (diff)
downloadrust-f68c28b6cefb9e1f9c258f20a3b1b7b7cddbc84f.tar.gz
rust-f68c28b6cefb9e1f9c258f20a3b1b7b7cddbc84f.zip
Auto merge of #129845 - scottmcm:redo-layout, r=Noratrieb
Take more advantage of the `isize::MAX` limit in `Layout`

Things like `padding_needed_for` are current implemented being super careful to handle things like `Layout::size` potentially being `usize::MAX`.

But now that #95295 has happened, that's no longer a concern.  It's possible to add two `Layout::size`s together without risking overflow now.

So take advantage of that to remove a bunch of checked math that's not actually needed.  For example, the round-up-and-add-next-size in `extend` doesn't need any overflow checks at all, just the final check for compatibility with the alignment.

(And while I was doing that I made it all unstably const, because there's nothing in `Layout` that's fundamentally runtime-only.)
Diffstat (limited to 'library/core/src/ptr/alignment.rs')
-rw-r--r--library/core/src/ptr/alignment.rs5
1 files changed, 5 insertions, 0 deletions
diff --git a/library/core/src/ptr/alignment.rs b/library/core/src/ptr/alignment.rs
index 834cec9dcfc..ceb5906d226 100644
--- a/library/core/src/ptr/alignment.rs
+++ b/library/core/src/ptr/alignment.rs
@@ -154,6 +154,11 @@ impl Alignment {
         // SAFETY: The alignment is always nonzero, and therefore decrementing won't overflow.
         !(unsafe { self.as_usize().unchecked_sub(1) })
     }
+
+    // Remove me once `Ord::max` is usable in const
+    pub(crate) const fn max(a: Self, b: Self) -> Self {
+        if a.as_usize() > b.as_usize() { a } else { b }
+    }
 }
 
 #[unstable(feature = "ptr_alignment_type", issue = "102070")]