type_proof/type_utils.rs
1//! Provides some helpers for working with Rust types.
2
3/// This trait just says a type is equal to itself, for making [`assert_type_eq`] work.
4///
5/// This is automatically implemented on every type.
6pub trait TypeEq {
7 /// This is always the type that the trait is implemented on.
8 type SelfType;
9}
10
11impl<T> TypeEq for T {
12 type SelfType = Self;
13}
14
15/// Statically asserts that two types are the same, throwing a type checker error if they are not.
16///
17/// # Example
18///
19/// ```
20/// use type_proof::type_utils::assert_type_eq;
21///
22/// type X = usize;
23/// assert_type_eq::<X, usize>();
24/// ```
25///
26/// ```compile_fail
27/// # use type_proof::type_utils::assert_type_eq;
28/// #
29/// assert_type_eq::<f32, usize>();
30/// ```
31pub fn assert_type_eq<T, U>()
32where
33 T: TypeEq<SelfType = U>,
34{
35}