1 | //! Macros that should be exported from both `darling_core` and `darling`. |
2 | //! Note that these are **sym-linked** into the main code, and so cannot declare on items that are exported differently |
3 | //! in `darling_core` vs. `darling`. |
4 | |
5 | /// Generator for `UsesTypeParam` impls that unions the used type parameters of the selected fields. |
6 | /// |
7 | /// # Usage |
8 | /// The macro takes the type implementing the trait as the first argument, then a comma-separated list of |
9 | /// fields for the rest of its arguments. |
10 | /// |
11 | /// The type of each passed-in field must implement `UsesTypeParams`, or the resulting code won't compile. |
12 | /// |
13 | /// ```rust |
14 | /// # extern crate syn; |
15 | /// # use darling_core::uses_type_params; |
16 | /// # |
17 | /// struct MyField { |
18 | /// ty: syn::Type, |
19 | /// } |
20 | /// |
21 | /// uses_type_params!(MyField, ty); |
22 | /// |
23 | /// fn main() { |
24 | /// // no test run |
25 | /// } |
26 | /// ``` |
27 | /// |
28 | /// `darling` cannot derive this trait automatically, as it doesn't know which information extracted from |
29 | /// proc-macro input is meant to constitute "using" the type parameter, but crate consumers should |
30 | /// implement it by hand or using the macro. |
31 | #[macro_export ] |
32 | macro_rules! uses_type_params { |
33 | ($impl_type:ty, $accessor:ident) => { |
34 | impl $crate::usage::UsesTypeParams for $impl_type { |
35 | fn uses_type_params<'gen>( |
36 | &self, |
37 | options: &$crate::usage::Options, |
38 | type_set: &'gen $crate::usage::IdentSet |
39 | ) -> $crate::usage::IdentRefSet<'gen> { |
40 | self.$accessor.uses_type_params(options, type_set) |
41 | } |
42 | } |
43 | }; |
44 | ($impl_type:ty, $first:ident, $($field:ident),+) => { |
45 | impl $crate::usage::UsesTypeParams for $impl_type { |
46 | fn uses_type_params<'gen>( |
47 | &self, |
48 | options: &$crate::usage::Options, |
49 | type_set: &'gen $crate::usage::IdentSet |
50 | ) -> $crate::usage::IdentRefSet<'gen> { |
51 | let mut hits = self.$first.uses_type_params(options, type_set); |
52 | $( |
53 | hits.extend(self.$field.uses_type_params(options, type_set)); |
54 | )* |
55 | hits |
56 | } |
57 | } |
58 | }; |
59 | } |
60 | |
61 | /// Generator for `UsesLifetimes` impls that unions the used lifetimes of the selected fields. |
62 | /// |
63 | /// # Usage |
64 | /// The macro takes the type implementing the trait as the first argument, then a comma-separated list of |
65 | /// fields for the rest of its arguments. |
66 | /// |
67 | /// The type of each passed-in field must implement `UsesLifetimes`, or the resulting code won't compile. |
68 | #[macro_export ] |
69 | macro_rules! uses_lifetimes { |
70 | ($impl_type:ty, $accessor:ident) => { |
71 | impl $crate::usage::UsesLifetimes for $impl_type { |
72 | fn uses_lifetimes<'gen>( |
73 | &self, |
74 | options: &$crate::usage::Options, |
75 | type_set: &'gen $crate::usage::LifetimeSet |
76 | ) -> $crate::usage::LifetimeRefSet<'gen> { |
77 | self.$accessor.uses_lifetimes(options, type_set) |
78 | } |
79 | } |
80 | }; |
81 | ($impl_type:ty, $first:ident, $($field:ident),+) => { |
82 | impl $crate::usage::UsesLifetimes for $impl_type { |
83 | fn uses_lifetimes<'gen>( |
84 | &self, |
85 | options: &$crate::usage::Options, |
86 | type_set: &'gen $crate::usage::LifetimeSet |
87 | ) -> $crate::usage::LifetimeRefSet<'gen> { |
88 | let mut hits = self.$first.uses_lifetimes(options, type_set); |
89 | $( |
90 | hits.extend(self.$field.uses_lifetimes(options, type_set)); |
91 | )* |
92 | hits |
93 | } |
94 | } |
95 | }; |
96 | } |
97 | |