#named #name #user-friendly #stable #string #getting #api

named_type

An API for getting a user-friendly name string for a type on stable Rust

10 releases

0.2.2 Feb 5, 2020
0.2.1 Feb 12, 2019
0.2.0 Jan 30, 2019
0.1.6 Oct 17, 2018
0.1.3 Mar 15, 2017

#35 in #getting

Download history 1724/week @ 2024-04-09 2640/week @ 2024-04-16 1730/week @ 2024-04-23 1846/week @ 2024-04-30 1171/week @ 2024-05-07 1809/week @ 2024-05-14 2024/week @ 2024-05-21 2242/week @ 2024-05-28 1541/week @ 2024-06-04 1741/week @ 2024-06-11 1403/week @ 2024-06-18 920/week @ 2024-06-25 806/week @ 2024-07-02 1348/week @ 2024-07-09 567/week @ 2024-07-16 375/week @ 2024-07-23

3,244 downloads per month
Used in devii

MIT/Apache

4KB

This crate provides the NamedType trait. The named_type_derive crate also provides the ability to automatically derive this trait using #[derive(NamedType)].

Examples

You can derive NamedType for any struct or enum to get some obvious generated names. This is the expected usage of this crate for most types.

use named_type_derive::*;
use named_type::NamedType;

#[derive(NamedType)]
struct MyStruct {}

#[derive(NamedType)]
enum MyEnum {}

fn main() {
    assert_eq!(MyStruct::type_name(), concat!(module_path!(), "::MyStruct"));
    assert_eq!(MyStruct::short_type_name(), "MyStruct");

    assert_eq!(MyEnum::type_name(), concat!(module_path!(), "::MyEnum"));
    assert_eq!(MyEnum::short_type_name(), "MyEnum");
}

Since it's possible that short type names conflict, there is the option to add a prefix or suffix to a generated name to reduce ambiguity. Note that this only affects the short type name.


#[derive(NamedType)]
#[named_type(short_suffix = "_suffix")]
struct Suffixed {}

#[derive(NamedType)]
#[named_type(short_prefix = "Pre")]
enum Prefixed {}

assert_eq!(Suffixed::type_name(), concat!(module_path!(), "::Suffixed"));
assert_eq!(Suffixed::short_type_name(), "Suffixed_suffix");

assert_eq!(Prefixed::type_name(), concat!(module_path!(), "::Prefixed"));
assert_eq!(Prefixed::short_type_name(), "PrePrefixed");

No runtime deps