Crate typedef

Source
Expand description

TypeDef is used to identify and compare types, as well as print their names.

If you do not need readable type name, you should use TypeId. This wrapper re-implements TypeId.

Since Rust 1.0, this library can only work on nightly Rust. To activate the nice names instead of gobbledygook, include this library with features = ["nightly"] configuration parameter. On stable rust, it falls back to gobbledygook (type identifier) instead of a nice name.

To get a name of a type:

โ“˜
use typedef::{ TypeDef };

assert_eq!(TypeDef::name_of::<i64>(), "i64");

Type can also serve as type identifier and name container:

โ“˜
use typedef::{ TypeDef };

let typedef = TypeDef::of::<i64>();

assert!(typedef.is::<i64>());
assert_eq!(typedef.get_str(), "i64");

More common usage would be in a generic method:

โ“˜
use std::any::Any;
use std::fmt;
use typedef::TypeDef;

fn foo<T: Any + fmt::Debug>(value: T) -> String {
    format!(
        "the value of {} type is {:?}",
        TypeDef::of::<T>(),
        value
    )
}

fn main() {
    assert_eq!(foo(15), "the value of i32 type is 15");
}

Structsยง

TypeDef