Skip to content

Commit 490c0d6

Browse files
authored
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.
2 parents e9e2ebb + 65d7478 commit 490c0d6

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

compiler/rustc_abi/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -742,11 +742,21 @@ impl Align {
742742
1 << self.pow2
743743
}
744744

745+
#[inline]
746+
pub fn bytes_usize(self) -> usize {
747+
self.bytes().try_into().unwrap()
748+
}
749+
745750
#[inline]
746751
pub fn bits(self) -> u64 {
747752
self.bytes() * 8
748753
}
749754

755+
#[inline]
756+
pub fn bits_usize(self) -> usize {
757+
self.bits().try_into().unwrap()
758+
}
759+
750760
/// Computes the best alignment possible for the given offset
751761
/// (the largest power of two that the offset is a multiple of).
752762
///

compiler/rustc_transmute/src/layout/tree.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ pub(crate) mod rustc {
266266

267267
ty::Ref(lifetime, ty, mutability) => {
268268
let ty_and_layout = cx.layout_of(*ty)?;
269-
let align = ty_and_layout.align.abi.bytes() as usize;
269+
let align = ty_and_layout.align.abi.bytes_usize();
270270
let size = ty_and_layout.size.bytes_usize();
271271
Ok(Tree::Ref(Ref {
272272
lifetime: *lifetime,

0 commit comments

Comments
 (0)