quote2/
lib.rs

1#![doc = include_str!("../README.md")]
2use core::fmt;
3
4#[doc(hidden)]
5pub mod tt;
6pub mod utils;
7pub use proc_macro2;
8
9use proc_macro2::{TokenStream, TokenTree};
10pub use quote::{format_ident, ToTokens};
11pub use quote2_macros::{quote, quote_spanned};
12
13pub trait Quote: Extend<TokenTree> {
14    fn add_tokens(&mut self, _: impl ToTokens);
15}
16
17impl Quote for TokenStream {
18    #[inline]
19    fn add_tokens(&mut self, t: impl ToTokens) {
20        t.to_tokens(self);
21    }
22}
23
24#[derive(Clone, Copy)]
25pub struct QuoteFn<T>(pub T);
26
27impl<T> From<T> for QuoteFn<T> {
28    #[inline]
29    fn from(value: T) -> Self {
30        Self(value)
31    }
32}
33
34#[inline]
35pub fn quote<F>(f: F) -> QuoteFn<F>
36where
37    F: Fn(&mut TokenStream),
38{
39    QuoteFn(f)
40}
41
42impl<F> quote::ToTokens for QuoteFn<F>
43where
44    F: Fn(&mut TokenStream),
45{
46    #[inline]
47    fn to_tokens(&self, tokens: &mut TokenStream) {
48        (self.0)(tokens)
49    }
50}
51
52impl<F> fmt::Debug for QuoteFn<F> {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.debug_struct("QuoteFn").finish()
55    }
56}
57
58impl<T> std::ops::Deref for QuoteFn<T> {
59    type Target = T;
60    #[inline]
61    fn deref(&self) -> &Self::Target {
62        &self.0
63    }
64}
65
66impl<T> std::ops::DerefMut for QuoteFn<T> {
67    #[inline]
68    fn deref_mut(&mut self) -> &mut Self::Target {
69        &mut self.0
70    }
71}