about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-05-02 19:42:48 +0200
committerGitHub <noreply@github.com>2024-05-02 19:42:48 +0200
commit490c0d60f820145d432bfd0100c5cfb2d95a38a6 (patch)
tree99262524b3e7ca1b50ca8acdae9f014e1c4bc34a
parente9e2ebb051b5b108973b02f80cb5531a213a865b (diff)
parent65d74785d7b43de0bae1e7687efbddbb4b170f91 (diff)
Rollup merge of #124579 - RalfJung:align-bytes-usize, r=fmease
Align: add bytes_usize and bits_usize

This matches `Size::bytes/bits_usize`. I recently wanted this in Miri as well.
-rw-r--r--compiler/rustc_abi/src/lib.rs10
-rw-r--r--compiler/rustc_transmute/src/layout/tree.rs2
2 files changed, 11 insertions, 1 deletions
diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs
index a46148242d5..6a2943da4a3 100644
--- a/compiler/rustc_abi/src/lib.rs
+++ b/compiler/rustc_abi/src/lib.rs
@@ -743,10 +743,20 @@ impl Align {
     }
 
     #[inline]
+    pub fn bytes_usize(self) -> usize {
+        self.bytes().try_into().unwrap()
+    }
+
+    #[inline]
     pub fn bits(self) -> u64 {
         self.bytes() * 8
     }
 
+    #[inline]
+    pub fn bits_usize(self) -> usize {
+        self.bits().try_into().unwrap()
+    }
+
     /// Computes the best alignment possible for the given offset
     /// (the largest power of two that the offset is a multiple of).
     ///
diff --git a/compiler/rustc_transmute/src/layout/tree.rs b/compiler/rustc_transmute/src/layout/tree.rs
index edd3227210b..604b68d2cd4 100644
--- a/compiler/rustc_transmute/src/layout/tree.rs
+++ b/compiler/rustc_transmute/src/layout/tree.rs
@@ -266,7 +266,7 @@ pub(crate) mod rustc {
 
                 ty::Ref(lifetime, ty, mutability) => {
                     let ty_and_layout = cx.layout_of(*ty)?;
-                    let align = ty_and_layout.align.abi.bytes() as usize;
+                    let align = ty_and_layout.align.abi.bytes_usize();
                     let size = ty_and_layout.size.bytes_usize();
                     Ok(Tree::Ref(Ref {
                         lifetime: *lifetime,