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
use proc_macro::Span;
use proc_macro2::TokenStream as TokenStream2;
use quote::ToTokens;
pub trait Spanned {
fn span(&self) -> Span;
}
impl<T: ToTokens> Spanned for T {
fn span(&self) -> Span {
let mut tokens = TokenStream2::new();
self.to_tokens(&mut tokens);
let mut iter = tokens.into_iter();
let mut span = match iter.next() {
Some(tt) => tt.span().unstable(),
None => {
return Span::call_site();
}
};
for tt in iter {
if let Some(joined) = span.join(tt.span().unstable()) {
span = joined;
}
}
span
}
}