ZST const assertion

⚓ Rust    📅 2025-10-21    👤 surdeus    👁️ 3      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: ZST const assertion

I'm currently trying to implement in a struct a const assertion to check that the struct is not a ZST.
My current implementation is:

struct Foo;
impl Foo {
    pub const _VALIDITY: () = assert!(size_of::<Self>() != 0, "Foo cannot be a ZST");
}

but this doesn't actually throw a compile-time error. I noticed that the code throws an error only if I use this constant in a function, just like this:

struct Foo;
impl Foo {
    pub const _VALIDITY: () = assert!(size_of::<Self>() != 0, "Foo cannot be a ZST");
    fn _bar() {
        Self::_VALIDITY;
    }
}

so, I wanted to ask

  1. Is this the correct way to implement this check?
  2. If so, is the function that uses this constant necessary, or am I doing something wrong?

3 posts - 3 participants

Read full topic

🏷️ Rust_feed