Crate unty

source ·
Expand description

A crate that allows you to untype your types.

This provides 2 functions:

type_equal allows you to check if two types are the same.

unty allows you to downcast a generic type into a concrete type.

This is mostly useful for generic functions, e.g.

pub fn foo<S>(s: S) {
    if let Ok(a) = unsafe { unty::<S, u8>(s) } {
        println!("It is an u8 with value {a}");
    } else {
        println!("it is not an u8");
    }
}
foo(10u8); // will print "it is an u8"
foo("test"); // will print "it is not an u8"

Note that both of these functions may give false positives if both types have lifetimes. There currently is not a way to prevent this. See type_equal for more information.

assert!(type_equal::<&'a str, &'static str>()); // these are not actually the same
if let Ok(str) = unsafe { unty::<&'a str, &'static str>(input) } {
    // this will extend the &'a str lifetime to be &'static, which is not allowed.
    // the compiler may now light your PC on fire.
}

Functions

  • Checks to see if the two types are equal.
  • unty
    Untypes your types. For documentation see the root of this crate.