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

add align_of! for computing alignment of DSTs #576

Merged
merged 1 commit into from
Oct 31, 2023
Merged

add align_of! for computing alignment of DSTs #576

merged 1 commit into from
Oct 31, 2023

Conversation

jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Oct 31, 2023

The align_of! macro uses trailing_field_offset! to compute the alignment of DSTs with minimum alignments up to 65536. By contrast, core::mem::align_of does not support DSTs.

We can use this for supplying the alignment argument required by DstLayout::for_dst (introduced by #541).

@jswrenn jswrenn requested a review from joshlf October 31, 2023 17:06
@jswrenn
Copy link
Collaborator Author

jswrenn commented Oct 31, 2023

This probably could be implemented directly in terms of align_of_val_raw, but writing it in terms of trailing_field_offset! means we:

  • only need to wait for size_of_val_raw to be stabilized
  • don't need to relitigate the safety preconditions of using {size,align}_of_val_raw

#[macro_export]
macro_rules! align_of {
($t:ty) => {{
#[repr(C)]
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a safety comment here that proves that this macro computes the correct value?

@@ -205,6 +205,26 @@ macro_rules! trailing_field_offset {
}};
}

/// Computes alignment of `$ty`.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe also note that $ty doesn't need to be Sized?

The `align_of!` macro uses `trailing_field_offset!` to compute the
alignment of DSTs with minimum alignments up to 65536. By contrast,
`core::mem::align_of` does not support DSTs.

Makes progress on #29.
@jswrenn jswrenn added this pull request to the merge queue Oct 31, 2023
Merged via the queue into main with commit c35dc45 Oct 31, 2023
126 checks passed
@jswrenn jswrenn deleted the align_of branch October 31, 2023 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants