[][src]Crate typename

DEPRECATION NOTICE: This crate has been deprecated. The type_name intrinsic has been stablized in Rust 1.38. Users of this crate are asked to migrate to std::any::type_name.

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:

This example is not tested
#[macro_use] extern crate typename;

// Note that Custom<T> will only implement TypeName when T does, too.
struct Custom<T> {
    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 instances of the type parameters.



Trait which returns the canonical name of the implementing type.