1 unstable release

0.0.3 Sep 24, 2023
0.0.2 Sep 23, 2023
0.0.1 Sep 23, 2023
0.0.0 Sep 23, 2023

#2182 in Rust patterns

Download history 1028/week @ 2024-03-13 231/week @ 2024-03-20 43/week @ 2024-03-27 39/week @ 2024-04-03 75/week @ 2024-04-10 255/week @ 2024-04-17 44/week @ 2024-04-24 651/week @ 2024-05-01 20/week @ 2024-05-08 28/week @ 2024-05-15 599/week @ 2024-05-22 139/week @ 2024-05-29 104/week @ 2024-06-05 99/week @ 2024-06-12 33/week @ 2024-06-19 22/week @ 2024-06-26

274 downloads per month

MIT/Apache

6KB

A crate that allows you to mostly-safely cast one type into another 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"

This operation is still unsafe because it allows you to extend lifetimes. There currently is not a way to prevent this

if let Ok(str) = unsafe { unty::<&'a str, &'static str>(input) } {
    // the compiler may now light your PC on fire
}

License

This crate is dual licenced MIT and Apache-2.0, at your own leisure


lib.rs:

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.
}

No runtime deps