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