abi_stable_derive/
arenas.rs1#![allow(clippy::mut_from_ref)]
2
3use std::fmt;
4
5use typed_arena::Arena;
6
7macro_rules! declare_arenas {
8 (
9 $( $field_name:ident : $arena_type:ty , )*
10 ) => {
11 pub(crate) struct Arenas {
12 $(pub(crate) $field_name : Arena<$arena_type>, )*
13 }
14
15 impl Default for Arenas{
16 fn default()->Self{
17 Arenas{
18 $( $field_name:Arena::new(), )*
19 }
20 }
21 }
22
23 impl fmt::Debug for Arenas{
24 fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
25 fmt::Debug::fmt("Arenas{..}",f)
26 }
27 }
28
29 pub trait AllocMethods<T>{
30 fn alloc(&self, value: T) -> &T {
31 self.alloc_mut(value)
32 }
33
34 fn alloc_mut(&self, value: T) -> &mut T ;
35
36 fn alloc_extend<I>(&self, iterable: I) -> &[T]
37 where
38 I: IntoIterator<Item = T>
39 {
40 self.alloc_extend_mut(iterable)
41 }
42
43 fn alloc_extend_mut<I>(&self, iterable: I) -> &mut [T]
44 where
45 I: IntoIterator<Item = T>;
46 }
47
48
49 $(
50 impl AllocMethods<$arena_type> for Arenas{
51 fn alloc_mut(&self, value: $arena_type) -> &mut $arena_type {
52 self.$field_name.alloc(value)
53 }
54
55 fn alloc_extend_mut<I>(&self, iterable: I) -> &mut [$arena_type]
56 where
57 I: IntoIterator<Item = $arena_type>
58 {
59 self.$field_name.alloc_extend(iterable)
60 }
61 }
62
63 )*
64
65 }
66}
67
68declare_arenas! {
69 vec_meta: Vec<syn::Attribute>,
70 vec_expr: Vec<syn::Expr>,
71 ident: syn::Ident,
72 ident_vec: Vec<syn::Ident>,
73 trait_bound: syn::TraitBound,
74 lifetimes:syn::Lifetime,
75 fields_named: syn::FieldsNamed,
76 types: syn::Type,
77 lifetime_defs: syn::LifetimeDef,
79 tokenstream: proc_macro2::TokenStream,
80 expr: syn::Expr,
81 strings: String,
82 paths: syn::Path,
83}