| David Tolnay | 795e7b4 | 2021-03-26 22:08:18 -0400 | [diff] [blame^] | 1 | use crate::syntax::resolve::Resolution; |
| 2 | use crate::syntax::Impl; |
| 3 | use proc_macro2::TokenStream; |
| 4 | use quote::ToTokens; |
| 5 | |
| 6 | pub struct ImplGenerics<'a> { |
| 7 | explicit_impl: Option<&'a Impl>, |
| 8 | resolve: Resolution<'a>, |
| 9 | } |
| 10 | |
| 11 | pub struct TyGenerics<'a> { |
| 12 | explicit_impl: Option<&'a Impl>, |
| 13 | resolve: Resolution<'a>, |
| 14 | } |
| 15 | |
| 16 | pub fn split_for_impl<'a>( |
| 17 | explicit_impl: Option<&'a Impl>, |
| 18 | resolve: Resolution<'a>, |
| 19 | ) -> (ImplGenerics<'a>, TyGenerics<'a>) { |
| 20 | let impl_generics = ImplGenerics { |
| 21 | explicit_impl, |
| 22 | resolve, |
| 23 | }; |
| 24 | let ty_generics = TyGenerics { |
| 25 | explicit_impl, |
| 26 | resolve, |
| 27 | }; |
| 28 | (impl_generics, ty_generics) |
| 29 | } |
| 30 | |
| 31 | impl<'a> ToTokens for ImplGenerics<'a> { |
| 32 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 33 | if let Some(imp) = self.explicit_impl { |
| 34 | imp.impl_generics.to_tokens(tokens); |
| 35 | } else { |
| 36 | self.resolve.generics.to_tokens(tokens); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl<'a> ToTokens for TyGenerics<'a> { |
| 42 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 43 | if let Some(imp) = self.explicit_impl { |
| 44 | imp.ty_generics.to_tokens(tokens); |
| 45 | } else { |
| 46 | self.resolve.generics.to_tokens(tokens); |
| 47 | } |
| 48 | } |
| 49 | } |