abi_stable_derive_lib/
lifetimes.rs

1
2
3
4
5
6
7use proc_macro2::TokenStream;
8use syn::token::Paren;
9use quote::{ToTokens};
10
11use crate::common_tokens::LifetimeTokens;
12
13
14#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
15pub(crate) enum LifetimeIndex {
16    Static,
17    Param { index: u8 },
18}
19
20impl LifetimeIndex {
21    /// Produces the tokens of the type_layout::LifetimeIndex version of this type
22    pub fn tokenizer<'a>(self,ctokens:&'a LifetimeTokens)->LifetimeIndexTokenizer<'a>{
23        LifetimeIndexTokenizer{li:self,ctokens}
24    }
25}
26
27
28pub struct LifetimeIndexTokenizer<'a>{
29    li:LifetimeIndex,
30    ctokens:&'a LifetimeTokens,
31}
32
33
34impl<'a> ToTokens for LifetimeIndexTokenizer<'a> {
35    fn to_tokens(&self, ts: &mut TokenStream) {
36        let ctokens=self.ctokens;
37        match self.li {
38            LifetimeIndex::Static=>{
39                ctokens.li_static.to_tokens(ts);
40            }
41            LifetimeIndex::Param{index,..}=>{
42                ctokens.li_index.to_tokens(ts);
43                Paren::default().surround(ts,|ts| index.to_tokens(ts) );
44            }
45        }
46    }
47}
48