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 DstLayout::pad_to_align #638

Merged
merged 1 commit into from
Nov 26, 2023
Merged

Add DstLayout::pad_to_align #638

merged 1 commit into from
Nov 26, 2023

Conversation

jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Nov 24, 2023

DstLayout::pad_to_align is comparable to alloc::Layout::pad_to_align, except that it additionally handles DSTs. This is the second of two steps needed for DstLayout to support an API in the form of:

// For illustrative purposes; `align` and `packed`
// can't actually be combined like this.
#[repr(C, align(2), packed(4))]
struct ReprCStruct<T, U, V> {
    foo: Foo<T>,
    bar: Bar<U>,
    baz: Baz<V>,
}

impl<T, U, V> KnownLayout for ReprCStruct<T, U, V> {
    const LAYOUT: DstLayout = {
        use ::zerocopy::macro_utils::core_reexport::num::NonZeroUsize;
        use ::zerocopy::DstLayout;

        let min_align = NonZeroUsize::new(2); // or `None`, w/o `align`
        let max_align = NonZeroUsize::new(4); // or `None`, w/o `packed`

        DstLayout::new(min_align)
            .extend(<Foo<T> as KnownLayout>::LAYOUT, max_align)
            .extend(<Bar<T> as KnownLayout>::LAYOUT, max_align)
            .extend(<Baz<T> as KnownLayout>::LAYOUT, max_align)
            .pad_to_align()
    };
}

(The first step was implementing DstLayout::extend in #633).

Despite the similarities between DstLayout::pad_to_align and Layout::pad_to_align, the former cannot be implemented in terms of the latter because Layout::pad_to_align is not const. This introduces a risk that our computations here diverge from those in Layout::pad_to_align, but this PR also includes Kani proofs that DstLayout::pad_to_align behaves comparably to Layout::pad_to_align.

Makes progress towards #29.

Copy link
Member

@joshlf joshlf left a comment

Choose a reason for hiding this comment

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

Two tiny nits, but otherwise looks great!

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
This method is comparable to `Layout::pad_to_align`, but also
handles dynamically sized types.

Makes progress towards #29.
@jswrenn jswrenn added this pull request to the merge queue Nov 26, 2023
Merged via the queue into main with commit fe7a54b Nov 26, 2023
126 checks passed
@jswrenn jswrenn deleted the layout-pad_to_align branch November 26, 2023 03:59
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