abi_stable_derive/
arenas.rs

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![allow(clippy::mut_from_ref)]

use std::fmt;

use typed_arena::Arena;

macro_rules! declare_arenas {
    (
        $( $field_name:ident : $arena_type:ty , )*
    ) => {
        pub(crate) struct Arenas {
            $(pub(crate) $field_name : Arena<$arena_type>, )*
        }

        impl Default for Arenas{
            fn default()->Self{
                Arenas{
                    $( $field_name:Arena::new(), )*
                }
            }
        }

        impl fmt::Debug for Arenas{
            fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
                fmt::Debug::fmt("Arenas{..}",f)
            }
        }

        pub trait AllocMethods<T>{
            fn alloc(&self, value: T) -> &T {
                self.alloc_mut(value)
            }

            fn alloc_mut(&self, value: T) -> &mut T ;

            fn alloc_extend<I>(&self, iterable: I) -> &[T]
            where
                I: IntoIterator<Item = T>
            {
                self.alloc_extend_mut(iterable)
            }

            fn alloc_extend_mut<I>(&self, iterable: I) -> &mut [T]
            where
                I: IntoIterator<Item = T>;
        }


        $(
            impl AllocMethods<$arena_type> for Arenas{
                fn alloc_mut(&self, value: $arena_type) -> &mut $arena_type {
                    self.$field_name.alloc(value)
                }

                fn alloc_extend_mut<I>(&self, iterable: I) -> &mut [$arena_type]
                where
                    I: IntoIterator<Item = $arena_type>
                {
                    self.$field_name.alloc_extend(iterable)
                }
            }

        )*

    }
}

declare_arenas! {
    vec_meta: Vec<syn::Attribute>,
    vec_expr: Vec<syn::Expr>,
    ident: syn::Ident,
    ident_vec: Vec<syn::Ident>,
    trait_bound: syn::TraitBound,
    lifetimes:syn::Lifetime,
    fields_named: syn::FieldsNamed,
    types: syn::Type,
    // metalists: syn::MetaList,
    lifetime_defs: syn::LifetimeDef,
    tokenstream: proc_macro2::TokenStream,
    expr: syn::Expr,
    strings: String,
    paths: syn::Path,
}