static-assertions icon indicating copy to clipboard operation
static-assertions copied to clipboard

`assert_eq_size!()` doesn't work with type paramenters

Open orium opened this issue 5 years ago • 2 comments

In version 0.3 it was possible to have a assert_eq_size!() inside a function, and refer to the type parameters of that function.

In version 1.0.0 that is no longer possible:

fn foo<T>() {
    static_assertions::assert_eq_size!(Rc<T>, Arc<T>);
}
error[E0401]: can't use generic parameters from outer function
   |
   | fn foo<T>() {
   |    --- - type parameter from outer function
   |    |
   |    try adding a local generic parameter in this method instead
   |     static_assertions::assert_eq_size!(Rc<T>, Arc<T>);
   |                                           ^ use of generic parameter from outer function

orium avatar Oct 03 '19 20:10 orium

assert_not_impl_any! also doesn't work from inside a function, which is very annoying since negative trait bounds are not supported...

LoganDark avatar Nov 15 '21 18:11 LoganDark

A way around this issue:

fn foo<T>() {
    let _ = Assert::<Rc<T>, Arc<T>>::EQ_SIZE;
}

struct Assert<T, U> {
    _marker: PhantomData<(T, U)>,
}

impl<T, U> Assert<T, U> {
    const EQ_SIZE: () = assert!(mem::size_of::<T>() == mem::size_of::<U>());
}

yescallop avatar Apr 13 '22 12:04 yescallop