blob: 6b29176b320991cb6a2ce9b7237b98435b8a5dc5 [file] [log] [blame]
David Tolnayf3198012018-01-06 20:00:42 -08001//! A punctuated sequence of syntax tree nodes separated by punctuation.
2//!
3//! Lots of things in Rust are punctuated sequences.
4//!
5//! - The fields of a struct are `Punctuated<Field, Token![,]>`.
6//! - The segments of a path are `Punctuated<PathSegment, Token![::]>`.
David Tolnayc8b0e0f2019-03-07 22:46:32 -08007//! - The bounds on a generic parameter are `Punctuated<TypeParamBound,
8//! Token![+]>`.
David Tolnayf3198012018-01-06 20:00:42 -08009//! - The arguments to a function call are `Punctuated<Expr, Token![,]>`.
10//!
11//! This module provides a common representation for these punctuated sequences
12//! in the form of the [`Punctuated<T, P>`] type. We store a vector of pairs of
13//! syntax tree node + punctuation, where every node in the sequence is followed
14//! by punctuation except for possibly the final one.
15//!
16//! [`Punctuated<T, P>`]: struct.Punctuated.html
17//!
18//! ```text
19//! a_function_call(arg1, arg2, arg3);
20//! ^^^^^ ~~~~~ ^^^^
21//! ```
22
David Tolnay94d2b792018-04-29 12:26:10 -070023#[cfg(feature = "extra-traits")]
24use std::fmt::{self, Debug};
David Tolnay6c0a6092018-03-31 22:47:39 +020025#[cfg(any(feature = "full", feature = "derive"))]
26use std::iter;
27use std::iter::FromIterator;
David Tolnaybb987132018-01-08 13:51:19 -080028use std::ops::{Index, IndexMut};
Mateusz Naściszewski14111202018-04-11 21:17:41 +020029use std::option;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070030use std::slice;
31use std::vec;
32
David Tolnayf3198012018-01-06 20:00:42 -080033#[cfg(feature = "parsing")]
David Tolnayecf0fbc2018-08-30 18:28:33 -070034use parse::{Parse, ParseStream, Result};
David Tolnay94f06632018-08-31 10:17:17 -070035#[cfg(any(feature = "full", feature = "derive"))]
36use private;
David Tolnay52619f62018-08-31 09:30:01 -070037#[cfg(feature = "parsing")]
38use token::Token;
David Tolnayf3198012018-01-06 20:00:42 -080039
40/// A punctuated sequence of syntax tree nodes of type `T` separated by
41/// punctuation of type `P`.
42///
43/// Refer to the [module documentation] for details about punctuated sequences.
44///
45/// [module documentation]: index.html
Nika Layzelld73a3652017-10-24 08:57:05 -040046#[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070047#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayf2cfd722017-12-31 18:02:51 -050048pub struct Punctuated<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020049 inner: Vec<(T, P)>,
50 last: Option<Box<T>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070051}
52
David Tolnayf2cfd722017-12-31 18:02:51 -050053impl<T, P> Punctuated<T, P> {
David Tolnayf3198012018-01-06 20:00:42 -080054 /// Creates an empty punctuated sequence.
David Tolnayf2cfd722017-12-31 18:02:51 -050055 pub fn new() -> Punctuated<T, P> {
David Tolnay94d2b792018-04-29 12:26:10 -070056 Punctuated {
57 inner: Vec::new(),
58 last: None,
59 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070060 }
61
David Tolnayf3198012018-01-06 20:00:42 -080062 /// Determines whether this punctuated sequence is empty, meaning it
63 /// contains no syntax tree nodes or punctuation.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070064 pub fn is_empty(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020065 self.inner.len() == 0 && self.last.is_none()
Alex Crichtonccbb45d2017-05-23 10:58:24 -070066 }
67
David Tolnayf3198012018-01-06 20:00:42 -080068 /// Returns the number of syntax tree nodes in this punctuated sequence.
69 ///
70 /// This is the number of nodes of type `T`, not counting the punctuation of
71 /// type `P`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -070072 pub fn len(&self) -> usize {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020073 self.inner.len() + if self.last.is_some() { 1 } else { 0 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070074 }
75
David Tolnayf3198012018-01-06 20:00:42 -080076 /// Borrows the first punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080077 pub fn first(&self) -> Option<Pair<&T, &P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020078 self.pairs().next()
Alex Crichton0aa50e02017-07-07 20:59:03 -070079 }
80
David Tolnayf3198012018-01-06 20:00:42 -080081 /// Borrows the last punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080082 pub fn last(&self) -> Option<Pair<&T, &P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020083 if self.last.is_some() {
David Tolnay94d2b792018-04-29 12:26:10 -070084 self.last.as_ref().map(|t| Pair::End(t.as_ref()))
Mateusz Naściszewski14111202018-04-11 21:17:41 +020085 } else {
David Tolnay94d2b792018-04-29 12:26:10 -070086 self.inner
87 .last()
Mateusz Naściszewski14111202018-04-11 21:17:41 +020088 .map(|&(ref t, ref d)| Pair::Punctuated(t, d))
89 }
Alex Crichton0aa50e02017-07-07 20:59:03 -070090 }
91
David Tolnayf3198012018-01-06 20:00:42 -080092 /// Mutably borrows the last punctuated pair in this sequence.
David Tolnay56080682018-01-06 14:01:52 -080093 pub fn last_mut(&mut self) -> Option<Pair<&mut T, &mut P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +020094 if self.last.is_some() {
David Tolnay94d2b792018-04-29 12:26:10 -070095 self.last.as_mut().map(|t| Pair::End(t.as_mut()))
Mateusz Naściszewski14111202018-04-11 21:17:41 +020096 } else {
David Tolnay94d2b792018-04-29 12:26:10 -070097 self.inner
98 .last_mut()
Mateusz Naściszewski14111202018-04-11 21:17:41 +020099 .map(|&mut (ref mut t, ref mut d)| Pair::Punctuated(t, d))
100 }
Alex Crichton0aa50e02017-07-07 20:59:03 -0700101 }
102
David Tolnayf3198012018-01-06 20:00:42 -0800103 /// Returns an iterator over borrowed syntax tree nodes of type `&T`.
David Tolnay8095c302018-03-31 19:34:17 +0200104 pub fn iter(&self) -> Iter<T> {
David Tolnay51382052017-12-27 13:46:21 -0500105 Iter {
David Tolnay8095c302018-03-31 19:34:17 +0200106 inner: Box::new(PrivateIter {
107 inner: self.inner.iter(),
David Tolnay124e50b2019-02-28 23:53:36 -0800108 last: self.last.as_ref().map(Box::as_ref).into_iter(),
David Tolnay8095c302018-03-31 19:34:17 +0200109 }),
David Tolnay51382052017-12-27 13:46:21 -0500110 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700111 }
112
David Tolnayf3198012018-01-06 20:00:42 -0800113 /// Returns an iterator over mutably borrowed syntax tree nodes of type
114 /// `&mut T`.
David Tolnay8095c302018-03-31 19:34:17 +0200115 pub fn iter_mut(&mut self) -> IterMut<T> {
David Tolnaya0834b42018-01-01 21:30:02 -0800116 IterMut {
David Tolnay8095c302018-03-31 19:34:17 +0200117 inner: Box::new(PrivateIterMut {
118 inner: self.inner.iter_mut(),
David Tolnay124e50b2019-02-28 23:53:36 -0800119 last: self.last.as_mut().map(Box::as_mut).into_iter(),
David Tolnay8095c302018-03-31 19:34:17 +0200120 }),
David Tolnaya0834b42018-01-01 21:30:02 -0800121 }
122 }
123
David Tolnayf3198012018-01-06 20:00:42 -0800124 /// Returns an iterator over the contents of this sequence as borrowed
125 /// punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800126 pub fn pairs(&self) -> Pairs<T, P> {
127 Pairs {
David Tolnay6eff4da2018-01-01 20:27:45 -0800128 inner: self.inner.iter(),
David Tolnay124e50b2019-02-28 23:53:36 -0800129 last: self.last.as_ref().map(Box::as_ref).into_iter(),
David Tolnay6eff4da2018-01-01 20:27:45 -0800130 }
131 }
132
David Tolnayf3198012018-01-06 20:00:42 -0800133 /// Returns an iterator over the contents of this sequence as mutably
134 /// borrowed punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800135 pub fn pairs_mut(&mut self) -> PairsMut<T, P> {
136 PairsMut {
David Tolnay51382052017-12-27 13:46:21 -0500137 inner: self.inner.iter_mut(),
David Tolnay124e50b2019-02-28 23:53:36 -0800138 last: self.last.as_mut().map(Box::as_mut).into_iter(),
David Tolnay51382052017-12-27 13:46:21 -0500139 }
Alex Crichton164c5332017-07-06 13:18:34 -0700140 }
141
David Tolnayf3198012018-01-06 20:00:42 -0800142 /// Returns an iterator over the contents of this sequence as owned
143 /// punctuated pairs.
David Tolnay56080682018-01-06 14:01:52 -0800144 pub fn into_pairs(self) -> IntoPairs<T, P> {
145 IntoPairs {
David Tolnay6eff4da2018-01-01 20:27:45 -0800146 inner: self.inner.into_iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200147 last: self.last.map(|t| *t).into_iter(),
David Tolnay6eff4da2018-01-01 20:27:45 -0800148 }
149 }
150
David Tolnayf3198012018-01-06 20:00:42 -0800151 /// Appends a syntax tree node onto the end of this punctuated sequence. The
152 /// sequence must previously have a trailing punctuation.
153 ///
154 /// Use [`push`] instead if the punctuated sequence may or may not already
155 /// have trailing punctuation.
156 ///
157 /// [`push`]: #method.push
158 ///
159 /// # Panics
160 ///
161 /// Panics if the sequence does not already have a trailing punctuation when
162 /// this method is called.
David Tolnay56080682018-01-06 14:01:52 -0800163 pub fn push_value(&mut self, value: T) {
David Tolnaydc03aec2017-12-30 01:54:18 -0500164 assert!(self.empty_or_trailing());
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200165 self.last = Some(Box::new(value));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700166 }
167
David Tolnayf3198012018-01-06 20:00:42 -0800168 /// Appends a trailing punctuation onto the end of this punctuated sequence.
169 /// The sequence must be non-empty and must not already have trailing
170 /// punctuation.
171 ///
172 /// # Panics
173 ///
174 /// Panics if the sequence is empty or already has a trailing punctuation.
David Tolnaya0834b42018-01-01 21:30:02 -0800175 pub fn push_punct(&mut self, punctuation: P) {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200176 assert!(self.last.is_some());
David Tolnay03163082018-04-11 12:10:18 -0700177 let last = self.last.take().unwrap();
178 self.inner.push((*last, punctuation));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700179 }
180
David Tolnayf3198012018-01-06 20:00:42 -0800181 /// Removes the last punctuated pair from this sequence, or `None` if the
182 /// sequence is empty.
David Tolnay56080682018-01-06 14:01:52 -0800183 pub fn pop(&mut self) -> Option<Pair<T, P>> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200184 if self.last.is_some() {
David Tolnay03163082018-04-11 12:10:18 -0700185 self.last.take().map(|t| Pair::End(*t))
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200186 } else {
187 self.inner.pop().map(|(t, d)| Pair::Punctuated(t, d))
188 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700189 }
190
David Tolnayf3198012018-01-06 20:00:42 -0800191 /// Determines whether this punctuated sequence ends with a trailing
192 /// punctuation.
David Tolnaya0834b42018-01-01 21:30:02 -0800193 pub fn trailing_punct(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200194 self.last.is_none() && !self.is_empty()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700195 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400196
David Tolnayf2cfd722017-12-31 18:02:51 -0500197 /// Returns true if either this `Punctuated` is empty, or it has a trailing
198 /// punctuation.
David Tolnaydc03aec2017-12-30 01:54:18 -0500199 ///
David Tolnaya0834b42018-01-01 21:30:02 -0800200 /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`.
Michael Layzell3936ceb2017-07-08 00:28:36 -0400201 pub fn empty_or_trailing(&self) -> bool {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200202 self.last.is_none()
Michael Layzell3936ceb2017-07-08 00:28:36 -0400203 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700204
David Tolnayf3198012018-01-06 20:00:42 -0800205 /// Appends a syntax tree node onto the end of this punctuated sequence.
206 ///
207 /// If there is not a trailing punctuation in this sequence when this method
208 /// is called, the default value of punctuation type `P` is inserted before
209 /// the given value of type `T`.
David Tolnay8842c7e2018-09-01 12:37:32 -0700210 pub fn push(&mut self, value: T)
211 where
212 P: Default,
213 {
David Tolnaya0834b42018-01-01 21:30:02 -0800214 if !self.empty_or_trailing() {
215 self.push_punct(Default::default());
216 }
David Tolnay56080682018-01-06 14:01:52 -0800217 self.push_value(value);
David Tolnaya0834b42018-01-01 21:30:02 -0800218 }
David Tolnayb77d1802018-01-11 16:18:35 -0800219
220 /// Inserts an element at position `index`.
221 ///
222 /// # Panics
223 ///
224 /// Panics if `index` is greater than the number of elements previously in
225 /// this punctuated sequence.
David Tolnay8842c7e2018-09-01 12:37:32 -0700226 pub fn insert(&mut self, index: usize, value: T)
227 where
228 P: Default,
229 {
David Tolnayb77d1802018-01-11 16:18:35 -0800230 assert!(index <= self.len());
231
232 if index == self.len() {
233 self.push(value);
234 } else {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200235 self.inner.insert(index, (value, Default::default()));
David Tolnayb77d1802018-01-11 16:18:35 -0800236 }
237 }
David Tolnay8842c7e2018-09-01 12:37:32 -0700238
David Tolnay0f2a4ac2018-09-01 12:44:25 -0700239 /// Parses zero or more occurrences of `T` separated by punctuation of type
240 /// `P`, with optional trailing punctuation.
241 ///
242 /// Parsing continues until the end of this parse stream. The entire content
243 /// of this parse stream must consist of `T` and `P`.
David Tolnay206edfb2018-09-01 16:02:20 -0700244 ///
245 /// *This function is available if Syn is built with the `"parsing"`
246 /// feature.*
David Tolnay8842c7e2018-09-01 12:37:32 -0700247 #[cfg(feature = "parsing")]
248 pub fn parse_terminated(input: ParseStream) -> Result<Self>
249 where
250 T: Parse,
251 P: Parse,
252 {
253 Self::parse_terminated_with(input, T::parse)
254 }
255
David Tolnay0f2a4ac2018-09-01 12:44:25 -0700256 /// Parses zero or more occurrences of `T` using the given parse function,
257 /// separated by punctuation of type `P`, with optional trailing
258 /// punctuation.
259 ///
260 /// Like [`parse_terminated`], the entire content of this stream is expected
261 /// to be parsed.
262 ///
263 /// [`parse_terminated`]: #method.parse_terminated
David Tolnay206edfb2018-09-01 16:02:20 -0700264 ///
265 /// *This function is available if Syn is built with the `"parsing"`
266 /// feature.*
David Tolnay8842c7e2018-09-01 12:37:32 -0700267 #[cfg(feature = "parsing")]
268 pub fn parse_terminated_with(
269 input: ParseStream,
270 parser: fn(ParseStream) -> Result<T>,
271 ) -> Result<Self>
272 where
273 P: Parse,
274 {
275 let mut punctuated = Punctuated::new();
276
277 loop {
278 if input.is_empty() {
279 break;
280 }
281 let value = parser(input)?;
282 punctuated.push_value(value);
283 if input.is_empty() {
284 break;
285 }
286 let punct = input.parse()?;
287 punctuated.push_punct(punct);
288 }
289
290 Ok(punctuated)
291 }
292
David Tolnay0f2a4ac2018-09-01 12:44:25 -0700293 /// Parses one or more occurrences of `T` separated by punctuation of type
294 /// `P`, not accepting trailing punctuation.
295 ///
296 /// Parsing continues as long as punctuation `P` is present at the head of
297 /// the stream. This method returns upon parsing a `T` and observing that it
298 /// is not followed by a `P`, even if there are remaining tokens in the
299 /// stream.
David Tolnay206edfb2018-09-01 16:02:20 -0700300 ///
301 /// *This function is available if Syn is built with the `"parsing"`
302 /// feature.*
David Tolnay8842c7e2018-09-01 12:37:32 -0700303 #[cfg(feature = "parsing")]
304 pub fn parse_separated_nonempty(input: ParseStream) -> Result<Self>
305 where
306 T: Parse,
307 P: Token + Parse,
308 {
309 Self::parse_separated_nonempty_with(input, T::parse)
310 }
311
David Tolnay0f2a4ac2018-09-01 12:44:25 -0700312 /// Parses one or more occurrences of `T` using the given parse function,
313 /// separated by punctuation of type `P`, not accepting trailing
314 /// punctuation.
315 ///
316 /// Like [`parse_separated_nonempty`], may complete early without parsing
317 /// the entire content of this stream.
318 ///
319 /// [`parse_separated_nonempty`]: #method.parse_separated_nonempty
David Tolnay206edfb2018-09-01 16:02:20 -0700320 ///
321 /// *This function is available if Syn is built with the `"parsing"`
322 /// feature.*
David Tolnay8842c7e2018-09-01 12:37:32 -0700323 #[cfg(feature = "parsing")]
324 pub fn parse_separated_nonempty_with(
325 input: ParseStream,
326 parser: fn(ParseStream) -> Result<T>,
327 ) -> Result<Self>
328 where
329 P: Token + Parse,
330 {
331 let mut punctuated = Punctuated::new();
332
333 loop {
334 let value = parser(input)?;
335 punctuated.push_value(value);
336 if !P::peek(input.cursor()) {
337 break;
338 }
339 let punct = input.parse()?;
340 punctuated.push_punct(punct);
341 }
342
343 Ok(punctuated)
344 }
David Tolnaya0834b42018-01-01 21:30:02 -0800345}
346
Nika Layzelld73a3652017-10-24 08:57:05 -0400347#[cfg(feature = "extra-traits")]
David Tolnayf2cfd722017-12-31 18:02:51 -0500348impl<T: Debug, P: Debug> Debug for Punctuated<T, P> {
Nika Layzelld73a3652017-10-24 08:57:05 -0400349 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200350 let mut list = f.debug_list();
David Tolnay0c18c512018-07-22 10:39:10 -0700351 for &(ref t, ref p) in &self.inner {
352 list.entry(t);
353 list.entry(p);
354 }
355 if let Some(ref last) = self.last {
356 list.entry(last);
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200357 }
358 list.finish()
Nika Layzelld73a3652017-10-24 08:57:05 -0400359 }
360}
361
David Tolnay9ef24bc2018-01-09 10:43:55 -0800362impl<T, P> FromIterator<T> for Punctuated<T, P>
363where
364 P: Default,
365{
366 fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self {
367 let mut ret = Punctuated::new();
368 ret.extend(i);
369 ret
370 }
371}
372
373impl<T, P> Extend<T> for Punctuated<T, P>
374where
375 P: Default,
376{
377 fn extend<I: IntoIterator<Item = T>>(&mut self, i: I) {
378 for value in i {
379 self.push(value);
380 }
381 }
382}
383
David Tolnay56080682018-01-06 14:01:52 -0800384impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> {
385 fn from_iter<I: IntoIterator<Item = Pair<T, P>>>(i: I) -> Self {
David Tolnayf2cfd722017-12-31 18:02:51 -0500386 let mut ret = Punctuated::new();
Alex Crichton24f12822017-07-14 07:15:32 -0700387 ret.extend(i);
Alex Crichton954046c2017-05-30 21:49:42 -0700388 ret
389 }
390}
391
David Tolnay56080682018-01-06 14:01:52 -0800392impl<T, P> Extend<Pair<T, P>> for Punctuated<T, P> {
393 fn extend<I: IntoIterator<Item = Pair<T, P>>>(&mut self, i: I) {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200394 assert!(self.empty_or_trailing());
395 let mut nomore = false;
David Tolnay56080682018-01-06 14:01:52 -0800396 for pair in i {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200397 if nomore {
398 panic!("Punctuated extended with items after a Pair::End");
399 }
David Tolnay56080682018-01-06 14:01:52 -0800400 match pair {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200401 Pair::Punctuated(a, b) => self.inner.push((a, b)),
402 Pair::End(a) => {
403 self.last = Some(Box::new(a));
404 nomore = true;
405 }
Alex Crichton24f12822017-07-14 07:15:32 -0700406 }
407 }
408 }
409}
410
David Tolnayf2cfd722017-12-31 18:02:51 -0500411impl<T, P> IntoIterator for Punctuated<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800412 type Item = T;
David Tolnayf2cfd722017-12-31 18:02:51 -0500413 type IntoIter = IntoIter<T, P>;
Alex Crichton954046c2017-05-30 21:49:42 -0700414
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500415 fn into_iter(self) -> Self::IntoIter {
David Tolnay51382052017-12-27 13:46:21 -0500416 IntoIter {
417 inner: self.inner.into_iter(),
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200418 last: self.last.map(|t| *t).into_iter(),
David Tolnay51382052017-12-27 13:46:21 -0500419 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700420 }
421}
422
David Tolnay6eff4da2018-01-01 20:27:45 -0800423impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> {
424 type Item = &'a T;
David Tolnay8095c302018-03-31 19:34:17 +0200425 type IntoIter = Iter<'a, T>;
David Tolnay6eff4da2018-01-01 20:27:45 -0800426
427 fn into_iter(self) -> Self::IntoIter {
428 Punctuated::iter(self)
429 }
430}
431
David Tolnaya0834b42018-01-01 21:30:02 -0800432impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> {
433 type Item = &'a mut T;
David Tolnay8095c302018-03-31 19:34:17 +0200434 type IntoIter = IterMut<'a, T>;
David Tolnaya0834b42018-01-01 21:30:02 -0800435
436 fn into_iter(self) -> Self::IntoIter {
437 Punctuated::iter_mut(self)
438 }
439}
440
David Tolnayf2cfd722017-12-31 18:02:51 -0500441impl<T, P> Default for Punctuated<T, P> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700442 fn default() -> Self {
David Tolnayf2cfd722017-12-31 18:02:51 -0500443 Punctuated::new()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700444 }
445}
446
David Tolnayf3198012018-01-06 20:00:42 -0800447/// An iterator over borrowed pairs of type `Pair<&T, &P>`.
448///
449/// Refer to the [module documentation] for details about punctuated sequences.
450///
451/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800452pub struct Pairs<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200453 inner: slice::Iter<'a, (T, P)>,
454 last: option::IntoIter<&'a T>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700455}
456
David Tolnay56080682018-01-06 14:01:52 -0800457impl<'a, T, P> Iterator for Pairs<'a, T, P> {
458 type Item = Pair<&'a T, &'a P>;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700459
David Tolnay6eff4da2018-01-01 20:27:45 -0800460 fn next(&mut self) -> Option<Self::Item> {
David Tolnay94d2b792018-04-29 12:26:10 -0700461 self.inner
462 .next()
463 .map(|&(ref t, ref p)| Pair::Punctuated(t, p))
David Tolnay6e4a9c22018-04-11 12:20:08 -0700464 .or_else(|| self.last.next().map(Pair::End))
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700465 }
466}
467
David Tolnay9700be02018-04-30 00:51:15 -0700468impl<'a, T, P> ExactSizeIterator for Pairs<'a, T, P> {
469 fn len(&self) -> usize {
470 self.inner.len() + self.last.len()
471 }
472}
473
David Tolnayf3198012018-01-06 20:00:42 -0800474/// An iterator over mutably borrowed pairs of type `Pair<&mut T, &mut P>`.
475///
476/// Refer to the [module documentation] for details about punctuated sequences.
477///
478/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800479pub struct PairsMut<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200480 inner: slice::IterMut<'a, (T, P)>,
481 last: option::IntoIter<&'a mut T>,
Alex Crichton164c5332017-07-06 13:18:34 -0700482}
483
David Tolnay56080682018-01-06 14:01:52 -0800484impl<'a, T, P> Iterator for PairsMut<'a, T, P> {
485 type Item = Pair<&'a mut T, &'a mut P>;
Alex Crichton164c5332017-07-06 13:18:34 -0700486
David Tolnay6eff4da2018-01-01 20:27:45 -0800487 fn next(&mut self) -> Option<Self::Item> {
David Tolnay94d2b792018-04-29 12:26:10 -0700488 self.inner
489 .next()
490 .map(|&mut (ref mut t, ref mut p)| Pair::Punctuated(t, p))
David Tolnay6e4a9c22018-04-11 12:20:08 -0700491 .or_else(|| self.last.next().map(Pair::End))
Alex Crichton164c5332017-07-06 13:18:34 -0700492 }
493}
494
David Tolnay9700be02018-04-30 00:51:15 -0700495impl<'a, T, P> ExactSizeIterator for PairsMut<'a, T, P> {
496 fn len(&self) -> usize {
497 self.inner.len() + self.last.len()
498 }
499}
500
David Tolnayf3198012018-01-06 20:00:42 -0800501/// An iterator over owned pairs of type `Pair<T, P>`.
502///
503/// Refer to the [module documentation] for details about punctuated sequences.
504///
505/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800506pub struct IntoPairs<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200507 inner: vec::IntoIter<(T, P)>,
508 last: option::IntoIter<T>,
David Tolnay6eff4da2018-01-01 20:27:45 -0800509}
510
David Tolnay56080682018-01-06 14:01:52 -0800511impl<T, P> Iterator for IntoPairs<T, P> {
512 type Item = Pair<T, P>;
David Tolnay6eff4da2018-01-01 20:27:45 -0800513
514 fn next(&mut self) -> Option<Self::Item> {
David Tolnay94d2b792018-04-29 12:26:10 -0700515 self.inner
516 .next()
517 .map(|(t, p)| Pair::Punctuated(t, p))
David Tolnay6e4a9c22018-04-11 12:20:08 -0700518 .or_else(|| self.last.next().map(Pair::End))
David Tolnay6eff4da2018-01-01 20:27:45 -0800519 }
520}
521
David Tolnay9700be02018-04-30 00:51:15 -0700522impl<T, P> ExactSizeIterator for IntoPairs<T, P> {
523 fn len(&self) -> usize {
524 self.inner.len() + self.last.len()
525 }
526}
527
David Tolnayf3198012018-01-06 20:00:42 -0800528/// An iterator over owned values of type `T`.
529///
530/// Refer to the [module documentation] for details about punctuated sequences.
531///
532/// [module documentation]: index.html
David Tolnayf2cfd722017-12-31 18:02:51 -0500533pub struct IntoIter<T, P> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200534 inner: vec::IntoIter<(T, P)>,
535 last: option::IntoIter<T>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700536}
537
David Tolnayf2cfd722017-12-31 18:02:51 -0500538impl<T, P> Iterator for IntoIter<T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800539 type Item = T;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700540
David Tolnay6eff4da2018-01-01 20:27:45 -0800541 fn next(&mut self) -> Option<Self::Item> {
David Tolnay94d2b792018-04-29 12:26:10 -0700542 self.inner
543 .next()
544 .map(|pair| pair.0)
545 .or_else(|| self.last.next())
David Tolnay6eff4da2018-01-01 20:27:45 -0800546 }
547}
548
David Tolnay9700be02018-04-30 00:51:15 -0700549impl<T, P> ExactSizeIterator for IntoIter<T, P> {
550 fn len(&self) -> usize {
551 self.inner.len() + self.last.len()
552 }
553}
554
David Tolnayf3198012018-01-06 20:00:42 -0800555/// An iterator over borrowed values of type `&T`.
556///
557/// Refer to the [module documentation] for details about punctuated sequences.
558///
559/// [module documentation]: index.html
David Tolnay8095c302018-03-31 19:34:17 +0200560pub struct Iter<'a, T: 'a> {
David Tolnay9700be02018-04-30 00:51:15 -0700561 inner: Box<ExactSizeIterator<Item = &'a T> + 'a>,
David Tolnay8095c302018-03-31 19:34:17 +0200562}
563
564struct PrivateIter<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200565 inner: slice::Iter<'a, (T, P)>,
566 last: option::IntoIter<&'a T>,
David Tolnay6eff4da2018-01-01 20:27:45 -0800567}
568
David Tolnay96a09d92018-01-16 22:24:03 -0800569#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay10951d52018-08-31 10:27:39 -0700570impl private {
571 pub fn empty_punctuated_iter<'a, T>() -> Iter<'a, T> {
David Tolnay96a09d92018-01-16 22:24:03 -0800572 Iter {
David Tolnay8095c302018-03-31 19:34:17 +0200573 inner: Box::new(iter::empty()),
David Tolnay96a09d92018-01-16 22:24:03 -0800574 }
575 }
576}
577
David Tolnay8095c302018-03-31 19:34:17 +0200578impl<'a, T> Iterator for Iter<'a, T> {
579 type Item = &'a T;
580
581 fn next(&mut self) -> Option<Self::Item> {
582 self.inner.next()
583 }
584}
585
David Tolnay9700be02018-04-30 00:51:15 -0700586impl<'a, T> ExactSizeIterator for Iter<'a, T> {
587 fn len(&self) -> usize {
588 self.inner.len()
589 }
590}
591
David Tolnay8095c302018-03-31 19:34:17 +0200592impl<'a, T, P> Iterator for PrivateIter<'a, T, P> {
David Tolnay6eff4da2018-01-01 20:27:45 -0800593 type Item = &'a T;
594
595 fn next(&mut self) -> Option<Self::Item> {
David Tolnay94d2b792018-04-29 12:26:10 -0700596 self.inner
597 .next()
598 .map(|pair| &pair.0)
599 .or_else(|| self.last.next())
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700600 }
601}
602
David Tolnay9700be02018-04-30 00:51:15 -0700603impl<'a, T, P> ExactSizeIterator for PrivateIter<'a, T, P> {
604 fn len(&self) -> usize {
605 self.inner.len() + self.last.len()
606 }
607}
608
David Tolnayf3198012018-01-06 20:00:42 -0800609/// An iterator over mutably borrowed values of type `&mut T`.
610///
611/// Refer to the [module documentation] for details about punctuated sequences.
612///
613/// [module documentation]: index.html
David Tolnay8095c302018-03-31 19:34:17 +0200614pub struct IterMut<'a, T: 'a> {
David Tolnay9700be02018-04-30 00:51:15 -0700615 inner: Box<ExactSizeIterator<Item = &'a mut T> + 'a>,
David Tolnay8095c302018-03-31 19:34:17 +0200616}
617
618struct PrivateIterMut<'a, T: 'a, P: 'a> {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200619 inner: slice::IterMut<'a, (T, P)>,
620 last: option::IntoIter<&'a mut T>,
David Tolnaya0834b42018-01-01 21:30:02 -0800621}
622
Michael Bradshaw0b13ae62018-08-02 23:43:15 -0600623#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay10951d52018-08-31 10:27:39 -0700624impl private {
625 pub fn empty_punctuated_iter_mut<'a, T>() -> IterMut<'a, T> {
Michael Bradshaw0b13ae62018-08-02 23:43:15 -0600626 IterMut {
627 inner: Box::new(iter::empty()),
628 }
629 }
630}
631
David Tolnay8095c302018-03-31 19:34:17 +0200632impl<'a, T> Iterator for IterMut<'a, T> {
633 type Item = &'a mut T;
634
635 fn next(&mut self) -> Option<Self::Item> {
636 self.inner.next()
637 }
638}
639
David Tolnay9700be02018-04-30 00:51:15 -0700640impl<'a, T> ExactSizeIterator for IterMut<'a, T> {
641 fn len(&self) -> usize {
642 self.inner.len()
643 }
644}
645
David Tolnay8095c302018-03-31 19:34:17 +0200646impl<'a, T, P> Iterator for PrivateIterMut<'a, T, P> {
David Tolnaya0834b42018-01-01 21:30:02 -0800647 type Item = &'a mut T;
648
649 fn next(&mut self) -> Option<Self::Item> {
David Tolnay94d2b792018-04-29 12:26:10 -0700650 self.inner
651 .next()
652 .map(|pair| &mut pair.0)
653 .or_else(|| self.last.next())
David Tolnaya0834b42018-01-01 21:30:02 -0800654 }
655}
656
David Tolnay9700be02018-04-30 00:51:15 -0700657impl<'a, T, P> ExactSizeIterator for PrivateIterMut<'a, T, P> {
658 fn len(&self) -> usize {
659 self.inner.len() + self.last.len()
660 }
661}
662
David Tolnayf3198012018-01-06 20:00:42 -0800663/// A single syntax tree node of type `T` followed by its trailing punctuation
664/// of type `P` if any.
665///
666/// Refer to the [module documentation] for details about punctuated sequences.
667///
668/// [module documentation]: index.html
David Tolnay56080682018-01-06 14:01:52 -0800669pub enum Pair<T, P> {
David Tolnayf2cfd722017-12-31 18:02:51 -0500670 Punctuated(T, P),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700671 End(T),
672}
673
David Tolnay56080682018-01-06 14:01:52 -0800674impl<T, P> Pair<T, P> {
David Tolnayf3198012018-01-06 20:00:42 -0800675 /// Extracts the syntax tree node from this punctuated pair, discarding the
676 /// following punctuation.
David Tolnay56080682018-01-06 14:01:52 -0800677 pub fn into_value(self) -> T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700678 match self {
David Tolnay56080682018-01-06 14:01:52 -0800679 Pair::Punctuated(t, _) | Pair::End(t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700680 }
681 }
682
David Tolnayf3198012018-01-06 20:00:42 -0800683 /// Borrows the syntax tree node from this punctuated pair.
David Tolnay56080682018-01-06 14:01:52 -0800684 pub fn value(&self) -> &T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700685 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800686 Pair::Punctuated(ref t, _) | Pair::End(ref t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700687 }
688 }
689
David Tolnayf3198012018-01-06 20:00:42 -0800690 /// Mutably borrows the syntax tree node from this punctuated pair.
David Tolnay56080682018-01-06 14:01:52 -0800691 pub fn value_mut(&mut self) -> &mut T {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700692 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800693 Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700694 }
695 }
696
David Tolnayf3198012018-01-06 20:00:42 -0800697 /// Borrows the punctuation from this punctuated pair, unless this pair is
698 /// the final one and there is no trailing punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500699 pub fn punct(&self) -> Option<&P> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700700 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800701 Pair::Punctuated(_, ref d) => Some(d),
702 Pair::End(_) => None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700703 }
704 }
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400705
David Tolnayf3198012018-01-06 20:00:42 -0800706 /// Creates a punctuated pair out of a syntax tree node and an optional
707 /// following punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500708 pub fn new(t: T, d: Option<P>) -> Self {
David Tolnay660fd1f2017-12-31 01:52:57 -0500709 match d {
David Tolnay56080682018-01-06 14:01:52 -0800710 Some(d) => Pair::Punctuated(t, d),
711 None => Pair::End(t),
David Tolnay660fd1f2017-12-31 01:52:57 -0500712 }
713 }
714
David Tolnayf3198012018-01-06 20:00:42 -0800715 /// Produces this punctuated pair as a tuple of syntax tree node and
716 /// optional following punctuation.
David Tolnayf2cfd722017-12-31 18:02:51 -0500717 pub fn into_tuple(self) -> (T, Option<P>) {
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400718 match self {
David Tolnay56080682018-01-06 14:01:52 -0800719 Pair::Punctuated(t, d) => (t, Some(d)),
720 Pair::End(t) => (t, None),
Nika Layzellcda7ebd2017-10-24 23:10:44 -0400721 }
722 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700723}
724
David Tolnaybb987132018-01-08 13:51:19 -0800725impl<T, P> Index<usize> for Punctuated<T, P> {
726 type Output = T;
727
728 fn index(&self, index: usize) -> &Self::Output {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200729 if index == self.len() - 1 {
730 match self.last {
731 Some(ref t) => t,
David Tolnay94d2b792018-04-29 12:26:10 -0700732 None => &self.inner[index].0,
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200733 }
734 } else {
735 &self.inner[index].0
736 }
David Tolnaybb987132018-01-08 13:51:19 -0800737 }
738}
739
740impl<T, P> IndexMut<usize> for Punctuated<T, P> {
741 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200742 if index == self.len() - 1 {
743 match self.last {
744 Some(ref mut t) => t,
David Tolnay94d2b792018-04-29 12:26:10 -0700745 None => &mut self.inner[index].0,
Mateusz Naściszewski14111202018-04-11 21:17:41 +0200746 }
747 } else {
748 &mut self.inner[index].0
749 }
David Tolnaybb987132018-01-08 13:51:19 -0800750 }
751}
752
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700753#[cfg(feature = "printing")]
754mod printing {
755 use super::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700756 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -0700757 use quote::{ToTokens, TokenStreamExt};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700758
David Tolnayf2cfd722017-12-31 18:02:51 -0500759 impl<T, P> ToTokens for Punctuated<T, P>
David Tolnay51382052017-12-27 13:46:21 -0500760 where
761 T: ToTokens,
David Tolnayf2cfd722017-12-31 18:02:51 -0500762 P: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700763 {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700764 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay56080682018-01-06 14:01:52 -0800765 tokens.append_all(self.pairs())
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700766 }
767 }
768
David Tolnay56080682018-01-06 14:01:52 -0800769 impl<T, P> ToTokens for Pair<T, P>
David Tolnay51382052017-12-27 13:46:21 -0500770 where
771 T: ToTokens,
David Tolnayf2cfd722017-12-31 18:02:51 -0500772 P: ToTokens,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700773 {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700774 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700775 match *self {
David Tolnay56080682018-01-06 14:01:52 -0800776 Pair::Punctuated(ref a, ref b) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700777 a.to_tokens(tokens);
778 b.to_tokens(tokens);
779 }
David Tolnay56080682018-01-06 14:01:52 -0800780 Pair::End(ref a) => a.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700781 }
782 }
783 }
784}