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 traits for size and alignment comparisons #125

Closed
joshlf opened this issue Nov 6, 2022 · 1 comment
Closed

Add traits for size and alignment comparisons #125

joshlf opened this issue Nov 6, 2022 · 1 comment
Labels
compatibility-breaking Changes that are (likely to be) breaking

Comments

@joshlf
Copy link
Member

joshlf commented Nov 6, 2022

This is a half-baked idea, and it requires the unstable associated_const_equality feature. Also, the AlignedTo impls don't currently work thanks to rust-lang/rust#103292.

trait Size {
    const SIZE: usize;
}

impl<T> Size for T {
    const SIZE: usize = core::mem::size_of::<T>();
}

trait Align {
    const ALIGN: usize;
}

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

trait Zst {}

impl<T: Size<SIZE = 0>> Zst for T {}

trait SameSizeAs<T> {}

impl<const SIZE: usize, T: Size<SIZE = { SIZE }>, U: Size<SIZE = { SIZE }>> SameSizeAs<U> for T {}

trait AlignedTo<T> {}

impl<T: Align<ALIGN = 1>, U: Align<ALIGN = 1>> AlignedTo<U> for T {}
impl<T: Align<ALIGN = 2>, U: Align<ALIGN = 2>> AlignedTo<U> for T {}
impl<T: Align<ALIGN = 2>, U: Align<ALIGN = 1>> AlignedTo<U> for T {}
impl<T: Align<ALIGN = 4>, U: Align<ALIGN = 4>> AlignedTo<U> for T {}
impl<T: Align<ALIGN = 4>, U: Align<ALIGN = 2>> AlignedTo<U> for T {}
impl<T: Align<ALIGN = 4>, U: Align<ALIGN = 1>> AlignedTo<U> for T {}
@joshlf joshlf added the compatibility-breaking Changes that are (likely to be) breaking label Aug 12, 2023
@joshlf
Copy link
Member Author

joshlf commented May 19, 2024

Merging into #1316.

@joshlf joshlf closed this as completed May 19, 2024
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