Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ fn main() {
name_of!(type i32),
name_of!(type f64)
);

println!("{}", path_of!(crate::greet));
}
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ macro_rules! name_of_type {
}};
}

/// Takes the path of a type/mod/const.. as its path &str,
/// e.g. `path_of!(web::info::Foo)`.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate nameof;
/// use web::info::Foo;
///
/// # fn main() {
///
/// println!("struct path `{}`.", path_of!(web::info::Foo));
///
/// # }
/// ```
#[macro_export]
macro_rules! path_of {
($p: path) => {{
let _ = || {
use $p;
};

stringify!($p)
}};
}

#[cfg(test)]
mod tests {
fn test_fn() {
Expand Down Expand Up @@ -180,6 +206,11 @@ mod tests {
assert_eq!(name_of_type!(i32), "i32");
}

#[test]
fn path_of() {
assert_eq!(path_of!(test_fn), "test_fn");
}

#[test]
fn name_of_struct() {
assert_eq!(name_of!(type TestStruct), "TestStruct");
Expand Down