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
#![warn(rust_2018_idioms, unreachable_pub)]
#![deny(elided_lifetimes_in_paths)]
#![forbid(unsafe_code)]
#[doc(hidden)]
pub mod private {
pub use core::{
default::Default,
option::Option::{None, Some}
};
pub use proc_macro2::{Ident, Span};
pub use syn::{punctuated::Punctuated, Path, PathSegment};
}
#[macro_export]
macro_rules! path {
(:: $($segment:ident)::*) => {
path!(@private $crate::private::Some($crate::private::Default::default()), $($segment),*)
};
($($segment:ident)::*) => {
path!(@private $crate::private::None, $($segment),*)
};
(@private $leading_colon:expr, $($segment:ident),*) => {
{
#[allow(unused_mut)]
let mut segments: $crate::private::Punctuated<$crate::private::PathSegment, _> = $crate::private::Default::default();
$(
segments.push($crate::private::PathSegment {
ident: $crate::private::Ident::new(stringify!($segment), $crate::private::Span::call_site()),
arguments: $crate::private::Default::default()
});
)*
$crate::private::Path {
leading_colon: $leading_colon,
segments
}
}
};
}
#[cfg(test)]
mod tests {
use syn::Path;
#[test]
fn with_leading_colon() {
let path = path!(::my_crate::my_mod::FooBar);
let expected: Path = syn::parse_str("::my_crate::my_mod::FooBar").unwrap();
assert_eq!(expected, path);
}
#[test]
fn without_leading_colon() {
let path = path!(my_crate::my_mod::FooBar);
let expected: Path = syn::parse_str("my_crate::my_mod::FooBar").unwrap();
assert_eq!(expected, path);
}
}