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

Automatically implement Unaligned without custom derive #110

Open
joshlf opened this issue Oct 30, 2022 · 0 comments
Open

Automatically implement Unaligned without custom derive #110

joshlf opened this issue Oct 30, 2022 · 0 comments
Labels
compatibility-breaking Changes that are (likely to be) breaking

Comments

@joshlf
Copy link
Member

joshlf commented Oct 30, 2022

Once the associated_const_equality feature is stabilized, we ought to be able to automatically implement Unaligned without needing a custom derive:

#![feature(associated_const_equality)]

unsafe trait Align {
    const ALIGN: usize;
}

unsafe impl<T> Align for T {
    const ALIGN: usize = core::mem::align_of::<T>();
}

unsafe trait Unaligned {}

unsafe impl<T: Align<ALIGN = 1>> Unaligned for T {}

Note that we may not want to do this - it would mean that types would no longer need to opt-in to implementing Unaligned as they do today. This probably isn't a huge deal since you can't do anything with Unaligned on its own (FromBytes and AsBytes are the traits that really unlock the ability to muck with a type's internal state), but at a minimum it would make the API inconsistent. One option would be to just make Align<ALIGN = 1> a bound so that Unaligned could become a safe trait that just represents the fact of opting-in:

trait Unaligned: Align<ALIGN = 1> {}
@joshlf joshlf added the compatibility-breaking Changes that are (likely to be) breaking label Aug 12, 2023
@joshlf joshlf mentioned this issue Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility-breaking Changes that are (likely to be) breaking
Projects
None yet
Development

No branches or pull requests

1 participant