Trait bound for ZST (zero sized types)

Do we have any means to detect ZST at the current moment or in future(RFC)?

It is interesting topic once trait specialization will be in place, but as of now I do not see any easy way to do it. We cannot really trait bound on something like mem::size_of::<T> == 0 I think?

Here's a way with const generics. (Might be possible with just one trait, I didn't play with it too long.)

#![feature(const_generics)]
#![feature(const_evaluatable_checked)]

pub enum HasSize<const SIZE: usize> {}
pub trait IsZeroSized {}
impl IsZeroSized for HasSize<0> {}

pub trait ZeroSized {}
impl<T: Sized> ZeroSized for T where HasSize<{ std::mem::size_of::<T>() }>: IsZeroSized {}

fn check<T: ZeroSized>(_: T) {
    println!("Emptiness is loneliness")
}

fn main() {
    check(());
    // mismatched types: expected `4_usize`, found `0_usize`
    // check(1);
}

And a less comprehensible way that only uses bounds, no helper traits or types.

#![feature(const_generics)]
#![feature(const_evaluatable_checked)]

fn check<T>(_: T)
where
    [(); 1 / ((std::mem::size_of::<T>() == 0) as usize)]: ,
{
    println!("Emptiness is loneliness")
}

fn main() {
    check(());
    // evaluation of `check::<i32>::{constant#0}` failed:
    //    attempt to divide `1_usize` by zero
    //check(1);
}

Thanks.

I'll subscribe to tracking issue for const_evaluatable_checked, hopefully it will work with specialization

:grinning_face_with_smiling_eyes:

but is it? Let's not mix the inhabited ZSTs, which are lonely, with the uninhabited ones, which are empty :upside_down_face:

It does work with specialization Rust Playground

Yeah yeah I know.
But it is still nightly feature and I'm not going to use it until stabilization, which may change things

const_evaluatable_unchecked is also unstable.