blob: f553207d97a2fa4ccc2273ff256d3c355a6f76b2 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::{Derive, ExternFn, Ref, Ty1, Type, Var};
2use proc_macro2::{Ident, Span, TokenStream};
3use quote::{quote_spanned, ToTokens};
4use syn::Token;
5
6impl ToTokens for Type {
7 fn to_tokens(&self, tokens: &mut TokenStream) {
8 match self {
9 Type::Ident(ident) => {
10 if ident == "CxxString" {
11 let span = ident.span();
12 tokens.extend(quote_spanned!(span=> ::cxx::));
13 }
14 ident.to_tokens(tokens);
15 }
16 Type::RustBox(ty) | Type::UniquePtr(ty) => ty.to_tokens(tokens),
17 Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
18 }
19 }
20}
21
22impl ToTokens for Var {
23 fn to_tokens(&self, tokens: &mut TokenStream) {
24 self.ident.to_tokens(tokens);
25 Token![:](self.ident.span()).to_tokens(tokens);
26 self.ty.to_tokens(tokens);
27 }
28}
29
30impl ToTokens for Ty1 {
31 fn to_tokens(&self, tokens: &mut TokenStream) {
32 if self.name == "UniquePtr" {
33 let span = self.name.span();
34 tokens.extend(quote_spanned!(span=> ::cxx::));
35 }
36 self.name.to_tokens(tokens);
37 self.langle.to_tokens(tokens);
38 self.inner.to_tokens(tokens);
39 self.rangle.to_tokens(tokens);
40 }
41}
42
43impl ToTokens for Ref {
44 fn to_tokens(&self, tokens: &mut TokenStream) {
45 self.ampersand.to_tokens(tokens);
46 self.mutability.to_tokens(tokens);
47 self.inner.to_tokens(tokens);
48 }
49}
50
51impl ToTokens for Derive {
52 fn to_tokens(&self, tokens: &mut TokenStream) {
53 let name = match self {
54 Derive::Clone => "Clone",
55 Derive::Copy => "Copy",
56 };
57 Ident::new(name, Span::call_site()).to_tokens(tokens);
58 }
59}
60
61impl ToTokens for ExternFn {
62 fn to_tokens(&self, tokens: &mut TokenStream) {
63 self.fn_token.to_tokens(tokens);
64 self.ident.to_tokens(tokens);
65 self.semi_token.to_tokens(tokens);
66 }
67}