Skip to main content

macro_tools/
quantifier.rs

1// HACK: The following line is a temporary workaround for a bug in the linter.
2// This line will be removed automatically when the bug is fixed.
3// Please, do not remove this line manually.
4// #![allow(clippy ::too_many_lines)]
5//!
6//! Quantifiers like Pair and Many.
7//!
8
9/// Define a private namespace for all its items.
10mod private 
11{
12  extern crate alloc;
13
14  use crate :: *;
15
16  ///
17  /// Marker saying how to parse several elements of such type in a row.
18  ///
19  pub trait AsMuchAsPossibleNoDelimiter {}
20
21  /// Element of parsing.
22  pub trait Element
23  where
24  // Self: syn ::parse ::Parse + quote ::ToTokens,
25  Self: quote ::ToTokens,
26  {
27 }
28
29  impl< T > Element for T where
30  // Self: syn ::parse ::Parse + quote ::ToTokens,
31  Self: quote ::ToTokens,
32  {
33 }
34
35  /// Pair of two elements of parsing.
36  #[ derive( Debug, PartialEq, Eq, Clone, Default ) ]
37  pub struct Pair< T1: Element, T2: Element >(pub T1, pub T2);
38
39  impl< T1, T2 > Pair< T1, T2 >
40  where
41  T1: Element,
42  T2: Element,
43  {
44  /// Constructor.
45  pub fn new(src1: T1, src2: T2) -> Self
46  {
47   Self(src1, src2)
48 }
49 }
50
51  impl< T1, T2 > From< (T1, T2) > for Pair< T1, T2 >
52  where
53  T1: Element,
54  T2: Element,
55  {
56  #[ inline( always ) ]
57  fn from(src: (T1, T2)) -> Self 
58  {
59   Self(src.0, src.1)
60 }
61 }
62
63  impl< T1, T2 > From< Pair<T1, T2 >> for (T1, T2)
64  where
65  T1: Element,
66  T2: Element,
67  {
68  #[ inline( always ) ]
69  fn from(src: Pair< T1, T2 >) -> Self 
70  {
71   (src.0, src.1)
72 }
73 }
74
75  impl< T1, T2 > syn ::parse ::Parse for Pair< T1, T2 >
76  where
77  T1: Element + syn ::parse ::Parse,
78  T2: Element + syn ::parse ::Parse,
79  {
80  fn parse(input: ParseStream< '_ >) -> syn ::Result< Self > 
81  {
82   Ok(Self(input.parse()?, input.parse()?))
83 }
84 }
85
86  impl< T1, T2 > quote ::ToTokens for Pair< T1, T2 >
87  where
88  T1: Element + quote ::ToTokens,
89  T2: Element + quote ::ToTokens,
90  {
91  fn to_tokens(&self, tokens: &mut proc_macro2 ::TokenStream) 
92  {
93   self.0.to_tokens(tokens);
94   self.1.to_tokens(tokens);
95 }
96 }
97
98  ///
99  /// Parse as much elements as possible.
100  ///
101  #[ derive( Debug, PartialEq, Eq, Clone, Default ) ]
102  pub struct Many< T: quote ::ToTokens >(pub Vec< T >);
103
104  impl< T > Many< T >
105  where
106  T: Element,
107  {
108  /// Constructor.
109  #[ must_use ]
110  pub fn new() -> Self
111  {
112   Self(Vec ::new())
113 }
114  /// Constructor.
115  #[ must_use ]
116  pub fn new_with(src: Vec< T >) -> Self
117  {
118   Self(src)
119 }
120  /// Iterator
121  pub fn iter( &self ) -> core ::slice ::Iter< '_, T >
122  {
123   self.0.iter()
124 }
125 }
126
127  impl< T > From< Vec< T >> for Many< T >
128  where
129  T: quote ::ToTokens,
130  {
131  #[ inline( always ) ]
132  fn from(src: Vec< T >) -> Self 
133  {
134   Self(src)
135 }
136 }
137
138  impl< T > From< Many<T >> for Vec< T >
139  where
140  T: quote ::ToTokens,
141  {
142  #[ inline( always ) ]
143  fn from(src: Many< T >) -> Self 
144  {
145   src.0
146 }
147 }
148
149  impl< T > IntoIterator for Many< T >
150  where
151  T: quote ::ToTokens,
152  {
153  type Item = T;
154  #[ allow( clippy ::std_instead_of_alloc ) ]
155  type IntoIter = alloc ::vec ::IntoIter< Self ::Item >;
156  fn into_iter(self) -> Self ::IntoIter 
157  {
158   self.0.into_iter()
159 }
160 }
161
162  impl< 'a, T > IntoIterator for &'a Many< T >
163  where
164  T: quote ::ToTokens,
165  {
166  type Item = &'a T;
167  type IntoIter = core ::slice ::Iter< 'a, T >;
168  fn into_iter(self) -> Self ::IntoIter 
169  {
170   // let x = vec![ 1, 2, 3 ].iter();
171   (self.0).iter()
172 }
173 }
174
175  // impl< T > From< Many< T > > for Vec<  T  >
176  // where
177  //   T: Element,
178  // {
179  //   fn from( src: Many< T > ) -> Self
180  //   {
181  //     src.0
182  // }
183  // }
184
185  impl< T > quote ::ToTokens for Many< T >
186  where
187  T: Element + quote ::ToTokens,
188  {
189  fn to_tokens(&self, tokens: &mut proc_macro2 ::TokenStream) 
190  {
191   use crate ::quote ::TokenStreamExt;
192   tokens.append_all(self.0.iter());
193 }
194 }
195
196  impl< T > syn ::parse ::Parse for Many< T >
197  where
198  T: Element + syn ::parse ::Parse + AsMuchAsPossibleNoDelimiter,
199  {
200  fn parse(input: syn ::parse ::ParseStream< '_ >) -> syn ::Result< Self > 
201  {
202   let mut items = vec![];
203   while !input.is_empty() 
204   {
205  let item: T = input.parse()?;
206  items.push(item);
207 }
208   Ok(Self(items))
209 }
210 }
211
212  //   impl WhileDelimiter for AttributesOuter
213  //   {
214  //     type Peek = syn ::token ::Pound;
215  //     type Delimiter = syn ::token ::Pound;
216  // }
217}
218
219#[ doc( inline ) ]
220#[ allow( unused_imports ) ]
221pub use own :: *;
222
223/// Own namespace of the module.
224#[ allow( unused_imports ) ]
225pub mod own 
226{
227
228  use super :: *;
229  #[ doc( inline ) ]
230  pub use orphan :: *;
231}
232
233/// Orphan namespace of the module.
234#[ allow( unused_imports ) ]
235pub mod orphan 
236{
237
238  use super :: *;
239  #[ doc( inline ) ]
240  pub use exposed :: *;
241}
242
243/// Exposed namespace of the module.
244#[ allow( unused_imports ) ]
245pub mod exposed 
246{
247
248  use super :: *;
249
250  pub use super ::super ::quantifier;
251  // pub use super ::own as quantifier;
252
253  #[ doc( inline ) ]
254  pub use prelude :: *;
255  #[ doc( inline ) ]
256  pub use private :: { AsMuchAsPossibleNoDelimiter, Pair, Many };
257}
258
259/// Prelude to use essentials: `use my_module ::prelude :: *`.
260#[ allow( unused_imports ) ]
261pub mod prelude 
262{
263
264  use super :: *;
265  #[ doc( inline ) ]
266  pub use private :: { };
267}