abi_stable_derive/stable_abi/
common_tokens.rs

1//! This module defines the CommonTokens type,
2//! used to pass constants of type from `syn` to
3//! many functions in the `abi_stable_derive_lib::stable_abi` module.
4
5use proc_macro2::{Span, TokenStream as TokenStream2};
6
7use std::{
8    cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
9    marker::PhantomData,
10};
11
12use crate::Arenas;
13
14macro_rules! declare_common_tokens {
15    (
16        with_new[ $( $field_new:ident = $token_new:ty , )* ]
17        token[ $( $field_token:ident = $token_token:ident , )* ]
18        token_streams[ $( $field_ts:ident = $ts_str:expr , )* ]
19        types[ $( $field_ty:ident = $ty_str:expr , )* ]
20        idents[ $( $field_ident:ident = $ident_str:expr , )* ]
21        lifetime[ $( $lifetime_ident:ident = $lifetime_str:expr , )* ]
22        str_lits[ $( $strlit_ident:ident = $strlit_str:expr , )* ]
23    ) => {
24        #[derive(Debug)]
25        pub(crate) struct CommonTokens<'a>{
26            $( pub(crate) $field_new : $token_new , )*
27            $( pub(crate) $field_token : ::syn::token::$token_token , )*
28            $( pub(crate) $field_ts : TokenStream2 , )*
29            $( pub(crate) $field_ty : ::syn::Type , )*
30            $( pub(crate) $field_ident : ::syn::Ident , )*
31            $( pub(crate) $lifetime_ident : ::syn::Lifetime , )*
32            $( pub(crate) $strlit_ident : ::syn::LitStr , )*
33            _marker: PhantomData<&'a ()>,
34        }
35
36        impl<'a> CommonTokens<'a>{
37            #[allow(unused_variables)]
38            pub(crate) fn new(arenas:&'a Arenas)->Self{
39                let span=Span::call_site();
40                Self{
41                    $( $field_new : < $token_new >::new(span) , )*
42                    $( $field_token : Default::default() , )*
43                    $( $field_ts : ::syn::parse_str($ts_str).expect("BUG") , )*
44                    $( $field_ty : ::syn::parse_str($ty_str).expect("BUG") , )*
45                    $( $field_ident : ::syn::Ident::new($ident_str,span) , )*
46                    $( $lifetime_ident : ::syn::parse_str($lifetime_str).expect("BUG") , )*
47                    $( $strlit_ident : ::syn::LitStr::new($strlit_str,span) , )*
48                    _marker: PhantomData,
49                }
50            }
51        }
52
53        $(
54            impl<'a> AsRef<$token_new> for CommonTokens<'a>{
55                fn as_ref(&self)->&$token_new{
56                    &self.$field_new
57                }
58            }
59        )*
60    }
61}
62
63impl<'a> Eq for CommonTokens<'a> {}
64impl<'a> PartialEq for CommonTokens<'a> {
65    fn eq(&self, _other: &Self) -> bool {
66        true
67    }
68}
69
70impl<'a> PartialOrd for CommonTokens<'a> {
71    fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
72        Some(Ordering::Equal)
73    }
74}
75
76impl<'a> Ord for CommonTokens<'a> {
77    fn cmp(&self, _other: &Self) -> Ordering {
78        Ordering::Equal
79    }
80}
81
82declare_common_tokens! {
83    with_new[
84        start_len_tokens=crate::common_tokens::StartLenTokens,
85        fn_pointer_tokens=crate::common_tokens::FnPointerTokens,
86    ]
87
88    token[
89        and_=And,
90        comma=Comma,
91        equal=Eq,
92        colon2=Colon2,
93        bracket=Bracket,
94        paren=Paren,
95        lt=Lt,
96        gt=Gt,
97    ]
98
99    token_streams[
100        und_storage="__Storage,",
101    ]
102
103    types[
104        empty_tuple="()",
105    ]
106
107    idents[
108        some="Some",
109        none="None",
110        new="new",
111        comp_tl_fields="__CompTLFields",
112        //layout="LAYOUT",
113        static_equivalent="__GetStaticEquivalent",
114        cap_opaque_field="OPAQUE_FIELD",
115        cap_sabi_opaque_field="SABI_OPAQUE_FIELD",
116    ]
117
118    lifetime[
119        static_lt="'static",
120    ]
121
122    str_lits[
123    ]
124}