1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
//!
//! Rust name_of! Macro
//!
//! MIT License
//! Copyright (c) 2018 SilentByte <https://silentbyte.com/>
//!
#![crate_name = "nameof"]
/// Takes a binding, type, or function as an argument and returns its
/// unqualified string representation. If the identifier does not exist
/// in the current context, the macro will cause a compilation error.
/// This macro is mainly intended for debugging purposes and to improve
/// the refactoring experience compared to plain `stringify!()`.
///
/// The syntax depends on the type of the identifier:
///
/// 1. Bindings to variables and functions require no annotation,
/// e.g. `name_of!(some_binding)`.
///
/// 2. Types and structs require the keyword `type`, e.g. `name_of!(type SomeType)`.
/// Alternatively, the macro `name_of_type!(SomeType)` may be used.
///
/// 3. Fields within structs are referred to with the `in` keyword,
/// e.g. `name_of!(some_field in SomeType)`.
///
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate nameof;
/// # fn main() {
/// struct TestStruct {
/// test_field: i32,
/// }
///
/// struct GenericStruct<T> {
/// test_field_t: T,
/// }
///
/// fn greet() -> &'static str {
/// "Hi, World"
/// }
///
/// let text = "Hello, World!";
///
/// println!("Binding `{}` holds `{}`.", name_of!(text), text);
///
/// println!("Function `{}` says `{}`.", name_of!(greet), greet());
///
/// println!(
/// "Struct `{}` has a field `{}`.",
/// name_of!(type TestStruct),
/// name_of!(test_field in TestStruct)
/// );
///
/// println!(
/// "Generic Struct `{}` has a field `{}`.",
/// name_of!(type GenericStruct<String>),
/// name_of!(test_field_t in GenericStruct<String>)
/// );
///
/// println!(
/// "Standard types such as `{}` and `{}` also work.",
/// name_of!(type i32),
/// name_of!(type f64)
/// );
///
/// # }
/// ```
#[macro_export]
macro_rules! name_of {
// Covers Bindings
($n: ident) => {{
|| {
&$n;
};
stringify!($n)
}};
// Covers Types
(type $t: ty) => {{
name_of_type!($t)
}};
// Covers Struct Fields
($n: ident in $t: ty) => {{
|f: $t| {
let _ = &f.$n;
};
stringify!($n)
}};
}
/// Takes the name of a type as its sole parameter,
/// e.g. `name_of_type!(SomeStruct)` or `name_of_type!(f64)`.
///
/// It is an alternative to the `name_of!(type T)` macro, specifically for types.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate nameof;
/// # fn main() {
/// struct TestStruct {
/// test_field: i32
/// }
///
/// println!("Struct is called `{}`.", name_of_type!(TestStruct));
/// println!("Type is called `{}`.", name_of_type!(i32));
///
/// # }
/// ```
#[macro_export]
macro_rules! name_of_type {
// Covers Types
($t: ty) => {{
|| {
let _: $t;
};
stringify!($t)
}};
}
#[cfg(test)]
mod tests {
fn test_fn() {
//
}
struct TestStruct {
test_field: i32,
}
struct TestGenericStruct<T> {
test_field: T,
}
struct TestGenericStructMultiType<T, U> {
test_field_t: T,
test_field_u: U,
}
#[test]
fn name_of_binding() {
let test_variable = 123;
assert_eq!(name_of!(test_variable), "test_variable");
}
#[test]
fn name_of_fn() {
assert_eq!(name_of!(test_fn), "test_fn");
}
#[test]
fn name_of_type() {
assert_eq!(name_of!(type i32), "i32");
assert_eq!(name_of_type!(i32), "i32");
}
#[test]
fn name_of_struct() {
assert_eq!(name_of!(type TestStruct), "TestStruct");
assert_eq!(name_of_type!(TestStruct), "TestStruct");
}
#[test]
fn name_of_generic_struct() {
assert_eq!(
name_of!(type TestGenericStruct<i32>),
"TestGenericStruct<i32>"
);
assert_eq!(
name_of_type!(TestGenericStruct<i32>),
"TestGenericStruct<i32>"
);
}
#[test]
fn name_of_generic_multi_type_struct() {
assert_eq!(
name_of!(type TestGenericStructMultiType<i32, TestGenericStruct<String>>),
"TestGenericStructMultiType<i32, TestGenericStruct<String>>"
);
assert_eq!(
name_of_type!( TestGenericStructMultiType<i32, TestGenericStruct<String>>),
"TestGenericStructMultiType<i32, TestGenericStruct<String>>"
);
}
#[test]
fn name_of_struct_field() {
assert_eq!(name_of!(test_field in TestStruct), "test_field");
}
#[test]
fn name_of_generic_struct_field() {
assert_eq!(name_of!(test_field in TestGenericStruct<i32>), "test_field");
}
#[test]
fn name_of_generic_multi_type_struct_field() {
assert_eq!(
name_of!(test_field_t in TestGenericStructMultiType<i32, TestGenericStruct<String>>),
"test_field_t"
);
assert_eq!(
name_of!(test_field_u in TestGenericStructMultiType<i32, TestGenericStruct<String>>),
"test_field_u"
);
}
}