glsl_lang_quote/
quoted.rs

1//! A set of small traits that enable tokenizing some common types that get tokenizing erased
2//! normally, such as `Option<T>` as `Some(_)` or `None`, `Box<T>` as `Box::new(_)`, etc.
3
4use proc_macro2::TokenStream;
5use quote::{quote, ToTokens};
6
7// Quoted type.
8pub trait Quoted {
9    fn quote(&self) -> TokenStream;
10}
11
12impl Quoted for String {
13    fn quote(&self) -> TokenStream {
14        quote! { #self.to_owned() }
15    }
16}
17
18impl Quoted for glsl_lang::ast::SmolStr {
19    fn quote(&self) -> TokenStream {
20        let s = self.as_str();
21        quote! { #s.into() }
22    }
23}
24
25impl<T> Quoted for Option<T>
26where
27    T: ToTokens,
28{
29    fn quote(&self) -> TokenStream {
30        if let Some(ref x) = *self {
31            quote! { Some(#x) }
32        } else {
33            quote! { None }
34        }
35    }
36}
37
38impl<T> Quoted for &T
39where
40    T: ToTokens + ?Sized,
41{
42    fn quote(&self) -> TokenStream {
43        quote! { Box::new(#self) }
44    }
45}