#generics #u8 #fire

unty

Explicitly types your generics

2 releases

0.0.4 Mar 7, 2025
0.0.3 Sep 24, 2023
0.0.2 Sep 23, 2023
0.0.1 Sep 23, 2023
0.0.0 Sep 23, 2023

#577 in Algorithms

Download history 90/week @ 2024-12-08 44/week @ 2024-12-15 74/week @ 2024-12-22 71/week @ 2024-12-29 254/week @ 2025-01-05 574/week @ 2025-01-12 106/week @ 2025-01-19 177/week @ 2025-01-26 117/week @ 2025-02-02 644/week @ 2025-02-09 158/week @ 2025-02-16 253/week @ 2025-02-23 14839/week @ 2025-03-02 29831/week @ 2025-03-09 31435/week @ 2025-03-16 35889/week @ 2025-03-23

112,014 downloads per month
Used in 477 crates (via bincode)

MIT/Apache

8KB

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