I'm using the const generic trick to test if a type is zero-sized:
#[track_caller]
const fn check_zero_sized<T>() {
if std::mem::size_of::<T>() != 0 {
panic!("type is not zero-sized");
}
}
trait ZeroSized {
const _CHECK: ();
}
impl<T> ZeroSized for T {
// Can we add #[track_caller] here?
const _CHECK: () = check_zero_sized::<T>();
}
fn test<T: ZeroSized>(_t: T) {
// assert zero-sizedness
let _ = T::_CHECK;
}
fn main() {
test(0i32);
}
But unfortunately this will only trigger an error when running cargo build
, but not on cargo check
. This means that tools like rust-analyser also don't pick up the error. You are basically forced to cargo build/run
all the time while writing code.
I'm wondering if there is a way to force the failure earlier to provide a bit of a better developer experience?