Skip to content

Automatically implement Unaligned without custom derive #110

@joshlf

Description

@joshlf

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> {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions