Skip to content

Alignment at const time? #370

Open
Open
@joshlf

Description

@joshlf

I have the following function:

fn aligned_to(bytes: &[u8], align: usize) -> bool {
    (bytes as *const _ as *const () as usize) % align == 0
}

I'd like to make it a const fn, but if I just do that without changing the implementation, the compiler complains:

error: pointers cannot be cast to integers during const eval
    --> src/lib.rs:1636:5
     |
1636 |     (bytes as *const _ as *const () as usize) % align == 0
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: at compile-time, pointers do not have an integer value
     = note: avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior

This thread implies that there's no way to get this to work today (even by using unstable features). Is there intended to be a concept of alignment and const time? Will code like this ever be possible to write? This is relevant to zerocopy since I'd like to be able to do reference casts at const time where the target type has an alignment requirement greater than 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions