Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align: add bytes_usize and bits_usize #124579

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

@fmease fmease May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about adding a yet unused public method but if you plan on using it soon(tm) in miri, then I'd say it's fine.

Copy link
Member Author

@RalfJung RalfJung May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I don't plan to use bits_usize (only bytes_usize), but it exists for Size and I think having a somewhat symmetric API surface here is valuable. It's not always good to remove a function just because it is not used right now, in a large codebase like ours that constantly changes.

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
Loading