You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When const_panic first stabilized, it didn't seem to be applicable for checking properties of generics, since const items inside functions can't be tied to the generics. However, an associated const on a struct seems to do the trick:
use std::mem::{align_of, size_of};structAssertSameLayout<A,B>(std::marker::PhantomData<(A,B)>);impl<A,B>AssertSameLayout<A,B>{constOK:() = assert!(
size_of::<A>() == size_of::<B>() && align_of::<A>() == align_of::<B>(),"types must have identical size and alignment");}// used as() = AssertSameLayout::<A,B>::OK;
When
const_panic
first stabilized, it didn't seem to be applicable for checking properties of generics, since const items inside functions can't be tied to the generics. However, an associated const on a struct seems to do the trick:Playground
Idea stolen from here.
The text was updated successfully, but these errors were encountered: