Skip to content

Feature Request static asserts #2790

@elichai

Description

@elichai

I think asserts are a pretty common thing, especially in unsafe rust (checking sizes/alignments etc.)

Now with anonymous consts we can already do things like:

use std::mem::*;
#[macro_export]
macro_rules! static_assert {
    ($condition:expr) => {
        const _: &() = &[()][1 - ($condition) as usize];
    }
}

static_assert!(size_of::<usize>() == 8);
static_assert!(size_of::<*const u8>() == 8);
static_assert!(align_of::<*const u8>() >= align_of::<u128>());
static_assert!(5>3);

I propose to add macros like this to the core library, with the whole assert/assert_eq/assert_ne facade.

I think these would be really useful

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-assertionsProposals relating to assertions.A-const-evalProposals relating to compile time evaluation (CTFE).T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions