David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 1 | //! 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 Tolnay | c8b0e0f | 2019-03-07 22:46:32 -0800 | [diff] [blame] | 7 | //! - The bounds on a generic parameter are `Punctuated<TypeParamBound, |
| 8 | //! Token![+]>`. |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 9 | //! - 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 Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 23 | #[cfg(feature = "extra-traits")] |
| 24 | use std::fmt::{self, Debug}; |
David Tolnay | 6c0a609 | 2018-03-31 22:47:39 +0200 | [diff] [blame] | 25 | #[cfg(any(feature = "full", feature = "derive"))] |
| 26 | use std::iter; |
| 27 | use std::iter::FromIterator; |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 28 | use std::ops::{Index, IndexMut}; |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 29 | use std::option; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 30 | use std::slice; |
| 31 | use std::vec; |
| 32 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 33 | #[cfg(feature = "parsing")] |
David Tolnay | ecf0fbc | 2018-08-30 18:28:33 -0700 | [diff] [blame] | 34 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 35 | #[cfg(any(feature = "full", feature = "derive"))] |
| 36 | use private; |
David Tolnay | 52619f6 | 2018-08-31 09:30:01 -0700 | [diff] [blame] | 37 | #[cfg(feature = "parsing")] |
| 38 | use token::Token; |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 39 | |
| 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 Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 46 | #[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 47 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 48 | pub struct Punctuated<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 49 | inner: Vec<(T, P)>, |
| 50 | last: Option<Box<T>>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 51 | } |
| 52 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 53 | impl<T, P> Punctuated<T, P> { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 54 | /// Creates an empty punctuated sequence. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 55 | pub fn new() -> Punctuated<T, P> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 56 | Punctuated { |
| 57 | inner: Vec::new(), |
| 58 | last: None, |
| 59 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 60 | } |
| 61 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 62 | /// Determines whether this punctuated sequence is empty, meaning it |
| 63 | /// contains no syntax tree nodes or punctuation. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 64 | pub fn is_empty(&self) -> bool { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 65 | self.inner.len() == 0 && self.last.is_none() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 66 | } |
| 67 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 68 | /// 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 Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 72 | pub fn len(&self) -> usize { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 73 | self.inner.len() + if self.last.is_some() { 1 } else { 0 } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 74 | } |
| 75 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 76 | /// Borrows the first punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 77 | pub fn first(&self) -> Option<Pair<&T, &P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 78 | self.pairs().next() |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 79 | } |
| 80 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 81 | /// Borrows the last punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 82 | pub fn last(&self) -> Option<Pair<&T, &P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 83 | if self.last.is_some() { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 84 | self.last.as_ref().map(|t| Pair::End(t.as_ref())) |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 85 | } else { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 86 | self.inner |
| 87 | .last() |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 88 | .map(|&(ref t, ref d)| Pair::Punctuated(t, d)) |
| 89 | } |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 90 | } |
| 91 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 92 | /// Mutably borrows the last punctuated pair in this sequence. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 93 | pub fn last_mut(&mut self) -> Option<Pair<&mut T, &mut P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 94 | if self.last.is_some() { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 95 | self.last.as_mut().map(|t| Pair::End(t.as_mut())) |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 96 | } else { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 97 | self.inner |
| 98 | .last_mut() |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 99 | .map(|&mut (ref mut t, ref mut d)| Pair::Punctuated(t, d)) |
| 100 | } |
Alex Crichton | 0aa50e0 | 2017-07-07 20:59:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 103 | /// Returns an iterator over borrowed syntax tree nodes of type `&T`. |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 104 | pub fn iter(&self) -> Iter<T> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 105 | Iter { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 106 | inner: Box::new(PrivateIter { |
| 107 | inner: self.inner.iter(), |
David Tolnay | 124e50b | 2019-02-28 23:53:36 -0800 | [diff] [blame] | 108 | last: self.last.as_ref().map(Box::as_ref).into_iter(), |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 109 | }), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 110 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 111 | } |
| 112 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 113 | /// Returns an iterator over mutably borrowed syntax tree nodes of type |
| 114 | /// `&mut T`. |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 115 | pub fn iter_mut(&mut self) -> IterMut<T> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 116 | IterMut { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 117 | inner: Box::new(PrivateIterMut { |
| 118 | inner: self.inner.iter_mut(), |
David Tolnay | 124e50b | 2019-02-28 23:53:36 -0800 | [diff] [blame] | 119 | last: self.last.as_mut().map(Box::as_mut).into_iter(), |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 120 | }), |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 124 | /// Returns an iterator over the contents of this sequence as borrowed |
| 125 | /// punctuated pairs. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 126 | pub fn pairs(&self) -> Pairs<T, P> { |
| 127 | Pairs { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 128 | inner: self.inner.iter(), |
David Tolnay | 124e50b | 2019-02-28 23:53:36 -0800 | [diff] [blame] | 129 | last: self.last.as_ref().map(Box::as_ref).into_iter(), |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 133 | /// Returns an iterator over the contents of this sequence as mutably |
| 134 | /// borrowed punctuated pairs. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 135 | pub fn pairs_mut(&mut self) -> PairsMut<T, P> { |
| 136 | PairsMut { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 137 | inner: self.inner.iter_mut(), |
David Tolnay | 124e50b | 2019-02-28 23:53:36 -0800 | [diff] [blame] | 138 | last: self.last.as_mut().map(Box::as_mut).into_iter(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 139 | } |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 140 | } |
| 141 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 142 | /// Returns an iterator over the contents of this sequence as owned |
| 143 | /// punctuated pairs. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 144 | pub fn into_pairs(self) -> IntoPairs<T, P> { |
| 145 | IntoPairs { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 146 | inner: self.inner.into_iter(), |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 147 | last: self.last.map(|t| *t).into_iter(), |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 151 | /// 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 Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 163 | pub fn push_value(&mut self, value: T) { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 164 | assert!(self.empty_or_trailing()); |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 165 | self.last = Some(Box::new(value)); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 166 | } |
| 167 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 168 | /// 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 Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 175 | pub fn push_punct(&mut self, punctuation: P) { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 176 | assert!(self.last.is_some()); |
David Tolnay | 0316308 | 2018-04-11 12:10:18 -0700 | [diff] [blame] | 177 | let last = self.last.take().unwrap(); |
| 178 | self.inner.push((*last, punctuation)); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 179 | } |
| 180 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 181 | /// Removes the last punctuated pair from this sequence, or `None` if the |
| 182 | /// sequence is empty. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 183 | pub fn pop(&mut self) -> Option<Pair<T, P>> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 184 | if self.last.is_some() { |
David Tolnay | 0316308 | 2018-04-11 12:10:18 -0700 | [diff] [blame] | 185 | self.last.take().map(|t| Pair::End(*t)) |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 186 | } else { |
| 187 | self.inner.pop().map(|(t, d)| Pair::Punctuated(t, d)) |
| 188 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 189 | } |
| 190 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 191 | /// Determines whether this punctuated sequence ends with a trailing |
| 192 | /// punctuation. |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 193 | pub fn trailing_punct(&self) -> bool { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 194 | self.last.is_none() && !self.is_empty() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 195 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 196 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 197 | /// Returns true if either this `Punctuated` is empty, or it has a trailing |
| 198 | /// punctuation. |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 199 | /// |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 200 | /// Equivalent to `punctuated.is_empty() || punctuated.trailing_punct()`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 201 | pub fn empty_or_trailing(&self) -> bool { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 202 | self.last.is_none() |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 203 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 204 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 205 | /// 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 Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 210 | pub fn push(&mut self, value: T) |
| 211 | where |
| 212 | P: Default, |
| 213 | { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 214 | if !self.empty_or_trailing() { |
| 215 | self.push_punct(Default::default()); |
| 216 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 217 | self.push_value(value); |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 218 | } |
David Tolnay | b77d180 | 2018-01-11 16:18:35 -0800 | [diff] [blame] | 219 | |
| 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 Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 226 | pub fn insert(&mut self, index: usize, value: T) |
| 227 | where |
| 228 | P: Default, |
| 229 | { |
David Tolnay | b77d180 | 2018-01-11 16:18:35 -0800 | [diff] [blame] | 230 | assert!(index <= self.len()); |
| 231 | |
| 232 | if index == self.len() { |
| 233 | self.push(value); |
| 234 | } else { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 235 | self.inner.insert(index, (value, Default::default())); |
David Tolnay | b77d180 | 2018-01-11 16:18:35 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 238 | |
David Tolnay | 0f2a4ac | 2018-09-01 12:44:25 -0700 | [diff] [blame] | 239 | /// 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 Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 244 | /// |
| 245 | /// *This function is available if Syn is built with the `"parsing"` |
| 246 | /// feature.* |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 247 | #[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 Tolnay | 0f2a4ac | 2018-09-01 12:44:25 -0700 | [diff] [blame] | 256 | /// 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 Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 264 | /// |
| 265 | /// *This function is available if Syn is built with the `"parsing"` |
| 266 | /// feature.* |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 267 | #[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 Tolnay | 0f2a4ac | 2018-09-01 12:44:25 -0700 | [diff] [blame] | 293 | /// 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 Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 300 | /// |
| 301 | /// *This function is available if Syn is built with the `"parsing"` |
| 302 | /// feature.* |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 303 | #[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 Tolnay | 0f2a4ac | 2018-09-01 12:44:25 -0700 | [diff] [blame] | 312 | /// 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 Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 320 | /// |
| 321 | /// *This function is available if Syn is built with the `"parsing"` |
| 322 | /// feature.* |
David Tolnay | 8842c7e | 2018-09-01 12:37:32 -0700 | [diff] [blame] | 323 | #[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 Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 347 | #[cfg(feature = "extra-traits")] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 348 | impl<T: Debug, P: Debug> Debug for Punctuated<T, P> { |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 349 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 350 | let mut list = f.debug_list(); |
David Tolnay | 0c18c51 | 2018-07-22 10:39:10 -0700 | [diff] [blame] | 351 | 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ściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 357 | } |
| 358 | list.finish() |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
David Tolnay | 9ef24bc | 2018-01-09 10:43:55 -0800 | [diff] [blame] | 362 | impl<T, P> FromIterator<T> for Punctuated<T, P> |
| 363 | where |
| 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 | |
| 373 | impl<T, P> Extend<T> for Punctuated<T, P> |
| 374 | where |
| 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 Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 384 | impl<T, P> FromIterator<Pair<T, P>> for Punctuated<T, P> { |
| 385 | fn from_iter<I: IntoIterator<Item = Pair<T, P>>>(i: I) -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 386 | let mut ret = Punctuated::new(); |
Alex Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 387 | ret.extend(i); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 388 | ret |
| 389 | } |
| 390 | } |
| 391 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 392 | impl<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ściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 394 | assert!(self.empty_or_trailing()); |
| 395 | let mut nomore = false; |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 396 | for pair in i { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 397 | if nomore { |
| 398 | panic!("Punctuated extended with items after a Pair::End"); |
| 399 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 400 | match pair { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 401 | 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 Crichton | 24f1282 | 2017-07-14 07:15:32 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 411 | impl<T, P> IntoIterator for Punctuated<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 412 | type Item = T; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 413 | type IntoIter = IntoIter<T, P>; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 414 | |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 415 | fn into_iter(self) -> Self::IntoIter { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 416 | IntoIter { |
| 417 | inner: self.inner.into_iter(), |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 418 | last: self.last.map(|t| *t).into_iter(), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 419 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 423 | impl<'a, T, P> IntoIterator for &'a Punctuated<T, P> { |
| 424 | type Item = &'a T; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 425 | type IntoIter = Iter<'a, T>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 426 | |
| 427 | fn into_iter(self) -> Self::IntoIter { |
| 428 | Punctuated::iter(self) |
| 429 | } |
| 430 | } |
| 431 | |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 432 | impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P> { |
| 433 | type Item = &'a mut T; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 434 | type IntoIter = IterMut<'a, T>; |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 435 | |
| 436 | fn into_iter(self) -> Self::IntoIter { |
| 437 | Punctuated::iter_mut(self) |
| 438 | } |
| 439 | } |
| 440 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 441 | impl<T, P> Default for Punctuated<T, P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 442 | fn default() -> Self { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 443 | Punctuated::new() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 447 | /// 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 Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 452 | pub struct Pairs<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 453 | inner: slice::Iter<'a, (T, P)>, |
| 454 | last: option::IntoIter<&'a T>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 455 | } |
| 456 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 457 | impl<'a, T, P> Iterator for Pairs<'a, T, P> { |
| 458 | type Item = Pair<&'a T, &'a P>; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 459 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 460 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 461 | self.inner |
| 462 | .next() |
| 463 | .map(|&(ref t, ref p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame] | 464 | .or_else(|| self.last.next().map(Pair::End)) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 468 | impl<'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 Tolnay | 7f3f13e | 2019-04-22 10:55:30 -0700 | [diff] [blame^] | 474 | // No Clone bound on T or P. |
| 475 | impl<'a, T, P> Clone for Pairs<'a, T, P> { |
| 476 | fn clone(&self) -> Self { |
| 477 | Pairs { |
| 478 | inner: self.inner.clone(), |
| 479 | last: self.last.clone(), |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 484 | /// An iterator over mutably borrowed pairs of type `Pair<&mut T, &mut P>`. |
| 485 | /// |
| 486 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 487 | /// |
| 488 | /// [module documentation]: index.html |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 489 | pub struct PairsMut<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 490 | inner: slice::IterMut<'a, (T, P)>, |
| 491 | last: option::IntoIter<&'a mut T>, |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 492 | } |
| 493 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 494 | impl<'a, T, P> Iterator for PairsMut<'a, T, P> { |
| 495 | type Item = Pair<&'a mut T, &'a mut P>; |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 496 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 497 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 498 | self.inner |
| 499 | .next() |
| 500 | .map(|&mut (ref mut t, ref mut p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame] | 501 | .or_else(|| self.last.next().map(Pair::End)) |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 505 | impl<'a, T, P> ExactSizeIterator for PairsMut<'a, T, P> { |
| 506 | fn len(&self) -> usize { |
| 507 | self.inner.len() + self.last.len() |
| 508 | } |
| 509 | } |
| 510 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 511 | /// An iterator over owned pairs of type `Pair<T, P>`. |
| 512 | /// |
| 513 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 514 | /// |
| 515 | /// [module documentation]: index.html |
David Tolnay | 7f3f13e | 2019-04-22 10:55:30 -0700 | [diff] [blame^] | 516 | #[derive(Clone)] |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 517 | pub struct IntoPairs<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 518 | inner: vec::IntoIter<(T, P)>, |
| 519 | last: option::IntoIter<T>, |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 520 | } |
| 521 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 522 | impl<T, P> Iterator for IntoPairs<T, P> { |
| 523 | type Item = Pair<T, P>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 524 | |
| 525 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 526 | self.inner |
| 527 | .next() |
| 528 | .map(|(t, p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame] | 529 | .or_else(|| self.last.next().map(Pair::End)) |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 533 | impl<T, P> ExactSizeIterator for IntoPairs<T, P> { |
| 534 | fn len(&self) -> usize { |
| 535 | self.inner.len() + self.last.len() |
| 536 | } |
| 537 | } |
| 538 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 539 | /// An iterator over owned values of type `T`. |
| 540 | /// |
| 541 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 542 | /// |
| 543 | /// [module documentation]: index.html |
David Tolnay | 7f3f13e | 2019-04-22 10:55:30 -0700 | [diff] [blame^] | 544 | #[derive(Clone)] |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 545 | pub struct IntoIter<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 546 | inner: vec::IntoIter<(T, P)>, |
| 547 | last: option::IntoIter<T>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 548 | } |
| 549 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 550 | impl<T, P> Iterator for IntoIter<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 551 | type Item = T; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 552 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 553 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 554 | self.inner |
| 555 | .next() |
| 556 | .map(|pair| pair.0) |
| 557 | .or_else(|| self.last.next()) |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 561 | impl<T, P> ExactSizeIterator for IntoIter<T, P> { |
| 562 | fn len(&self) -> usize { |
| 563 | self.inner.len() + self.last.len() |
| 564 | } |
| 565 | } |
| 566 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 567 | /// An iterator over borrowed values of type `&T`. |
| 568 | /// |
| 569 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 570 | /// |
| 571 | /// [module documentation]: index.html |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 572 | pub struct Iter<'a, T: 'a> { |
David Tolnay | 7f3f13e | 2019-04-22 10:55:30 -0700 | [diff] [blame^] | 573 | // The `Item = &'a T` needs to be specified to support rustc 1.31 and older. |
| 574 | // On modern compilers we would be able to write just IterTrait<'a, T> where |
| 575 | // Item can be inferred unambiguously from the supertrait. |
| 576 | inner: Box<IterTrait<'a, T, Item = &'a T> + 'a>, |
| 577 | } |
| 578 | |
| 579 | trait IterTrait<'a, T: 'a>: ExactSizeIterator<Item = &'a T> { |
| 580 | fn clone_box(&self) -> Box<IterTrait<'a, T, Item = &'a T> + 'a>; |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | struct PrivateIter<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 584 | inner: slice::Iter<'a, (T, P)>, |
| 585 | last: option::IntoIter<&'a T>, |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 586 | } |
| 587 | |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 588 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 589 | impl private { |
| 590 | pub fn empty_punctuated_iter<'a, T>() -> Iter<'a, T> { |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 591 | Iter { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 592 | inner: Box::new(iter::empty()), |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
David Tolnay | 7f3f13e | 2019-04-22 10:55:30 -0700 | [diff] [blame^] | 597 | // No Clone bound on T. |
| 598 | impl<'a, T> Clone for Iter<'a, T> { |
| 599 | fn clone(&self) -> Self { |
| 600 | Iter { |
| 601 | inner: self.inner.clone_box(), |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 606 | impl<'a, T> Iterator for Iter<'a, T> { |
| 607 | type Item = &'a T; |
| 608 | |
| 609 | fn next(&mut self) -> Option<Self::Item> { |
| 610 | self.inner.next() |
| 611 | } |
| 612 | } |
| 613 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 614 | impl<'a, T> ExactSizeIterator for Iter<'a, T> { |
| 615 | fn len(&self) -> usize { |
| 616 | self.inner.len() |
| 617 | } |
| 618 | } |
| 619 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 620 | impl<'a, T, P> Iterator for PrivateIter<'a, T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 621 | type Item = &'a T; |
| 622 | |
| 623 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 624 | self.inner |
| 625 | .next() |
| 626 | .map(|pair| &pair.0) |
| 627 | .or_else(|| self.last.next()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 631 | impl<'a, T, P> ExactSizeIterator for PrivateIter<'a, T, P> { |
| 632 | fn len(&self) -> usize { |
| 633 | self.inner.len() + self.last.len() |
| 634 | } |
| 635 | } |
| 636 | |
David Tolnay | 7f3f13e | 2019-04-22 10:55:30 -0700 | [diff] [blame^] | 637 | // No Clone bound on T or P. |
| 638 | impl<'a, T, P> Clone for PrivateIter<'a, T, P> { |
| 639 | fn clone(&self) -> Self { |
| 640 | PrivateIter { |
| 641 | inner: self.inner.clone(), |
| 642 | last: self.last.clone(), |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | impl<'a, T: 'a, I: 'a> IterTrait<'a, T> for I |
| 648 | where |
| 649 | I: ExactSizeIterator<Item = &'a T> + Clone, |
| 650 | { |
| 651 | fn clone_box(&self) -> Box<IterTrait<'a, T, Item = &'a T> + 'a> { |
| 652 | Box::new(self.clone()) |
| 653 | } |
| 654 | } |
| 655 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 656 | /// An iterator over mutably borrowed values of type `&mut T`. |
| 657 | /// |
| 658 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 659 | /// |
| 660 | /// [module documentation]: index.html |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 661 | pub struct IterMut<'a, T: 'a> { |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 662 | inner: Box<ExactSizeIterator<Item = &'a mut T> + 'a>, |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | struct PrivateIterMut<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 666 | inner: slice::IterMut<'a, (T, P)>, |
| 667 | last: option::IntoIter<&'a mut T>, |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Michael Bradshaw | 0b13ae6 | 2018-08-02 23:43:15 -0600 | [diff] [blame] | 670 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 671 | impl private { |
| 672 | pub fn empty_punctuated_iter_mut<'a, T>() -> IterMut<'a, T> { |
Michael Bradshaw | 0b13ae6 | 2018-08-02 23:43:15 -0600 | [diff] [blame] | 673 | IterMut { |
| 674 | inner: Box::new(iter::empty()), |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 679 | impl<'a, T> Iterator for IterMut<'a, T> { |
| 680 | type Item = &'a mut T; |
| 681 | |
| 682 | fn next(&mut self) -> Option<Self::Item> { |
| 683 | self.inner.next() |
| 684 | } |
| 685 | } |
| 686 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 687 | impl<'a, T> ExactSizeIterator for IterMut<'a, T> { |
| 688 | fn len(&self) -> usize { |
| 689 | self.inner.len() |
| 690 | } |
| 691 | } |
| 692 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 693 | impl<'a, T, P> Iterator for PrivateIterMut<'a, T, P> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 694 | type Item = &'a mut T; |
| 695 | |
| 696 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 697 | self.inner |
| 698 | .next() |
| 699 | .map(|pair| &mut pair.0) |
| 700 | .or_else(|| self.last.next()) |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 704 | impl<'a, T, P> ExactSizeIterator for PrivateIterMut<'a, T, P> { |
| 705 | fn len(&self) -> usize { |
| 706 | self.inner.len() + self.last.len() |
| 707 | } |
| 708 | } |
| 709 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 710 | /// A single syntax tree node of type `T` followed by its trailing punctuation |
| 711 | /// of type `P` if any. |
| 712 | /// |
| 713 | /// Refer to the [module documentation] for details about punctuated sequences. |
| 714 | /// |
| 715 | /// [module documentation]: index.html |
David Tolnay | 7f3f13e | 2019-04-22 10:55:30 -0700 | [diff] [blame^] | 716 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 717 | pub enum Pair<T, P> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 718 | Punctuated(T, P), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 719 | End(T), |
| 720 | } |
| 721 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 722 | impl<T, P> Pair<T, P> { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 723 | /// Extracts the syntax tree node from this punctuated pair, discarding the |
| 724 | /// following punctuation. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 725 | pub fn into_value(self) -> T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 726 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 727 | Pair::Punctuated(t, _) | Pair::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 731 | /// Borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 732 | pub fn value(&self) -> &T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 733 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 734 | Pair::Punctuated(ref t, _) | Pair::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 738 | /// Mutably borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 739 | pub fn value_mut(&mut self) -> &mut T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 740 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 741 | Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 745 | /// Borrows the punctuation from this punctuated pair, unless this pair is |
| 746 | /// the final one and there is no trailing punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 747 | pub fn punct(&self) -> Option<&P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 748 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 749 | Pair::Punctuated(_, ref d) => Some(d), |
| 750 | Pair::End(_) => None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 753 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 754 | /// Creates a punctuated pair out of a syntax tree node and an optional |
| 755 | /// following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 756 | pub fn new(t: T, d: Option<P>) -> Self { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 757 | match d { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 758 | Some(d) => Pair::Punctuated(t, d), |
| 759 | None => Pair::End(t), |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 763 | /// Produces this punctuated pair as a tuple of syntax tree node and |
| 764 | /// optional following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 765 | pub fn into_tuple(self) -> (T, Option<P>) { |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 766 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 767 | Pair::Punctuated(t, d) => (t, Some(d)), |
| 768 | Pair::End(t) => (t, None), |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 769 | } |
| 770 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 771 | } |
| 772 | |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 773 | impl<T, P> Index<usize> for Punctuated<T, P> { |
| 774 | type Output = T; |
| 775 | |
| 776 | fn index(&self, index: usize) -> &Self::Output { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 777 | if index == self.len() - 1 { |
| 778 | match self.last { |
| 779 | Some(ref t) => t, |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 780 | None => &self.inner[index].0, |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 781 | } |
| 782 | } else { |
| 783 | &self.inner[index].0 |
| 784 | } |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | impl<T, P> IndexMut<usize> for Punctuated<T, P> { |
| 789 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 790 | if index == self.len() - 1 { |
| 791 | match self.last { |
| 792 | Some(ref mut t) => t, |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 793 | None => &mut self.inner[index].0, |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 794 | } |
| 795 | } else { |
| 796 | &mut self.inner[index].0 |
| 797 | } |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 801 | #[cfg(feature = "printing")] |
| 802 | mod printing { |
| 803 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 804 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 805 | use quote::{ToTokens, TokenStreamExt}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 806 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 807 | impl<T, P> ToTokens for Punctuated<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 808 | where |
| 809 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 810 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 811 | { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 812 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 813 | tokens.append_all(self.pairs()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 817 | impl<T, P> ToTokens for Pair<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 818 | where |
| 819 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 820 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 821 | { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 822 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 823 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 824 | Pair::Punctuated(ref a, ref b) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 825 | a.to_tokens(tokens); |
| 826 | b.to_tokens(tokens); |
| 827 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 828 | Pair::End(ref a) => a.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | } |
| 832 | } |