Skip to main content

template_quote/
lib.rs

1#![doc = include_str!("../README.md")]
2
3/// See module-level doc.
4pub use template_quote_impl::quote;
5/// [`quote_configured!`] macro is configurable version of [`quote!`].
6///
7/// ```ignore
8/// # use template_quote::quote_configured;
9/// quote_configured! {
10/// 	{
11/// 		proc_macro2: ::proc_macro2,
12/// 		quote: ::quote,
13/// 		core: ::core,	// core crate in std
14/// 		quote: ::quote,
15/// 		span: ::some_span,
16/// 	} =>
17/// 	...
18/// };
19/// ```
20pub use template_quote_impl::quote_configured;
21
22/// [`quote_spanned!`] macro emit `TokenTree` with specified
23/// `Span`.
24///
25/// ```ignore
26/// use syn::Span;
27/// let span = Span::call_site();
28/// let tokens = quote_spanned! {span => ... };
29/// ```
30pub use template_quote_impl::quote_spanned;
31
32pub use imp::Repeat;
33pub use quote::ToTokens;
34
35mod imp {
36	use quote::ToTokens;
37	use std::borrow::Borrow;
38	use std::slice;
39
40	// This trait is from `proc-quote` crate.
41	pub unsafe trait Repeat<T: Iterator> {
42		#[allow(non_snake_case)]
43		#[doc(hidden)]
44		fn __template_quote__as_repeat(self) -> T;
45
46		#[allow(non_snake_case)]
47		#[doc(hidden)]
48		fn __template_quote_is_iterable(&self) -> bool;
49	}
50
51	unsafe impl<T, I: Iterator<Item = T>> Repeat<I> for I {
52		fn __template_quote__as_repeat(self) -> I {
53			self
54		}
55
56		fn __template_quote_is_iterable(&self) -> bool {
57			true
58		}
59	}
60
61	unsafe impl<'a, T: 'a, S: Borrow<[T]>> Repeat<slice::Iter<'a, T>> for &'a S {
62		fn __template_quote__as_repeat(self) -> slice::Iter<'a, T> {
63			(*self).borrow().iter()
64		}
65
66		fn __template_quote_is_iterable(&self) -> bool {
67			true
68		}
69	}
70
71	unsafe impl<'a, T: ToTokens + 'a> Repeat<ToTokensRepeat<'a, T>> for &'a T {
72		fn __template_quote__as_repeat(self) -> ToTokensRepeat<'a, T> {
73			ToTokensRepeat(self)
74		}
75
76		fn __template_quote_is_iterable(&self) -> bool {
77			false
78		}
79	}
80
81	pub struct ToTokensRepeat<'a, T: ToTokens + 'a>(&'a T);
82	impl<'a, T: ToTokens + 'a> Iterator for ToTokensRepeat<'a, T> {
83		type Item = &'a T;
84		fn next(&mut self) -> Option<Self::Item> {
85			Some(self.0)
86		}
87	}
88}