David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1 | use {Generics, Ident, LifetimeDef, TyParam, WhereClause, WherePredicate}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 2 | use aster::invoke::{Identity, Invoke}; |
| 3 | use aster::lifetime::{IntoLifetime, LifetimeDefBuilder, IntoLifetimeDef}; |
| 4 | use aster::path::IntoPath; |
| 5 | use aster::ty_param::TyParamBuilder; |
| 6 | use aster::where_predicate::WherePredicateBuilder; |
| 7 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 8 | use delimited::Delimited; |
| 9 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 10 | pub struct GenericsBuilder<F = Identity> { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 11 | callback: F, |
| 12 | lifetimes: Vec<LifetimeDef>, |
| 13 | ty_params: Vec<TyParam>, |
| 14 | predicates: Vec<WherePredicate>, |
| 15 | } |
| 16 | |
| 17 | impl GenericsBuilder { |
| 18 | pub fn new() -> Self { |
| 19 | GenericsBuilder::with_callback(Identity) |
| 20 | } |
| 21 | |
| 22 | pub fn from_generics(generics: Generics) -> Self { |
| 23 | GenericsBuilder::from_generics_with_callback(generics, Identity) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl<F> GenericsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 28 | where F: Invoke<Generics> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 29 | { |
| 30 | pub fn with_callback(callback: F) -> Self { |
| 31 | GenericsBuilder { |
| 32 | callback: callback, |
| 33 | lifetimes: Vec::new(), |
| 34 | ty_params: Vec::new(), |
| 35 | predicates: Vec::new(), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn from_generics_with_callback(generics: Generics, callback: F) -> Self { |
| 40 | GenericsBuilder { |
| 41 | callback: callback, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 42 | lifetimes: generics.lifetimes.into_vec(), |
| 43 | ty_params: generics.ty_params.into_vec(), |
| 44 | predicates: generics.where_clause.predicates.into_vec(), |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | pub fn with(self, generics: Generics) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 49 | self.with_lifetimes(generics.lifetimes.into_vec().into_iter()) |
| 50 | .with_ty_params(generics.ty_params.into_vec().into_iter()) |
| 51 | .with_predicates(generics.where_clause.predicates.into_vec().into_iter()) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | pub fn with_lifetimes<I, L>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 55 | where I: IntoIterator<Item = L>, |
| 56 | L: IntoLifetimeDef |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 57 | { |
| 58 | let iter = iter.into_iter().map(|lifetime_def| lifetime_def.into_lifetime_def()); |
| 59 | self.lifetimes.extend(iter); |
| 60 | self |
| 61 | } |
| 62 | |
| 63 | pub fn with_lifetime_names<I, N>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 64 | where I: IntoIterator<Item = N>, |
| 65 | N: Into<Ident> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 66 | { |
| 67 | for name in iter { |
| 68 | self = self.lifetime_name(name); |
| 69 | } |
| 70 | self |
| 71 | } |
| 72 | |
| 73 | pub fn with_lifetime(mut self, lifetime: LifetimeDef) -> Self { |
| 74 | self.lifetimes.push(lifetime); |
| 75 | self |
| 76 | } |
| 77 | |
| 78 | pub fn lifetime_name<N>(self, name: N) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 79 | where N: Into<Ident> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 80 | { |
| 81 | self.lifetime(name).build() |
| 82 | } |
| 83 | |
| 84 | pub fn lifetime<N>(self, name: N) -> LifetimeDefBuilder<Self> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 85 | where N: Into<Ident> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 86 | { |
| 87 | LifetimeDefBuilder::with_callback(name, self) |
| 88 | } |
| 89 | |
| 90 | pub fn with_ty_params<I>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 91 | where I: IntoIterator<Item = TyParam> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 92 | { |
| 93 | self.ty_params.extend(iter); |
| 94 | self |
| 95 | } |
| 96 | |
| 97 | pub fn with_ty_param_ids<I, T>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 98 | where I: IntoIterator<Item = T>, |
| 99 | T: Into<Ident> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 100 | { |
| 101 | for id in iter { |
| 102 | self = self.ty_param_id(id); |
| 103 | } |
| 104 | self |
| 105 | } |
| 106 | |
| 107 | pub fn with_ty_param(mut self, ty_param: TyParam) -> Self { |
| 108 | self.ty_params.push(ty_param); |
| 109 | self |
| 110 | } |
| 111 | |
| 112 | pub fn ty_param_id<I>(self, id: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 113 | where I: Into<Ident> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 114 | { |
| 115 | self.ty_param(id).build() |
| 116 | } |
| 117 | |
| 118 | pub fn ty_param<I>(self, id: I) -> TyParamBuilder<Self> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 119 | where I: Into<Ident> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 120 | { |
| 121 | TyParamBuilder::with_callback(id, self) |
| 122 | } |
| 123 | |
| 124 | pub fn with_predicates<I>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 125 | where I: IntoIterator<Item = WherePredicate> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 126 | { |
| 127 | self.predicates.extend(iter); |
| 128 | self |
| 129 | } |
| 130 | |
| 131 | pub fn with_predicate(mut self, predicate: WherePredicate) -> Self { |
| 132 | self.predicates.push(predicate); |
| 133 | self |
| 134 | } |
| 135 | |
| 136 | pub fn predicate(self) -> WherePredicateBuilder<Self> { |
| 137 | WherePredicateBuilder::with_callback(self) |
| 138 | } |
| 139 | |
| 140 | pub fn add_lifetime_bound<L>(mut self, lifetime: L) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 141 | where L: IntoLifetime |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 142 | { |
| 143 | let lifetime = lifetime.into_lifetime(); |
| 144 | |
| 145 | for lifetime_def in &mut self.lifetimes { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 146 | lifetime_def.bounds.push_default(lifetime.clone()); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | for ty_param in &mut self.ty_params { |
| 150 | *ty_param = TyParamBuilder::from_ty_param(ty_param.clone()) |
| 151 | .lifetime_bound(lifetime.clone()) |
| 152 | .build(); |
| 153 | } |
| 154 | |
| 155 | self |
| 156 | } |
| 157 | |
| 158 | pub fn add_ty_param_bound<P>(mut self, path: P) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 159 | where P: IntoPath |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 160 | { |
| 161 | let path = path.into_path(); |
| 162 | |
| 163 | for ty_param in &mut self.ty_params { |
| 164 | *ty_param = TyParamBuilder::from_ty_param(ty_param.clone()) |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 165 | .trait_bound(path.clone()) |
| 166 | .build() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 167 | .build(); |
| 168 | } |
| 169 | |
| 170 | self |
| 171 | } |
| 172 | |
| 173 | pub fn strip_bounds(self) -> Self { |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 174 | self.strip_lifetimes().strip_ty_params().strip_predicates() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | pub fn strip_lifetimes(mut self) -> Self { |
| 178 | for lifetime in &mut self.lifetimes { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 179 | lifetime.bounds = Delimited::new(); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 180 | } |
| 181 | self |
| 182 | } |
| 183 | |
| 184 | pub fn strip_ty_params(mut self) -> Self { |
| 185 | for ty_param in &mut self.ty_params { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 186 | ty_param.bounds = Delimited::new(); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 187 | } |
| 188 | self |
| 189 | } |
| 190 | |
| 191 | pub fn strip_predicates(mut self) -> Self { |
| 192 | self.predicates = vec![]; |
| 193 | self |
| 194 | } |
| 195 | |
| 196 | pub fn build(self) -> F::Result { |
| 197 | self.callback.invoke(Generics { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 198 | lifetimes: self.lifetimes.into(), |
| 199 | ty_params: self.ty_params.into(), |
| 200 | where_clause: WhereClause { |
| 201 | predicates: self.predicates.into(), |
| 202 | where_token: Default::default(), |
| 203 | }, |
| 204 | lt_token: Default::default(), |
| 205 | gt_token: Default::default(), |
| 206 | }) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | impl<F> Invoke<LifetimeDef> for GenericsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 211 | where F: Invoke<Generics> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 212 | { |
| 213 | type Result = Self; |
| 214 | |
| 215 | fn invoke(self, lifetime: LifetimeDef) -> Self { |
| 216 | self.with_lifetime(lifetime) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | impl<F> Invoke<TyParam> for GenericsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 221 | where F: Invoke<Generics> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 222 | { |
| 223 | type Result = Self; |
| 224 | |
| 225 | fn invoke(self, ty_param: TyParam) -> Self { |
| 226 | self.with_ty_param(ty_param) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | impl<F> Invoke<WherePredicate> for GenericsBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 231 | where F: Invoke<Generics> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 232 | { |
| 233 | type Result = Self; |
| 234 | |
| 235 | fn invoke(self, predicate: WherePredicate) -> Self { |
| 236 | self.with_predicate(predicate) |
| 237 | } |
| 238 | } |