Crate typename [] [src]

This crate provides a compatible, safe and stable alternative to Rust's std::intrinsics::type_name intrinsic. This is achieved through the TypeName trait which is implemented for most types in std and can be implemented manually for custom types.

This crate also exposes a procedural macro to automatically derive the trait using #[derive(TypeName)]. This is an optional dependency which can be disabled by opting out from the derive feature in Cargo.


use typename::TypeName;

fn main() {
    assert_eq!(String::type_name(), "std::string::String");
    assert_eq!(Vec::<i32>::type_name(), "std::vec::Vec<i32>");
    assert_eq!([0, 1, 2].type_name_of(), "[i32; 3]");

You can derive the TypeName trait for custom types as follows:

Be careful when using this code, it's not being tested!
#[macro_use] extern crate typename;

struct Custom<T: TypeName> {
    some_t: T,

Prior work

This crate is inspired by the named_type crate which provides a similar interface. However, its output is not compatible with the type_name intrinsic, as its name does not contain the names of the concrete instanstances of the type parameters.



Trait which returns the canonical name of the implementing type.