Skip to content

Commit

Permalink
Rollup merge of rust-lang#124579 - RalfJung:align-bytes-usize, r=fmease
Browse files Browse the repository at this point in the history
Align: add bytes_usize and bits_usize

This matches `Size::bytes/bits_usize`. I recently wanted this in Miri as well.
  • Loading branch information
matthiaskrgr authored May 2, 2024
2 parents e9e2ebb + 65d7478 commit 490c0d6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,21 @@ impl Align {
1 << self.pow2
}

#[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).
///
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_transmute/src/layout/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 490c0d6

Please sign in to comment.