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

Improve usage of repr(packed) #68

Merged
merged 5 commits into from Aug 26, 2020
Merged

Improve usage of repr(packed) #68

merged 5 commits into from Aug 26, 2020

Conversation

ghost
Copy link

@ghost ghost commented Aug 24, 2020

Close #38.
We occasionally userepr(packed) when the struct is unaligned. When we use it, I added a comment explaining why. In addition, we derive Copy and Clone to allow the compiler to copy the fields to locals.

@ghost ghost requested a review from robert-w-gries August 24, 2020 23:02
Copy link
Contributor

@robert-w-gries robert-w-gries left a comment

Choose a reason for hiding this comment

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

Looks good to me. Will hold off on merging for a day in case @IsaacWoods wants to review, as I've been less involved in the project lately.

@VictorKoenders
Copy link

VictorKoenders commented Aug 26, 2020

Maybe outside of the scope of this PR, but should there be compile-time validations or tests to make sure the structs are the right size?

In my project i have a couple of

fn _test_size() {
    unsafe {
        core::mem::transmute::<[u8; 56], Hpet>([0u8; 56]);
    }
}

Which give a compile-time check to validate the Hpet struct is still 56 bytes. Of course this can also be done in a #[test].

Enforcing this would make sure that changes by humans or compilers will get noticed instead of loading wrongly-sized structs from memory pointers.

@ghost
Copy link
Author

ghost commented Aug 26, 2020

@VictorKoenders I completely agree. I will add those tests to this PR.

@ghost ghost requested a review from IsaacWoods August 26, 2020 15:09
Copy link
Contributor

@robert-w-gries robert-w-gries left a comment

Choose a reason for hiding this comment

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

Thanks for adding the tests

@robert-w-gries robert-w-gries merged commit 30d64f4 into rust-osdev:master Aug 26, 2020
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.

Warning safe_packed_borrows will become a hard error
2 participants