aboutsummaryrefslogtreecommitdiff
path: root/macro/src/generics.rs
blob: 6c391fc72ac49416b3ac9382f79561157718b6f9 (plain)
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
46
47
48
49
50
51
52
53
54
55
use crate::syntax::instantiate::NamedImplKey;
use crate::syntax::resolve::Resolution;
use crate::syntax::Impl;
use proc_macro2::TokenStream;
use quote::ToTokens;

pub struct ImplGenerics<'a> {
    explicit_impl: Option<&'a Impl>,
    resolve: Resolution<'a>,
}

pub struct TyGenerics<'a> {
    key: NamedImplKey<'a>,
    explicit_impl: Option<&'a Impl>,
    resolve: Resolution<'a>,
}

pub fn split_for_impl<'a>(
    key: NamedImplKey<'a>,
    explicit_impl: Option<&'a Impl>,
    resolve: Resolution<'a>,
) -> (ImplGenerics<'a>, TyGenerics<'a>) {
    let impl_generics = ImplGenerics {
        explicit_impl,
        resolve,
    };
    let ty_generics = TyGenerics {
        key,
        explicit_impl,
        resolve,
    };
    (impl_generics, ty_generics)
}

impl<'a> ToTokens for ImplGenerics<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        if let Some(imp) = self.explicit_impl {
            imp.impl_generics.to_tokens(tokens);
        } else {
            self.resolve.generics.to_tokens(tokens);
        }
    }
}

impl<'a> ToTokens for TyGenerics<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        if let Some(imp) = self.explicit_impl {
            imp.ty_generics.to_tokens(tokens);
        } else {
            self.key.lt_token.to_tokens(tokens);
            self.resolve.generics.lifetimes.to_tokens(tokens);
            self.key.gt_token.to_tokens(tokens);
        }
    }
}