Expand description
§pretty-name
Get the human-friendly name of types, functions, methods, fields, and enum variants in a refactoring-safe way.
§Overview
pretty-name provides a set of macros and functions for extracting names of Rust language constructs at compile time. Unlike stringify! or std::any::type_name, this crate offers:
§Key Features
-
Human-friendly output: Type names are cleaned to remove module paths (
std::vec::Vec<T>→Vec<T>), lifetime annotations (&'static str→&str), and other visual clutter. -
Refactoring-safe: When you rename items using IDE refactoring tools, the macro calls are automatically updated.
-
Compile-time validation: All macros check that the referenced items exist. If a referenced identifier, field, method, or variant doesn’t exist, you get a compile error instead of a runtime panic.
-
Natural syntax: No strange custom syntax like `pretty_name!(type i32).
-
Selfsupport: ResolvesSelfinto the appropriate type.
§Installation
Add this to your Cargo.toml:
[dependencies]
pretty-name = "0.1"Or use cargo add:
cargo add pretty-name§Usage
§Type Names
Get human-friendly type names with pretty_name::type_name<T>() for types and pretty_name::type_name_of(value) for values:
use pretty_name::{type_name, type_name_of};
// Get type name from a type parameter
assert_eq!(type_name::<Option<i32>>(), "Option<i32>");
assert_eq!(type_name::<&str>(), "&str");
assert_eq!(type_name::<Vec<Box<dyn std::fmt::Debug>>>(), "Vec<Box<dyn Debug>>");
// Get type name from a value
let s = "hello";
assert_eq!(type_name_of(&s), "&str");
let v = vec![1, 2, 3];
assert_eq!(type_name_of(&v), "Vec<i32>");The output removes module paths (e.g., std::vec::Vec becomes Vec) and lifetime annotations for cleaner, more readable type names.
§Identifier Names
Get the name of local variables, constants, and functions with pretty_name::of!:
fn my_function() {}
let my_variable = 42;
assert_eq!(pretty_name::of!(my_function), "my_function");
assert_eq!(pretty_name::of!(my_variable), "my_variable");The macro validates that the identifier exists in the current scope. If you rename my_variable using your IDE’s refactoring tools, the macro call will be updated automatically.
§Struct Field Names
Get the name of struct fields with pretty_name::of_field!:
struct MyStruct {
my_field: i32,
}
assert_eq!(pretty_name::of_field!(MyStruct::my_field), "MyStruct::my_field");This macro resolves Self when used inside impl blocks and validates that the field exists on the type.
§Method Names
Get the name of methods with pretty_name::of_method!:
struct MyStruct;
impl MyStruct {
fn my_method(&self) {}
}
assert_eq!(pretty_name::of_method!(MyStruct::my_method), "MyStruct::my_method");The macro resolves Self when used inside impl blocks and validates that the method exists on the type.
§Enum Variant Names
Get the name of enum variants with pretty_name::of_variant!. Supports unit, tuple, and struct variants:
enum MyEnum {
UnitVariant,
TupleVariant(i32, String),
StructVariant { field: i32 },
}
// Unit variant
assert_eq!(pretty_name::of_variant!(MyEnum::UnitVariant), "MyEnum::UnitVariant");
// Tuple variant - use (..) syntax
assert_eq!(pretty_name::of_variant!(MyEnum::TupleVariant(..)), "MyEnum::TupleVariant");
// Struct variant - use {..} syntax
assert_eq!(pretty_name::of_variant!(MyEnum::StructVariant {..}), "MyEnum::StructVariant");The macro resolves Self when used inside impl blocks and validates that the variant exists on the enum type.
§License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
§Links
Macros§
- of
- Get the name of the given identifier.
- of_
field - Get the name of the given struct field.
- of_
method - Get the name of the given method.
- of_
variant - Get the name of the given enum variant.
Functions§
- type_
name - Get the human-friendly type name of given type
T, removing visual clutter such as full module paths. - type_
name_ of - Get the human-friendly type name of the given value, removing visual clutter such as full module paths.