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 | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 474 | /// 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 Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 479 | pub struct PairsMut<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 480 | inner: slice::IterMut<'a, (T, P)>, |
| 481 | last: option::IntoIter<&'a mut T>, |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 482 | } |
| 483 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 484 | impl<'a, T, P> Iterator for PairsMut<'a, T, P> { |
| 485 | type Item = Pair<&'a mut T, &'a mut P>; |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 486 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 487 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 488 | self.inner |
| 489 | .next() |
| 490 | .map(|&mut (ref mut t, ref mut p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame] | 491 | .or_else(|| self.last.next().map(Pair::End)) |
Alex Crichton | 164c533 | 2017-07-06 13:18:34 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 495 | impl<'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 Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 501 | /// 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 Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 506 | pub struct IntoPairs<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 507 | inner: vec::IntoIter<(T, P)>, |
| 508 | last: option::IntoIter<T>, |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 509 | } |
| 510 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 511 | impl<T, P> Iterator for IntoPairs<T, P> { |
| 512 | type Item = Pair<T, P>; |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 513 | |
| 514 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 515 | self.inner |
| 516 | .next() |
| 517 | .map(|(t, p)| Pair::Punctuated(t, p)) |
David Tolnay | 6e4a9c2 | 2018-04-11 12:20:08 -0700 | [diff] [blame] | 518 | .or_else(|| self.last.next().map(Pair::End)) |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 522 | impl<T, P> ExactSizeIterator for IntoPairs<T, P> { |
| 523 | fn len(&self) -> usize { |
| 524 | self.inner.len() + self.last.len() |
| 525 | } |
| 526 | } |
| 527 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 528 | /// 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 Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 533 | pub struct IntoIter<T, P> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 534 | inner: vec::IntoIter<(T, P)>, |
| 535 | last: option::IntoIter<T>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 536 | } |
| 537 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 538 | impl<T, P> Iterator for IntoIter<T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 539 | type Item = T; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 540 | |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 541 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 542 | self.inner |
| 543 | .next() |
| 544 | .map(|pair| pair.0) |
| 545 | .or_else(|| self.last.next()) |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 549 | impl<T, P> ExactSizeIterator for IntoIter<T, P> { |
| 550 | fn len(&self) -> usize { |
| 551 | self.inner.len() + self.last.len() |
| 552 | } |
| 553 | } |
| 554 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 555 | /// 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 Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 560 | pub struct Iter<'a, T: 'a> { |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 561 | inner: Box<ExactSizeIterator<Item = &'a T> + 'a>, |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | struct PrivateIter<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 565 | inner: slice::Iter<'a, (T, P)>, |
| 566 | last: option::IntoIter<&'a T>, |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 567 | } |
| 568 | |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 569 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 570 | impl private { |
| 571 | pub fn empty_punctuated_iter<'a, T>() -> Iter<'a, T> { |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 572 | Iter { |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 573 | inner: Box::new(iter::empty()), |
David Tolnay | 96a09d9 | 2018-01-16 22:24:03 -0800 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 578 | impl<'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 Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 586 | impl<'a, T> ExactSizeIterator for Iter<'a, T> { |
| 587 | fn len(&self) -> usize { |
| 588 | self.inner.len() |
| 589 | } |
| 590 | } |
| 591 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 592 | impl<'a, T, P> Iterator for PrivateIter<'a, T, P> { |
David Tolnay | 6eff4da | 2018-01-01 20:27:45 -0800 | [diff] [blame] | 593 | type Item = &'a T; |
| 594 | |
| 595 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 596 | self.inner |
| 597 | .next() |
| 598 | .map(|pair| &pair.0) |
| 599 | .or_else(|| self.last.next()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 603 | impl<'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 Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 609 | /// 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 Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 614 | pub struct IterMut<'a, T: 'a> { |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 615 | inner: Box<ExactSizeIterator<Item = &'a mut T> + 'a>, |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | struct PrivateIterMut<'a, T: 'a, P: 'a> { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 619 | inner: slice::IterMut<'a, (T, P)>, |
| 620 | last: option::IntoIter<&'a mut T>, |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Michael Bradshaw | 0b13ae6 | 2018-08-02 23:43:15 -0600 | [diff] [blame] | 623 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 624 | impl private { |
| 625 | pub fn empty_punctuated_iter_mut<'a, T>() -> IterMut<'a, T> { |
Michael Bradshaw | 0b13ae6 | 2018-08-02 23:43:15 -0600 | [diff] [blame] | 626 | IterMut { |
| 627 | inner: Box::new(iter::empty()), |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 632 | impl<'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 Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 640 | impl<'a, T> ExactSizeIterator for IterMut<'a, T> { |
| 641 | fn len(&self) -> usize { |
| 642 | self.inner.len() |
| 643 | } |
| 644 | } |
| 645 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 646 | impl<'a, T, P> Iterator for PrivateIterMut<'a, T, P> { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 647 | type Item = &'a mut T; |
| 648 | |
| 649 | fn next(&mut self) -> Option<Self::Item> { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 650 | self.inner |
| 651 | .next() |
| 652 | .map(|pair| &mut pair.0) |
| 653 | .or_else(|| self.last.next()) |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
David Tolnay | 9700be0 | 2018-04-30 00:51:15 -0700 | [diff] [blame] | 657 | impl<'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 Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 663 | /// 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 Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 669 | pub enum Pair<T, P> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 670 | Punctuated(T, P), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 671 | End(T), |
| 672 | } |
| 673 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 674 | impl<T, P> Pair<T, P> { |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 675 | /// Extracts the syntax tree node from this punctuated pair, discarding the |
| 676 | /// following punctuation. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 677 | pub fn into_value(self) -> T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 678 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 679 | Pair::Punctuated(t, _) | Pair::End(t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 683 | /// Borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 684 | pub fn value(&self) -> &T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 685 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 686 | Pair::Punctuated(ref t, _) | Pair::End(ref t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 690 | /// Mutably borrows the syntax tree node from this punctuated pair. |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 691 | pub fn value_mut(&mut self) -> &mut T { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 692 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 693 | Pair::Punctuated(ref mut t, _) | Pair::End(ref mut t) => t, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 697 | /// Borrows the punctuation from this punctuated pair, unless this pair is |
| 698 | /// the final one and there is no trailing punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 699 | pub fn punct(&self) -> Option<&P> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 700 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 701 | Pair::Punctuated(_, ref d) => Some(d), |
| 702 | Pair::End(_) => None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 705 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 706 | /// Creates a punctuated pair out of a syntax tree node and an optional |
| 707 | /// following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 708 | pub fn new(t: T, d: Option<P>) -> Self { |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 709 | match d { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 710 | Some(d) => Pair::Punctuated(t, d), |
| 711 | None => Pair::End(t), |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
David Tolnay | f319801 | 2018-01-06 20:00:42 -0800 | [diff] [blame] | 715 | /// Produces this punctuated pair as a tuple of syntax tree node and |
| 716 | /// optional following punctuation. |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 717 | pub fn into_tuple(self) -> (T, Option<P>) { |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 718 | match self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 719 | Pair::Punctuated(t, d) => (t, Some(d)), |
| 720 | Pair::End(t) => (t, None), |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 721 | } |
| 722 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 723 | } |
| 724 | |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 725 | impl<T, P> Index<usize> for Punctuated<T, P> { |
| 726 | type Output = T; |
| 727 | |
| 728 | fn index(&self, index: usize) -> &Self::Output { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 729 | if index == self.len() - 1 { |
| 730 | match self.last { |
| 731 | Some(ref t) => t, |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 732 | None => &self.inner[index].0, |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 733 | } |
| 734 | } else { |
| 735 | &self.inner[index].0 |
| 736 | } |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
| 740 | impl<T, P> IndexMut<usize> for Punctuated<T, P> { |
| 741 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 742 | if index == self.len() - 1 { |
| 743 | match self.last { |
| 744 | Some(ref mut t) => t, |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 745 | None => &mut self.inner[index].0, |
Mateusz Naściszewski | 1411120 | 2018-04-11 21:17:41 +0200 | [diff] [blame] | 746 | } |
| 747 | } else { |
| 748 | &mut self.inner[index].0 |
| 749 | } |
David Tolnay | bb98713 | 2018-01-08 13:51:19 -0800 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 753 | #[cfg(feature = "printing")] |
| 754 | mod printing { |
| 755 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 756 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 757 | use quote::{ToTokens, TokenStreamExt}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 758 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 759 | impl<T, P> ToTokens for Punctuated<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 760 | where |
| 761 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 762 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 763 | { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 764 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 765 | tokens.append_all(self.pairs()) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 769 | impl<T, P> ToTokens for Pair<T, P> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 770 | where |
| 771 | T: ToTokens, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 772 | P: ToTokens, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 773 | { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 774 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 775 | match *self { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 776 | Pair::Punctuated(ref a, ref b) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 777 | a.to_tokens(tokens); |
| 778 | b.to_tokens(tokens); |
| 779 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 780 | Pair::End(ref a) => a.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |