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
//! A set of small traits that enable tokenizing some common types that get tokenizing erased
//! normally, such as `Option<T>` as `Some(_)` or `None`, `Box<T>` as `Box::new(_)`, etc.

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};

// Quoted type.
pub trait Quoted {
    fn quote(&self) -> TokenStream;
}

impl Quoted for String {
    fn quote(&self) -> TokenStream {
        quote! { #self.to_owned() }
    }
}

impl Quoted for glsl_lang::ast::SmolStr {
    fn quote(&self) -> TokenStream {
        let s = self.as_str();
        quote! { #s.into() }
    }
}

impl<T> Quoted for Option<T>
where
    T: ToTokens,
{
    fn quote(&self) -> TokenStream {
        if let Some(ref x) = *self {
            quote! { Some(#x) }
        } else {
            quote! { None }
        }
    }
}

impl<T> Quoted for &T
where
    T: ToTokens + ?Sized,
{
    fn quote(&self) -> TokenStream {
        quote! { Box::new(#self) }
    }
}