#typename #type #name

tynm

Returns type names in shorter form

5 releases

✓ Uses Rust 2018 edition

0.1.4 Mar 6, 2020
0.1.3 Jan 26, 2020
0.1.2 Jan 10, 2020
0.1.1 Jan 2, 2020
0.1.0 Dec 29, 2019

#521 in Rust patterns

Download history 75/week @ 2019-12-28 74/week @ 2020-01-04 103/week @ 2020-01-11 52/week @ 2020-01-18 69/week @ 2020-01-25 190/week @ 2020-02-01 376/week @ 2020-02-08 763/week @ 2020-02-15 627/week @ 2020-02-22 492/week @ 2020-02-29 708/week @ 2020-03-07 679/week @ 2020-03-14 931/week @ 2020-03-21 945/week @ 2020-03-28

942 downloads per month
Used in 39 crates (4 directly)

MIT/Apache

37KB
619 lines

Crates.io Build Status Build Status Coverage Status

Tynm -- Type Name

Returns type names with a specifiable number of module segments as a String.

// === std library === //
assert_eq!(
    std::any::type_name::<Option<String>>(),
    "core::option::Option<alloc::string::String>",
);

// === tynm === //
// Simple type name:
assert_eq!(tynm::type_name::<Option<String>>(), "Option<String>",);

// Type name with 1 module segment, starting from the most significant module.
assert_eq!(
    tynm::type_namem::<Option<String>>(1),
    "core::..::Option<alloc::..::String>",
);

// Type name with 1 module segment, starting from the least significant module.
assert_eq!(
    tynm::type_namen::<Option<String>>(1),
    "..::option::Option<..::string::String>",
);

// Type name with 1 module segment from both the most and least significant modules.
#[rustfmt::skip]
mod rust_out { pub mod two { pub mod three { pub struct Struct; } } }
assert_eq!(
    tynm::type_namemn::<rust_out::two::three::Struct>(1, 1),
    "rust_out::..::three::Struct",
);

Motivation

The std::any::type_name function stabilized in Rust 1.38 returns the fully qualified type name with all module segments. This can be difficult to read in error messages, especially for type-parameterized types.

Often, the simple type name is more readable, and enough to distinguish the type referenced in an error.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~690KB
~13K SLoC