7 releases

new 0.1.6 Oct 16, 2020
0.1.5 Oct 13, 2020
0.1.4 Mar 6, 2020
0.1.3 Jan 26, 2020
0.1.0 Dec 29, 2019

#66 in Rust patterns

Download history 1107/week @ 2020-07-02 1196/week @ 2020-07-09 1195/week @ 2020-07-16 1483/week @ 2020-07-23 1679/week @ 2020-07-30 1809/week @ 2020-08-06 3168/week @ 2020-08-13 2360/week @ 2020-08-20 1567/week @ 2020-08-27 1476/week @ 2020-09-03 1623/week @ 2020-09-10 1327/week @ 2020-09-17 1366/week @ 2020-09-24 1501/week @ 2020-10-01 1609/week @ 2020-10-08 1133/week @ 2020-10-15

7,294 downloads per month
Used in 81 crates (5 directly)

MIT/Apache

43KB
745 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.

#[rustfmt::skip]
assert_eq!(
    std::any::type_name::<Option<String>>(), "core::option::Option<alloc::string::String>"
);

#[rustfmt::skip]
let tuples = vec![
    (tynm::type_name::<Option<String>>(),    "Option<String>"),
    (tynm::type_namem::<Option<String>>(1),  "core::..::Option<alloc::..::String>"),
    (tynm::type_namen::<Option<String>>(1),  "..::option::Option<..::string::String>"),
    // 1 segment from most and least significant modules.
    (tynm::type_namemn::<rust_out::two::three::Struct>(1, 1), "rust_out::..::three::Struct"),
    // traits
    (tynm::type_name::<dyn core::fmt::Debug>(), "dyn Debug"),
];

tuples
    .iter()
    .for_each(|(left, right)| assert_eq!(left, right));

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

~730KB
~14K SLoC