| 1 | //! This crate removes some boilerplate for structs that simply delegate |
| 2 | //! some of their methods to one or more of their fields. |
| 3 | //! |
| 4 | //! It gives you the `delegate!` macro, which delegates method calls to selected expressions (usually inner fields). |
| 5 | //! |
| 6 | //! ## Features: |
| 7 | //! - Delegate to a method with a different name |
| 8 | //! ```rust |
| 9 | //! use delegate::delegate; |
| 10 | //! |
| 11 | //! struct Stack { inner: Vec<u32> } |
| 12 | //! impl Stack { |
| 13 | //! delegate! { |
| 14 | //! to self.inner { |
| 15 | //! #[call(push)] |
| 16 | //! pub fn add(&mut self, value: u32); |
| 17 | //! } |
| 18 | //! } |
| 19 | //! } |
| 20 | //! ``` |
| 21 | //! - Use an arbitrary inner field expression |
| 22 | //! ```rust |
| 23 | //! use delegate::delegate; |
| 24 | //! |
| 25 | //! use std::rc::Rc; |
| 26 | //! use std::cell::RefCell; |
| 27 | //! use std::ops::Deref; |
| 28 | //! |
| 29 | //! struct Wrapper { inner: Rc<RefCell<Vec<u32>>> } |
| 30 | //! impl Wrapper { |
| 31 | //! delegate! { |
| 32 | //! to self.inner.deref().borrow_mut() { |
| 33 | //! pub fn push(&mut self, val: u32); |
| 34 | //! } |
| 35 | //! } |
| 36 | //! } |
| 37 | //! ``` |
| 38 | //! |
| 39 | //! - Delegate to enum variants |
| 40 | //! |
| 41 | //! ```rust |
| 42 | //! use delegate::delegate; |
| 43 | //! |
| 44 | //! enum Enum { |
| 45 | //! A(A), |
| 46 | //! B(B), |
| 47 | //! C { v: C }, |
| 48 | //! } |
| 49 | //! |
| 50 | //! struct A { |
| 51 | //! val: usize, |
| 52 | //! } |
| 53 | //! |
| 54 | //! impl A { |
| 55 | //! fn dbg_inner(&self) -> usize { |
| 56 | //! dbg!(self.val); |
| 57 | //! 1 |
| 58 | //! } |
| 59 | //! } |
| 60 | //! struct B { |
| 61 | //! val_a: String, |
| 62 | //! } |
| 63 | //! |
| 64 | //! impl B { |
| 65 | //! fn dbg_inner(&self) -> usize { |
| 66 | //! dbg!(self.val_a.clone()); |
| 67 | //! 2 |
| 68 | //! } |
| 69 | //! } |
| 70 | //! |
| 71 | //! struct C { |
| 72 | //! val_c: f64, |
| 73 | //! } |
| 74 | //! |
| 75 | //! impl C { |
| 76 | //! fn dbg_inner(&self) -> usize { |
| 77 | //! dbg!(self.val_c); |
| 78 | //! 3 |
| 79 | //! } |
| 80 | //! } |
| 81 | //! |
| 82 | //! impl Enum { |
| 83 | //! delegate! { |
| 84 | //! // transformed to |
| 85 | //! // |
| 86 | //! // ```rust |
| 87 | //! // match self { |
| 88 | //! // Enum::A(a) => a.dbg_inner(), |
| 89 | //! // Enum::B(b) => { println!("i am b"); b }.dbg_inner(), |
| 90 | //! // Enum::C { v: c } => { c }.dbg_inner(), |
| 91 | //! // } |
| 92 | //! // ``` |
| 93 | //! to match self { |
| 94 | //! Enum::A(a) => a, |
| 95 | //! Enum::B(b) => { println!("i am b"); b }, |
| 96 | //! Enum::C { v: c } => { c }, |
| 97 | //! } { |
| 98 | //! fn dbg_inner(&self) -> usize; |
| 99 | //! } |
| 100 | //! } |
| 101 | //! } |
| 102 | //! ``` |
| 103 | //! |
| 104 | //! - Use modifiers that alter the generated method body |
| 105 | //! ```rust |
| 106 | //! use delegate::delegate; |
| 107 | //! struct Inner; |
| 108 | //! impl Inner { |
| 109 | //! pub fn method(&self, num: u32) -> u32 { num } |
| 110 | //! pub fn method_res(&self, num: u32) -> Result<u32, ()> { Ok(num) } |
| 111 | //! } |
| 112 | //! struct Wrapper { inner: Inner } |
| 113 | //! impl Wrapper { |
| 114 | //! delegate! { |
| 115 | //! to self.inner { |
| 116 | //! // calls method, converts result to u64 using `From` |
| 117 | //! #[into] |
| 118 | //! pub fn method(&self, num: u32) -> u64; |
| 119 | //! |
| 120 | //! // calls method, returns () |
| 121 | //! #[call(method)] |
| 122 | //! pub fn method_noreturn(&self, num: u32); |
| 123 | //! |
| 124 | //! // calls method, converts result to i6 using `TryFrom` |
| 125 | //! #[try_into] |
| 126 | //! #[call(method)] |
| 127 | //! pub fn method2(&self, num: u32) -> Result<u16, std::num::TryFromIntError>; |
| 128 | //! |
| 129 | //! // calls method_res, unwraps the result |
| 130 | //! #[unwrap] |
| 131 | //! pub fn method_res(&self, num: u32) -> u32; |
| 132 | //! |
| 133 | //! // calls method_res, unwraps the result, then calls into |
| 134 | //! #[unwrap] |
| 135 | //! #[into] |
| 136 | //! #[call(method_res)] |
| 137 | //! pub fn method_res_into(&self, num: u32) -> u64; |
| 138 | //! |
| 139 | //! // specify explicit type for into |
| 140 | //! #[into(u64)] |
| 141 | //! #[call(method)] |
| 142 | //! pub fn method_into_explicit(&self, num: u32) -> u64; |
| 143 | //! } |
| 144 | //! } |
| 145 | //! } |
| 146 | //! ``` |
| 147 | //! |
| 148 | //! - Custom called expression |
| 149 | //! |
| 150 | //! The `#[expr()]` attribute can be used to modify the delegated call. You can use the `$` sigil as a placeholder for what delegate would normally expand to, and wrap that expression with custom code. |
| 151 | //! |
| 152 | //! _Note:_ the `$` placeholder isn't required and can be present multiple times if you want. |
| 153 | //! |
| 154 | //! ```rs |
| 155 | //! struct A(Vec<u8>); |
| 156 | //! |
| 157 | //! impl A { |
| 158 | //! delegate! { |
| 159 | //! to self.0 { |
| 160 | //! #[expr(*$.unwrap())] |
| 161 | //! /// Here `$` == `self.0.get(idx)` |
| 162 | //! /// Will expand to `*self.0.get(idx).unwrap()` |
| 163 | //! fn get(&self, idx: usize) -> u8; |
| 164 | //! |
| 165 | //! #[call(get)] |
| 166 | //! #[expr($?.checked_pow(2))] |
| 167 | //! /// Here `$` == `self.0.get(idx)` |
| 168 | //! /// Will expand to `self.0.get(idx)?.checked_pow(2)` |
| 169 | //! fn get_checked_pow_2(&self, idx: usize) -> Option<u8>; |
| 170 | //! } |
| 171 | //! } |
| 172 | //! } |
| 173 | //! ``` |
| 174 | //! |
| 175 | //! - Call `await` on async functions |
| 176 | //! ```rust |
| 177 | //! use delegate::delegate; |
| 178 | //! |
| 179 | //! struct Inner; |
| 180 | //! impl Inner { |
| 181 | //! pub async fn method(&self, num: u32) -> u32 { num } |
| 182 | //! } |
| 183 | //! struct Wrapper { inner: Inner } |
| 184 | //! impl Wrapper { |
| 185 | //! delegate! { |
| 186 | //! to self.inner { |
| 187 | //! // calls method(num).await, returns impl Future<Output = u32> |
| 188 | //! pub async fn method(&self, num: u32) -> u32; |
| 189 | //! |
| 190 | //! // calls method(num).await.into(), returns impl Future<Output = u64> |
| 191 | //! #[into] |
| 192 | //! #[call(method)] |
| 193 | //! pub async fn method_into(&self, num: u32) -> u64; |
| 194 | //! } |
| 195 | //! } |
| 196 | //! } |
| 197 | //! ``` |
| 198 | //! You can use the `#[await(true/false)]` attribute on delegated methods to specify if `.await` should |
| 199 | //! be generated after the delegated expression. It will be generated by default if the delegated |
| 200 | //! method is `async`. |
| 201 | //! - Delegate to multiple fields |
| 202 | //! ```rust |
| 203 | //! use delegate::delegate; |
| 204 | //! |
| 205 | //! struct MultiStack { |
| 206 | //! left: Vec<u32>, |
| 207 | //! right: Vec<u32>, |
| 208 | //! } |
| 209 | //! impl MultiStack { |
| 210 | //! delegate! { |
| 211 | //! to self.left { |
| 212 | //! // Push an item to the top of the left stack |
| 213 | //! #[call(push)] |
| 214 | //! pub fn push_left(&mut self, value: u32); |
| 215 | //! } |
| 216 | //! to self.right { |
| 217 | //! // Push an item to the top of the right stack |
| 218 | //! #[call(push)] |
| 219 | //! pub fn push_right(&mut self, value: u32); |
| 220 | //! } |
| 221 | //! } |
| 222 | //! } |
| 223 | //! ``` |
| 224 | //! - Inserts `#[inline(always)]` automatically (unless you specify `#[inline]` manually on the method) |
| 225 | //! - You can use an attribute on a whole segment to automatically apply it to all methods in that |
| 226 | //! segment: |
| 227 | //! ```rust |
| 228 | //! use delegate::delegate; |
| 229 | //! |
| 230 | //! struct Inner; |
| 231 | //! |
| 232 | //! impl Inner { |
| 233 | //! fn foo(&self) -> Result<u32, ()> { Ok(0) } |
| 234 | //! fn bar(&self) -> Result<u32, ()> { Ok(1) } |
| 235 | //! } |
| 236 | //! |
| 237 | //! struct Wrapper { inner: Inner } |
| 238 | //! |
| 239 | //! impl Wrapper { |
| 240 | //! delegate! { |
| 241 | //! #[unwrap] |
| 242 | //! to self.inner { |
| 243 | //! fn foo(&self) -> u32; // calls self.inner.foo().unwrap() |
| 244 | //! fn bar(&self) -> u32; // calls self.inner.bar().unwrap() |
| 245 | //! } |
| 246 | //! } |
| 247 | //! } |
| 248 | //! ``` |
| 249 | //! - Specify expressions in the signature that will be used as delegated arguments |
| 250 | //! ```rust |
| 251 | //! use delegate::delegate; |
| 252 | //! struct Inner; |
| 253 | //! impl Inner { |
| 254 | //! pub fn polynomial(&self, a: i32, x: i32, b: i32, y: i32, c: i32) -> i32 { |
| 255 | //! a + x * x + b * y + c |
| 256 | //! } |
| 257 | //! } |
| 258 | //! struct Wrapper { inner: Inner, a: i32, b: i32, c: i32 } |
| 259 | //! impl Wrapper { |
| 260 | //! delegate! { |
| 261 | //! to self.inner { |
| 262 | //! // Calls `polynomial` on `inner` with `self.a`, `self.b` and |
| 263 | //! // `self.c` passed as arguments `a`, `b`, and `c`, effectively |
| 264 | //! // calling `polynomial(self.a, x, self.b, y, self.c)`. |
| 265 | //! pub fn polynomial(&self, [ self.a ], x: i32, [ self.b ], y: i32, [ self.c ]) -> i32 ; |
| 266 | //! // Calls `polynomial` on `inner` with `0`s passed for arguments |
| 267 | //! // `a` and `x`, and `self.b` and `self.c` for `b` and `c`, |
| 268 | //! // effectively calling `polynomial(0, 0, self.b, y, self.c)`. |
| 269 | //! #[call(polynomial)] |
| 270 | //! pub fn linear(&self, [ 0 ], [ 0 ], [ self.b ], y: i32, [ self.c ]) -> i32 ; |
| 271 | //! } |
| 272 | //! } |
| 273 | //! } |
| 274 | //! ``` |
| 275 | //! - Modify how will an input parameter be passed to the delegated method with parameter attribute modifiers. |
| 276 | //! Currently, the following modifiers are supported: |
| 277 | //! - `#[into]`: Calls `.into()` on the parameter passed to the delegated method. |
| 278 | //! - `#[as_ref]`: Calls `.as_ref()` on the parameter passed to the delegated method. |
| 279 | //! - `#[newtype]`: Calls `.0` on the parameter passed to the delegated method. |
| 280 | //! ```rust |
| 281 | //! use delegate::delegate; |
| 282 | //! |
| 283 | //! struct InnerType {} |
| 284 | //! impl InnerType { |
| 285 | //! fn foo(&self, other: Self) {} |
| 286 | //! } |
| 287 | //! |
| 288 | //! impl From<Wrapper> for InnerType { |
| 289 | //! fn from(wrapper: Wrapper) -> Self { |
| 290 | //! wrapper.0 |
| 291 | //! } |
| 292 | //! } |
| 293 | //! |
| 294 | //! struct Wrapper(InnerType); |
| 295 | //! impl Wrapper { |
| 296 | //! delegate! { |
| 297 | //! to self.0 { |
| 298 | //! // Calls `self.0.foo(other.into());` |
| 299 | //! pub fn foo(&self, #[into] other: Self); |
| 300 | //! } |
| 301 | //! } |
| 302 | //! } |
| 303 | //! ``` |
| 304 | //! - Specify a trait through which will the delegated method be called |
| 305 | //! (using [UFCS](https://doc.rust-lang.org/reference/expressions/call-expr.html#disambiguating-function-calls). |
| 306 | //! ```rust |
| 307 | //! use delegate::delegate; |
| 308 | //! |
| 309 | //! struct InnerType {} |
| 310 | //! impl InnerType { |
| 311 | //! |
| 312 | //! } |
| 313 | //! |
| 314 | //! trait MyTrait { |
| 315 | //! fn foo(&self); |
| 316 | //! } |
| 317 | //! impl MyTrait for InnerType { |
| 318 | //! fn foo(&self) {} |
| 319 | //! } |
| 320 | //! |
| 321 | //! struct Wrapper(InnerType); |
| 322 | //! impl Wrapper { |
| 323 | //! delegate! { |
| 324 | //! to &self.0 { |
| 325 | //! // Calls `MyTrait::foo(&self.0)` |
| 326 | //! #[through(MyTrait)] |
| 327 | //! pub fn foo(&self); |
| 328 | //! } |
| 329 | //! } |
| 330 | //! } |
| 331 | //! ``` |
| 332 | //! |
| 333 | //! - Add additional arguments to method |
| 334 | //! |
| 335 | //! ```rust |
| 336 | //! use delegate::delegate; |
| 337 | //! use std::cell::OnceCell; |
| 338 | //! struct Inner(u32); |
| 339 | //! impl Inner { |
| 340 | //! pub fn new(m: u32) -> Self { |
| 341 | //! // some "very complex" constructing work |
| 342 | //! Self(m) |
| 343 | //! } |
| 344 | //! pub fn method(&self, n: u32) -> u32 { |
| 345 | //! self.0 + n |
| 346 | //! } |
| 347 | //! } |
| 348 | //! |
| 349 | //! struct Wrapper { |
| 350 | //! inner: OnceCell<Inner>, |
| 351 | //! } |
| 352 | //! |
| 353 | //! impl Wrapper { |
| 354 | //! pub fn new() -> Self { |
| 355 | //! Self { |
| 356 | //! inner: OnceCell::new(), |
| 357 | //! } |
| 358 | //! } |
| 359 | //! fn content(&self, val: u32) -> &Inner { |
| 360 | //! self.inner.get_or_init(|| Inner(val)) |
| 361 | //! } |
| 362 | //! delegate! { |
| 363 | //! to |k: u32| self.content(k) { |
| 364 | //! // `wrapper.method(k, num)` will call `self.content(k).method(num)` |
| 365 | //! pub fn method(&self, num: u32) -> u32; |
| 366 | //! } |
| 367 | //! } |
| 368 | //! } |
| 369 | //! ``` |
| 370 | //! - Delegate associated functions |
| 371 | //! ```rust |
| 372 | //! use delegate::delegate; |
| 373 | //! |
| 374 | //! struct A {} |
| 375 | //! impl A { |
| 376 | //! fn foo(a: u32) -> u32 { |
| 377 | //! a + 1 |
| 378 | //! } |
| 379 | //! } |
| 380 | //! |
| 381 | //! struct B; |
| 382 | //! |
| 383 | //! impl B { |
| 384 | //! delegate! { |
| 385 | //! to A { |
| 386 | //! fn foo(a: u32) -> u32; |
| 387 | //! } |
| 388 | //! } |
| 389 | //! } |
| 390 | //! |
| 391 | //! assert_eq!(B::foo(1), 2); |
| 392 | //! ``` |
| 393 | //! - Delegate associated constants |
| 394 | //! |
| 395 | //! ```rust |
| 396 | //! use delegate::delegate; |
| 397 | //! |
| 398 | //! trait WithConst { |
| 399 | //! const TOTO: u8; |
| 400 | //! } |
| 401 | //! |
| 402 | //! struct A; |
| 403 | //! impl WithConst for A { |
| 404 | //! const TOTO: u8 = 1; |
| 405 | //! } |
| 406 | //! |
| 407 | //! struct B; |
| 408 | //! impl WithConst for B { |
| 409 | //! const TOTO: u8 = 2; |
| 410 | //! } |
| 411 | //! struct C; |
| 412 | //! impl WithConst for C { |
| 413 | //! const TOTO: u8 = 2; |
| 414 | //! } |
| 415 | //! |
| 416 | //! enum Enum { |
| 417 | //! A(A), |
| 418 | //! B(B), |
| 419 | //! C(C), |
| 420 | //! } |
| 421 | //! |
| 422 | //! impl Enum { |
| 423 | //! delegate! { |
| 424 | //! to match self { |
| 425 | //! Self::A(a) => a, |
| 426 | //! Self::B(b) => b, |
| 427 | //! Self::C(c) => { println!("hello from c"); c }, |
| 428 | //! } { |
| 429 | //! #[const(WithConst::TOTO)] |
| 430 | //! fn get_toto(&self) -> u8; |
| 431 | //! } |
| 432 | //! } |
| 433 | //! } |
| 434 | //! |
| 435 | //! assert_eq!(Enum::A(A).get_toto(), <A as WithConst>::TOTO); |
| 436 | //! ``` |
| 437 | //! |
| 438 | //! - Delegate to fields |
| 439 | //! ```rust |
| 440 | //! use delegate::delegate; |
| 441 | //! |
| 442 | //! struct Datum { |
| 443 | //! value: u32, |
| 444 | //! error: u32, |
| 445 | //! } |
| 446 | //! |
| 447 | //! struct DatumWrapper(Datum); |
| 448 | //! |
| 449 | //! impl DatumWrapper { |
| 450 | //! delegate! { |
| 451 | //! to self.0 { |
| 452 | //! /// Get the value of a nested field with the same name |
| 453 | //! #[field] |
| 454 | //! fn value(&self) -> u32; |
| 455 | //! |
| 456 | //! /// Get the value of a nested field with a different name |
| 457 | //! #[field(value)] |
| 458 | //! fn renamed_value(&self) -> u32; |
| 459 | //! |
| 460 | //! /// Get shared reference to a nested field |
| 461 | //! #[field(&value)] |
| 462 | //! fn value_ref(&self) -> &u32; |
| 463 | //! |
| 464 | //! /// Get mutable reference to a nested field |
| 465 | //! #[field(&mut value)] |
| 466 | //! fn value_ref_mut(&mut self) -> &mut u32; |
| 467 | //! |
| 468 | //! /// Get mutable reference to a nested field with the same name |
| 469 | //! #[field(&)] |
| 470 | //! fn error(&self) -> &u32; |
| 471 | //! } |
| 472 | //! } |
| 473 | //! } |
| 474 | //! ``` |
| 475 | |
| 476 | extern crate proc_macrorust-analyzer cargo delegate 0.13.5 proc_macro/; |
| 477 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/; |
| 478 | |
| 479 | use attributesrust-analyzer cargo delegate 0.13.5 attributes/::AssociatedConstantrust-analyzer cargo delegate 0.13.5 attributes/AssociatedConstant#; |
| 480 | use proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#; |
| 481 | |
| 482 | use proc_macro2rust-analyzer cargo proc-macro2 1.0.103 crate/::Identrust-analyzer cargo proc-macro2 1.0.103 Ident#; |
| 483 | use quoterust-analyzer cargo quote 1.0.42 crate/::{quoterust-analyzer cargo quote 1.0.42 quote!, ToTokensrust-analyzer cargo quote 1.0.42 to_tokens/ToTokens#}; |
| 484 | use synrust-analyzer cargo syn 2.0.110 crate/::parserust-analyzer cargo syn 2.0.110 parse/::{Parserust-analyzer cargo syn 2.0.110 parse/Parse#, ParseStreamrust-analyzer cargo syn 2.0.110 parse/ParseStream#}; |
| 485 | use synrust-analyzer cargo syn 2.0.110 crate/::spannedrust-analyzer cargo syn 2.0.110 spanned/::Spannedrust-analyzer cargo syn 2.0.110 spanned/Spanned#; |
| 486 | use synrust-analyzer cargo syn 2.0.110 crate/::visit_mutrust-analyzer cargo syn 2.0.110 gen/visit_mut/::VisitMutrust-analyzer cargo syn 2.0.110 gen/visit_mut/VisitMut#; |
| 487 | use synrust-analyzer cargo syn 2.0.110 crate/::{parse_quoterust-analyzer cargo syn 2.0.110 parse_quote/, Errorrust-analyzer cargo syn 2.0.110 error/Error#, Exprrust-analyzer cargo syn 2.0.110 expr/Expr#, ExprFieldrust-analyzer cargo syn 2.0.110 expr/ExprField#, ExprMethodCallrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall#, FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#, GenericParamrust-analyzer cargo syn 2.0.110 generics/GenericParam#, Metarust-analyzer cargo syn 2.0.110 attr/Meta#}; |
| 488 | |
| 489 | use craterust-analyzer cargo delegate 0.13.5 crate/::attributesrust-analyzer cargo delegate 0.13.5 attributes/::{ |
| 490 | combine_attributesrust-analyzer cargo delegate 0.13.5 attributes/combine_attributes()., parse_method_attributesrust-analyzer cargo delegate 0.13.5 attributes/parse_method_attributes()., parse_segment_attributesrust-analyzer cargo delegate 0.13.5 attributes/parse_segment_attributes()., ReturnExpressionrust-analyzer cargo delegate 0.13.5 attributes/ReturnExpression#, |
| 491 | SegmentAttributesrust-analyzer cargo delegate 0.13.5 attributes/SegmentAttributes#, TargetSpecifierrust-analyzer cargo delegate 0.13.5 attributes/TargetSpecifier#, |
| 492 | }; |
| 493 | |
| 494 | mod attributesrust-analyzer cargo delegate 0.13.5 attributes/; |
| 495 | |
| 496 | mod kwrust-analyzer cargo delegate 0.13.5 kw/ { |
| 497 | synrust-analyzer cargo syn 2.0.110 crate/::custom_keywordrust-analyzer cargo syn 2.0.110 custom_keyword/custom_keyword!!(torust-analyzer cargo delegate 0.13.5 kw/to#); |
| 498 | synrust-analyzer cargo syn 2.0.110 crate/::custom_keywordrust-analyzer cargo syn 2.0.110 custom_keyword/custom_keyword!!(targetrust-analyzer cargo delegate 0.13.5 kw/target#); |
| 499 | } |
| 500 | |
| 501 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 502 | enum ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier# { |
| 503 | Intorust-analyzer cargo delegate 0.13.5 ArgumentModifier#Into#, |
| 504 | AsRefrust-analyzer cargo delegate 0.13.5 ArgumentModifier#AsRef#, |
| 505 | Newtyperust-analyzer cargo delegate 0.13.5 ArgumentModifier#Newtype#, |
| 506 | } |
| 507 | |
| 508 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 509 | enum DelegatedInputrust-analyzer cargo delegate 0.13.5 DelegatedInput# { |
| 510 | Inputrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input# { |
| 511 | parameterrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input#parameter.: synrust-analyzer cargo syn 2.0.110 crate/::FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#, |
| 512 | modifierrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input#modifier.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier#>, |
| 513 | }, |
| 514 | Argumentrust-analyzer cargo delegate 0.13.5 DelegatedInput#Argument#(synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#), |
| 515 | } |
| 516 | |
| 517 | fn get_argument_modifierrust-analyzer cargo delegate 0.13.5 get_argument_modifier().(attribute: synrust-analyzer cargo syn 2.0.110 crate/::Attributerust-analyzer cargo syn 2.0.110 attr/Attribute#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier#, Errorrust-analyzer cargo syn 2.0.110 error/Error#> { |
| 518 | if let Metarust-analyzer cargo syn 2.0.110 attr/Meta#::Pathrust-analyzer cargo syn 2.0.110 attr/Meta#Path#(mut path) = attribute.metarust-analyzer cargo syn 2.0.110 attr/Attribute#meta. { |
| 519 | if path.segmentsrust-analyzer cargo syn 2.0.110 path/Path#segments..lenrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 { |
| 520 | let segment = path.segmentsrust-analyzer cargo syn 2.0.110 path/Path#segments..poprust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]pop().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 521 | if segment.valuerust-analyzer cargo syn 2.0.110 punctuated/impl#[`Pair<T, P>`]value().().argumentsrust-analyzer cargo syn 2.0.110 path/PathSegment#arguments..is_emptyrust-analyzer cargo syn 2.0.110 path/impl#[PathArguments]is_empty().() { |
| 522 | let ident = segment.valuerust-analyzer cargo syn 2.0.110 punctuated/impl#[`Pair<T, P>`]value().().identrust-analyzer cargo syn 2.0.110 path/PathSegment#ident..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 523 | let ident = ident.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(); |
| 524 | |
| 525 | match ident { |
| 526 | "into" => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier#::Intorust-analyzer cargo delegate 0.13.5 ArgumentModifier#Into#), |
| 527 | "as_ref" => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier#::AsRefrust-analyzer cargo delegate 0.13.5 ArgumentModifier#AsRef#), |
| 528 | "newtype" => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier#::Newtyperust-analyzer cargo delegate 0.13.5 ArgumentModifier#Newtype#), |
| 529 | _ => (), |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | }; |
| 534 | |
| 535 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("The attribute argument has to be `into` or `as_ref`, like this: `#[into] a: u32`.") |
| 536 | } |
| 537 | |
| 538 | impl synrust-analyzer cargo syn 2.0.110 crate/::parserust-analyzer cargo syn 2.0.110 parse/::Parserust-analyzer cargo syn 2.0.110 parse/Parse# for DelegatedInputrust-analyzer cargo delegate 0.13.5 DelegatedInput# { |
| 539 | fn parserust-analyzer cargo delegate 0.13.5 impl#[DelegatedInput][Parse]parse().(input: ParseStreamrust-analyzer cargo syn 2.0.110 parse/ParseStream#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo delegate 0.13.5 impl#[DelegatedInput][Parse], Errorrust-analyzer cargo syn 2.0.110 error/Error#> { |
| 540 | let lookahead = input.lookahead1rust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]lookahead1().(); |
| 541 | if lookahead.peekrust-analyzer cargo syn 2.0.110 lookahead/impl#[`Lookahead1<'a>`]peek().(synrust-analyzer cargo syn 2.0.110 crate/::tokenrust-analyzer cargo syn 2.0.110 token/::Bracketrust-analyzer cargo syn 2.0.110 token/Bracket().) { |
| 542 | let content; |
| 543 | let _bracket_token = synrust-analyzer cargo syn 2.0.110 crate/::bracketedrust-analyzer cargo syn 2.0.110 group/bracketed!!(content in input); |
| 544 | let expression: synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr# = content.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 545 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo delegate 0.13.5 impl#[DelegatedInput][Parse]::Argumentrust-analyzer cargo delegate 0.13.5 DelegatedInput#Argument#(expression)) |
| 546 | } else { |
| 547 | let (input, modifier) = if lookahead.peekrust-analyzer cargo syn 2.0.110 lookahead/impl#[`Lookahead1<'a>`]peek().(synrust-analyzer cargo syn 2.0.110 crate/::tokenrust-analyzer cargo syn 2.0.110 token/::Poundrust-analyzer cargo syn 2.0.110 token/Pound().) { |
| 548 | let mut attributes = input.callrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]call().(tolerant_outer_attributesrust-analyzer cargo delegate 0.13.5 tolerant_outer_attributes().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 549 | if attributes.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1 { |
| 550 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("You can specify at most a single attribute for each parameter in a delegated method"); |
| 551 | } |
| 552 | let modifier = get_argument_modifierrust-analyzer cargo delegate 0.13.5 get_argument_modifier().(attributes.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]pop().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()) |
| 553 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Could not parse argument modifier attribute"); |
| 554 | |
| 555 | let input: synrust-analyzer cargo syn 2.0.110 crate/::FnArgrust-analyzer cargo syn 2.0.110 item/FnArg# = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 556 | (input, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(modifier)) |
| 557 | } else { |
| 558 | (input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) |
| 559 | }; |
| 560 | |
| 561 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo delegate 0.13.5 impl#[DelegatedInput][Parse]::Inputrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input# { |
| 562 | parameterrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input#parameter.: input, |
| 563 | modifierrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input#modifier., |
| 564 | }) |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | struct DelegatedMethodrust-analyzer cargo delegate 0.13.5 DelegatedMethod# { |
| 570 | methodrust-analyzer cargo delegate 0.13.5 DelegatedMethod#method.: synrust-analyzer cargo syn 2.0.110 crate/::TraitItemFnrust-analyzer cargo syn 2.0.110 item/TraitItemFn#, |
| 571 | attributesrust-analyzer cargo delegate 0.13.5 DelegatedMethod#attributes.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<synrust-analyzer cargo syn 2.0.110 crate/::Attributerust-analyzer cargo syn 2.0.110 attr/Attribute#>, |
| 572 | visibilityrust-analyzer cargo delegate 0.13.5 DelegatedMethod#visibility.: synrust-analyzer cargo syn 2.0.110 crate/::Visibilityrust-analyzer cargo syn 2.0.110 restriction/Visibility#, |
| 573 | argumentsrust-analyzer cargo delegate 0.13.5 DelegatedMethod#arguments.: synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Punctuated#<synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#, synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![,]>, |
| 574 | } |
| 575 | |
| 576 | // Given an input parameter from a function signature, create a function |
| 577 | // argument used to call the delegate function: omit receiver, extract an |
| 578 | // identifier from a typed input parameter (and wrap it in an `Expr`). |
| 579 | fn parse_input_into_argument_expressionrust-analyzer cargo delegate 0.13.5 parse_input_into_argument_expression().( |
| 580 | function_name: &Identrust-analyzer cargo proc-macro2 1.0.103 Ident#, |
| 581 | input: &synrust-analyzer cargo syn 2.0.110 crate/::FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#, |
| 582 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#> { |
| 583 | match input { |
| 584 | // Parse inputs of the form `x: T` to retrieve their identifiers. |
| 585 | synrust-analyzer cargo syn 2.0.110 crate/::FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#::Typedrust-analyzer cargo syn 2.0.110 item/FnArg#Typed#(typed) => { |
| 586 | match &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().typed.patrust-analyzer cargo syn 2.0.110 pat/PatType#pat. { |
| 587 | // This should not happen, I think. If it does, |
| 588 | // it will be ignored as if it were the |
| 589 | // receiver. |
| 590 | synrust-analyzer cargo syn 2.0.110 crate/::Patrust-analyzer cargo syn 2.0.110 pat/Pat#::Identrust-analyzer cargo syn 2.0.110 pat/Pat#Ident#(ident) if ident.identrust-analyzer cargo syn 2.0.110 pat/PatIdent#ident. rust-analyzer cargo proc-macro2 1.0.103 impl#[Ident][`PartialEq<T>`]eq().==rust-analyzer cargo proc-macro2 1.0.103 impl#[Ident][`PartialEq<T>`]eq(). rust-analyzer cargo proc-macro2 1.0.103 impl#[Ident][`PartialEq<T>`]eq()."self" => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 591 | // Expression in the form `x: T`. Extract the |
| 592 | // identifier, wrap it in Expr for type compatibility with bracketed expressions, |
| 593 | // and append it |
| 594 | // to the argument list. |
| 595 | synrust-analyzer cargo syn 2.0.110 crate/::Patrust-analyzer cargo syn 2.0.110 pat/Pat#::Identrust-analyzer cargo syn 2.0.110 pat/Pat#Ident#(ident) => { |
| 596 | let path_segment = synrust-analyzer cargo syn 2.0.110 crate/::PathSegmentrust-analyzer cargo syn 2.0.110 path/PathSegment# { |
| 597 | identrust-analyzer cargo syn 2.0.110 path/PathSegment#ident.: ident.identrust-analyzer cargo syn 2.0.110 pat/PatIdent#ident..clonerust-analyzer cargo proc-macro2 1.0.103 impl#[Ident][Clone]clone().(), |
| 598 | argumentsrust-analyzer cargo syn 2.0.110 path/PathSegment#arguments.: synrust-analyzer cargo syn 2.0.110 crate/::PathArgumentsrust-analyzer cargo syn 2.0.110 path/PathArguments#::Nonerust-analyzer cargo syn 2.0.110 path/PathArguments#None#, |
| 599 | }; |
| 600 | let mut segments = synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Punctuated#::newrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]new().(); |
| 601 | segments.pushrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push().(path_segment); |
| 602 | let path = synrust-analyzer cargo syn 2.0.110 crate/::Pathrust-analyzer cargo syn 2.0.110 path/Path# { |
| 603 | leading_colonrust-analyzer cargo syn 2.0.110 path/Path#leading_colon.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 604 | segmentsrust-analyzer cargo syn 2.0.110 path/Path#segments., |
| 605 | }; |
| 606 | let ident_as_expr = synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#::fromrust-analyzer cargo syn 2.0.110 expr/impl#[Expr][`From<ExprPath>`]from().(synrust-analyzer cargo syn 2.0.110 crate/::ExprPathrust-analyzer cargo syn 2.0.110 expr/ExprPath# { |
| 607 | attrsrust-analyzer cargo syn 2.0.110 expr/ExprPath#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 608 | qselfrust-analyzer cargo syn 2.0.110 expr/ExprPath#qself.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 609 | pathrust-analyzer cargo syn 2.0.110 expr/ExprPath#path., |
| 610 | }); |
| 611 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ident_as_expr) |
| 612 | } |
| 613 | // Other more complex argument expressions are not covered. |
| 614 | _ => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!( |
| 615 | "You have to use simple identifiers for delegated method parameters ({})", |
| 616 | function_name // The signature is not constructed yet. We make due. |
| 617 | ), |
| 618 | } |
| 619 | } |
| 620 | // Skip any `self`/`&self`/`&mut self` argument, since |
| 621 | // it does not appear in the argument list and it's |
| 622 | // already added to the parameter list. |
| 623 | synrust-analyzer cargo syn 2.0.110 crate/::FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#::Receiverrust-analyzer cargo syn 2.0.110 item/FnArg#Receiver#(_receiver) => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | impl synrust-analyzer cargo syn 2.0.110 crate/::parserust-analyzer cargo syn 2.0.110 parse/::Parserust-analyzer cargo syn 2.0.110 parse/Parse# for DelegatedMethodrust-analyzer cargo delegate 0.13.5 DelegatedMethod# { |
| 628 | fn parserust-analyzer cargo delegate 0.13.5 impl#[DelegatedMethod][Parse]parse().(input: ParseStreamrust-analyzer cargo syn 2.0.110 parse/ParseStream#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo delegate 0.13.5 impl#[DelegatedMethod][Parse], Errorrust-analyzer cargo syn 2.0.110 error/Error#> { |
| 629 | let attributes = input.callrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]call().(tolerant_outer_attributesrust-analyzer cargo delegate 0.13.5 tolerant_outer_attributes().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 630 | let visibility = input.callrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]call().(synrust-analyzer cargo syn 2.0.110 crate/::Visibilityrust-analyzer cargo syn 2.0.110 restriction/Visibility#::parserust-analyzer cargo syn 2.0.110 restriction/parsing/impl#[Visibility][Parse]parse().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 631 | |
| 632 | // Unchanged from Parse from TraitItemMethod |
| 633 | let constness: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![const]> = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 634 | let asyncness: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![async]> = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 635 | let unsafety: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![unsafe]> = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 636 | let abi: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.110 crate/::Abirust-analyzer cargo syn 2.0.110 ty/Abi#> = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 637 | let fn_token: synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![fn] = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 638 | let ident: Identrust-analyzer cargo proc-macro2 1.0.103 Ident# = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 639 | let generics: synrust-analyzer cargo syn 2.0.110 crate/::Genericsrust-analyzer cargo syn 2.0.110 generics/Generics# = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 640 | |
| 641 | let content; |
| 642 | let paren_token = synrust-analyzer cargo syn 2.0.110 crate/::parenthesizedrust-analyzer cargo syn 2.0.110 group/parenthesized!!(content in input); |
| 643 | |
| 644 | // Parse inputs (method parameters) and arguments. The parameters |
| 645 | // constitute the parameter list of the signature of the delegating |
| 646 | // method so it must include all inputs, except bracketed expressions. |
| 647 | // The argument list constitutes the list of arguments used to call the |
| 648 | // delegated function. It must include all inputs, excluding the |
| 649 | // receiver (self-type) input. The arguments must all be parsed to |
| 650 | // retrieve the expressions inside of the brackets as well as variable |
| 651 | // identifiers of ordinary inputs. The arguments must preserve the order |
| 652 | // of the inputs. |
| 653 | let delegated_inputs = content.parse_terminatedrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse_terminated().(DelegatedInputrust-analyzer cargo delegate 0.13.5 DelegatedInput#::parserust-analyzer cargo delegate 0.13.5 impl#[DelegatedInput][Parse]parse()., synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![,])?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 654 | let mut inputs: synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Punctuated#<synrust-analyzer cargo syn 2.0.110 crate/::FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#, synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![,]> = |
| 655 | synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Punctuated#::newrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]new().(); |
| 656 | let mut arguments: synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Punctuated#<synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#, synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![,]> = |
| 657 | synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Punctuated#::newrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]new().(); |
| 658 | |
| 659 | // First, combine the cases for pairs with cases for end, to remove |
| 660 | // redundancy below. |
| 661 | delegated_inputs |
| 662 | .into_pairsrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]into_pairs().() |
| 663 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|punctuated_pair| match punctuated_pair { |
| 664 | synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Pairrust-analyzer cargo syn 2.0.110 punctuated/Pair#::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Pair#Punctuated#(item, comma) => (item, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(comma)), |
| 665 | synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Pairrust-analyzer cargo syn 2.0.110 punctuated/Pair#::Endrust-analyzer cargo syn 2.0.110 punctuated/Pair#End#(item) => (item, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), |
| 666 | }) |
| 667 | .for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|pair| match pair { |
| 668 | // This input is a bracketed argument (eg. `[ self.x ]`). It |
| 669 | // is omitted in the signature of the delegator, but the |
| 670 | // expression inside the brackets is used in the body of the |
| 671 | // delegator, as an arugnment to the delegated function (eg. |
| 672 | // `self.x`). The argument needs to be generated in the |
| 673 | // appropriate position with respect other arguments and non- |
| 674 | // argument inputs. As long as inputs are added to the |
| 675 | // `arguments` vector in order of occurance, this is trivial. |
| 676 | (DelegatedInputrust-analyzer cargo delegate 0.13.5 DelegatedInput#::Argumentrust-analyzer cargo delegate 0.13.5 DelegatedInput#Argument#(argument), maybe_comma) => { |
| 677 | arguments.push_valuerust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push_value().(argument); |
| 678 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(comma) = maybe_comma { |
| 679 | arguments.push_punctrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push_punct().(comma) |
| 680 | } |
| 681 | } |
| 682 | // The input is a standard function parameter with a name and |
| 683 | // a type (eg. `x: T`). This input needs to be reflected in |
| 684 | // the delegator signature as is (eg. `x: T`). The identifier |
| 685 | // also needs to be included in the argument list in part |
| 686 | // (eg. `x`). The argument list needs to preserve the order of |
| 687 | // the inputs with relation to arguments (see above), so the |
| 688 | // parsing is best done here (previously it was done at |
| 689 | // generation). |
| 690 | ( |
| 691 | DelegatedInputrust-analyzer cargo delegate 0.13.5 DelegatedInput#::Inputrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input# { |
| 692 | parameterrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input#parameter., |
| 693 | modifierrust-analyzer cargo delegate 0.13.5 DelegatedInput#Input#modifier., |
| 694 | }, |
| 695 | maybe_comma, |
| 696 | ) => { |
| 697 | inputs.push_valuerust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push_value().(parameter.clonerust-analyzer cargo syn 2.0.110 gen/clone/impl#[`crate::FnArg`][Clone]clone().()); |
| 698 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(comma) = maybe_comma { |
| 699 | inputs.push_punctrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push_punct().(comma); |
| 700 | } |
| 701 | let maybe_argument = parse_input_into_argument_expressionrust-analyzer cargo delegate 0.13.5 parse_input_into_argument_expression().(&ident, ¶meter); |
| 702 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(mut argument) = maybe_argument { |
| 703 | let span = argument.spanrust-analyzer cargo syn 2.0.110 spanned/impl#[T][Spanned]span().(); |
| 704 | |
| 705 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(modifier) = modifier { |
| 706 | let method_call = |name: &str| { |
| 707 | synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#::fromrust-analyzer cargo syn 2.0.110 expr/impl#[Expr][`From<ExprMethodCall>`]from().(ExprMethodCallrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall# { |
| 708 | attrsrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall#attrs.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![], |
| 709 | receiverrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall#receiver.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(argument.clonerust-analyzer cargo syn 2.0.110 gen/clone/impl#[`crate::Expr`][Clone]clone().()), |
| 710 | dot_tokenrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall#dot_token.: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo syn 2.0.110 token/impl#[Dot][Default]default().(), |
| 711 | methodrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall#method.: Identrust-analyzer cargo proc-macro2 1.0.103 Ident#::newrust-analyzer cargo proc-macro2 1.0.103 impl#[Ident]new().(name, span), |
| 712 | turbofishrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall#turbofish.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 713 | paren_tokenrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall#paren_token., |
| 714 | argsrust-analyzer cargo syn 2.0.110 expr/ExprMethodCall#args.: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`][Default]default().(), |
| 715 | }) |
| 716 | }; |
| 717 | |
| 718 | let field_call = || { |
| 719 | synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#::fromrust-analyzer cargo syn 2.0.110 expr/impl#[Expr][`From<ExprField>`]from().(ExprFieldrust-analyzer cargo syn 2.0.110 expr/ExprField# { |
| 720 | attrsrust-analyzer cargo syn 2.0.110 expr/ExprField#attrs.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![], |
| 721 | baserust-analyzer cargo syn 2.0.110 expr/ExprField#base.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(argument.clonerust-analyzer cargo syn 2.0.110 gen/clone/impl#[`crate::Expr`][Clone]clone().()), |
| 722 | dot_tokenrust-analyzer cargo syn 2.0.110 expr/ExprField#dot_token.: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo syn 2.0.110 token/impl#[Dot][Default]default().(), |
| 723 | memberrust-analyzer cargo syn 2.0.110 expr/ExprField#member.: synrust-analyzer cargo syn 2.0.110 crate/::Memberrust-analyzer cargo syn 2.0.110 expr/Member#::Unnamedrust-analyzer cargo syn 2.0.110 expr/Member#Unnamed#(0.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 724 | }) |
| 725 | }; |
| 726 | |
| 727 | match modifier { |
| 728 | ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier#::Intorust-analyzer cargo delegate 0.13.5 ArgumentModifier#Into# => { |
| 729 | argument = method_call("into"); |
| 730 | } |
| 731 | ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier#::AsRefrust-analyzer cargo delegate 0.13.5 ArgumentModifier#AsRef# => { |
| 732 | argument = method_call("as_ref"); |
| 733 | } |
| 734 | ArgumentModifierrust-analyzer cargo delegate 0.13.5 ArgumentModifier#::Newtyperust-analyzer cargo delegate 0.13.5 ArgumentModifier#Newtype# => argument = field_call(), |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | arguments.pushrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push().(argument); |
| 739 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(comma) = maybe_comma { |
| 740 | arguments.push_punctrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push_punct().(comma); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | }); |
| 745 | |
| 746 | // Unchanged from Parse from TraitItemMethod |
| 747 | let output: synrust-analyzer cargo syn 2.0.110 crate/::ReturnTyperust-analyzer cargo syn 2.0.110 ty/ReturnType# = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 748 | let where_clause: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.110 crate/::WhereClauserust-analyzer cargo syn 2.0.110 generics/WhereClause#> = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 749 | |
| 750 | // This needs to be generated manually, because inputs need to be |
| 751 | // separated into actual inputs that go in the signature (the |
| 752 | // parameters) and the additional expressions in square brackets which |
| 753 | // go into the arguments vector (artguments of the call on the method |
| 754 | // on the inner object). |
| 755 | let signature = synrust-analyzer cargo syn 2.0.110 crate/::Signaturerust-analyzer cargo syn 2.0.110 item/Signature# { |
| 756 | constnessrust-analyzer cargo syn 2.0.110 item/Signature#constness., |
| 757 | asyncnessrust-analyzer cargo syn 2.0.110 item/Signature#asyncness., |
| 758 | unsafetyrust-analyzer cargo syn 2.0.110 item/Signature#unsafety., |
| 759 | abirust-analyzer cargo syn 2.0.110 item/Signature#abi., |
| 760 | fn_tokenrust-analyzer cargo syn 2.0.110 item/Signature#fn_token., |
| 761 | identrust-analyzer cargo syn 2.0.110 item/Signature#ident., |
| 762 | paren_tokenrust-analyzer cargo syn 2.0.110 item/Signature#paren_token., |
| 763 | inputsrust-analyzer cargo syn 2.0.110 item/Signature#inputs., |
| 764 | outputrust-analyzer cargo syn 2.0.110 item/Signature#output., |
| 765 | variadicrust-analyzer cargo syn 2.0.110 item/Signature#variadic.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 766 | genericsrust-analyzer cargo syn 2.0.110 item/Signature#generics.: synrust-analyzer cargo syn 2.0.110 crate/::Genericsrust-analyzer cargo syn 2.0.110 generics/Generics# { |
| 767 | where_clauserust-analyzer cargo syn 2.0.110 generics/Generics#where_clause., |
| 768 | ..generics |
| 769 | }, |
| 770 | }; |
| 771 | |
| 772 | // Check if the input contains a semicolon or a brace. If it contains |
| 773 | // a semicolon, we parse it (to retain token location information) and |
| 774 | // continue. However, if it contains a brace, this indicates that |
| 775 | // there is a default definition of the method. This is not supported, |
| 776 | // so in that case we error out. |
| 777 | let lookahead = input.lookahead1rust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]lookahead1().(); |
| 778 | let semi_token: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![;]> = if lookahead.peekrust-analyzer cargo syn 2.0.110 lookahead/impl#[`Lookahead1<'a>`]peek().(synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![;]) { |
| 779 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) |
| 780 | } else { |
| 781 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!( |
| 782 | "Do not include implementation of delegated functions ({})", |
| 783 | signature.identrust-analyzer cargo syn 2.0.110 item/Signature#ident. |
| 784 | ); |
| 785 | }; |
| 786 | |
| 787 | // This needs to be populated from scratch because of the signature above. |
| 788 | let method = synrust-analyzer cargo syn 2.0.110 crate/::TraitItemFnrust-analyzer cargo syn 2.0.110 item/TraitItemFn# { |
| 789 | // All attributes are attached to `DelegatedMethod`, since they |
| 790 | // presumably pertain to the process of delegation, not the |
| 791 | // signature of the delegator. |
| 792 | attrsrust-analyzer cargo syn 2.0.110 item/TraitItemFn#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 793 | sigrust-analyzer cargo syn 2.0.110 item/TraitItemFn#sig.: signature, |
| 794 | defaultrust-analyzer cargo syn 2.0.110 item/TraitItemFn#default.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 795 | semi_tokenrust-analyzer cargo syn 2.0.110 item/TraitItemFn#semi_token., |
| 796 | }; |
| 797 | |
| 798 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DelegatedMethodrust-analyzer cargo delegate 0.13.5 DelegatedMethod# { |
| 799 | methodrust-analyzer cargo delegate 0.13.5 DelegatedMethod#method., |
| 800 | attributesrust-analyzer cargo delegate 0.13.5 DelegatedMethod#attributes., |
| 801 | visibilityrust-analyzer cargo delegate 0.13.5 DelegatedMethod#visibility., |
| 802 | argumentsrust-analyzer cargo delegate 0.13.5 DelegatedMethod#arguments., |
| 803 | }) |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | struct DelegatedSegmentrust-analyzer cargo delegate 0.13.5 DelegatedSegment# { |
| 808 | delegatorrust-analyzer cargo delegate 0.13.5 DelegatedSegment#delegator.: synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#, |
| 809 | methodsrust-analyzer cargo delegate 0.13.5 DelegatedSegment#methods.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DelegatedMethodrust-analyzer cargo delegate 0.13.5 DelegatedMethod#>, |
| 810 | segment_attrsrust-analyzer cargo delegate 0.13.5 DelegatedSegment#segment_attrs.: SegmentAttributesrust-analyzer cargo delegate 0.13.5 attributes/SegmentAttributes#, |
| 811 | } |
| 812 | |
| 813 | impl synrust-analyzer cargo syn 2.0.110 crate/::parserust-analyzer cargo syn 2.0.110 parse/::Parserust-analyzer cargo syn 2.0.110 parse/Parse# for DelegatedSegmentrust-analyzer cargo delegate 0.13.5 DelegatedSegment# { |
| 814 | fn parserust-analyzer cargo delegate 0.13.5 impl#[DelegatedSegment][Parse]parse().(input: ParseStreamrust-analyzer cargo syn 2.0.110 parse/ParseStream#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo delegate 0.13.5 impl#[DelegatedSegment][Parse], Errorrust-analyzer cargo syn 2.0.110 error/Error#> { |
| 815 | let attributes = input.callrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]call().(tolerant_outer_attributesrust-analyzer cargo delegate 0.13.5 tolerant_outer_attributes().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 816 | let segment_attrs = parse_segment_attributesrust-analyzer cargo delegate 0.13.5 attributes/parse_segment_attributes().(&attributes); |
| 817 | |
| 818 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(keyword) = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().::<kwrust-analyzer cargo delegate 0.13.5 kw/::targetrust-analyzer cargo delegate 0.13.5 kw/target#>() { |
| 819 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo syn 2.0.110 error/Error#::newrust-analyzer cargo syn 2.0.110 error/impl#[Error]new().(keyword.spanrust-analyzer cargo syn 2.0.110 spanned/impl#[T][Spanned]span().(), "You are using the old `target` expression, which is deprecated. Please replace `target` with `to`.")); |
| 820 | } else { |
| 821 | input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().::<kwrust-analyzer cargo delegate 0.13.5 kw/::torust-analyzer cargo delegate 0.13.5 kw/to#>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 822 | } |
| 823 | |
| 824 | synrust-analyzer cargo syn 2.0.110 crate/::Exprrust-analyzer cargo syn 2.0.110 expr/Expr#::parse_without_eager_bracerust-analyzer cargo syn 2.0.110 expr/impl#[Expr]parse_without_eager_brace().(input).and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|delegator| { |
| 825 | let content; |
| 826 | synrust-analyzer cargo syn 2.0.110 crate/::bracedrust-analyzer cargo syn 2.0.110 group/braced!!(content in input); |
| 827 | |
| 828 | let mut methods = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 829 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().content.is_emptyrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]is_empty().() { |
| 830 | methods.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().( |
| 831 | content |
| 832 | .parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().::<DelegatedMethodrust-analyzer cargo delegate 0.13.5 DelegatedMethod#>() |
| 833 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Cannot parse delegated method"), |
| 834 | ); |
| 835 | } |
| 836 | |
| 837 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DelegatedSegmentrust-analyzer cargo delegate 0.13.5 DelegatedSegment# { |
| 838 | delegatorrust-analyzer cargo delegate 0.13.5 DelegatedSegment#delegator., |
| 839 | methodsrust-analyzer cargo delegate 0.13.5 DelegatedSegment#methods., |
| 840 | segment_attrsrust-analyzer cargo delegate 0.13.5 DelegatedSegment#segment_attrs., |
| 841 | }) |
| 842 | }) |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | struct DelegationBlockrust-analyzer cargo delegate 0.13.5 DelegationBlock# { |
| 847 | segmentsrust-analyzer cargo delegate 0.13.5 DelegationBlock#segments.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DelegatedSegmentrust-analyzer cargo delegate 0.13.5 DelegatedSegment#>, |
| 848 | } |
| 849 | |
| 850 | impl synrust-analyzer cargo syn 2.0.110 crate/::parserust-analyzer cargo syn 2.0.110 parse/::Parserust-analyzer cargo syn 2.0.110 parse/Parse# for DelegationBlockrust-analyzer cargo delegate 0.13.5 DelegationBlock# { |
| 851 | fn parserust-analyzer cargo delegate 0.13.5 impl#[DelegationBlock][Parse]parse().(input: ParseStreamrust-analyzer cargo syn 2.0.110 parse/ParseStream#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo delegate 0.13.5 impl#[DelegationBlock][Parse], Errorrust-analyzer cargo syn 2.0.110 error/Error#> { |
| 852 | let mut segments = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 853 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().input.is_emptyrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]is_empty().() { |
| 854 | segments.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 855 | } |
| 856 | |
| 857 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DelegationBlockrust-analyzer cargo delegate 0.13.5 DelegationBlock# { segmentsrust-analyzer cargo delegate 0.13.5 DelegationBlock#segments. }) |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | /// Returns true if there are any `inline` attributes in the input. |
| 862 | fn has_inline_attributerust-analyzer cargo delegate 0.13.5 has_inline_attribute().(attrs: &[&synrust-analyzer cargo syn 2.0.110 crate/::Attributerust-analyzer cargo syn 2.0.110 attr/Attribute#]) -> bool { |
| 863 | attrs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|attr| { |
| 864 | if let synrust-analyzer cargo syn 2.0.110 crate/::AttrStylerust-analyzer cargo syn 2.0.110 attr/AttrStyle#::Outerrust-analyzer cargo syn 2.0.110 attr/AttrStyle#Outer# = attr.stylerust-analyzer cargo syn 2.0.110 attr/Attribute#style. { |
| 865 | attr.pathrust-analyzer cargo syn 2.0.110 attr/impl#[Attribute]path().().is_identrust-analyzer cargo syn 2.0.110 path/impl#[Path]is_ident().("inline") |
| 866 | } else { |
| 867 | false |
| 868 | } |
| 869 | }) |
| 870 | } |
| 871 | |
| 872 | struct MatchVisitorrust-analyzer cargo delegate 0.13.5 MatchVisitor#<F>(F); |
| 873 | |
| 874 | impl<F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&Exprrust-analyzer cargo syn 2.0.110 expr/Expr#) -> proc_macro2rust-analyzer cargo proc-macro2 1.0.103 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.103 TokenStream#> VisitMutrust-analyzer cargo syn 2.0.110 gen/visit_mut/VisitMut# for MatchVisitorrust-analyzer cargo delegate 0.13.5 MatchVisitor#<F> { |
| 875 | fn visit_arm_mutrust-analyzer cargo delegate 0.13.5 impl#[`MatchVisitor<F>`][VisitMut]visit_arm_mut().(&mut self, arm: &mut synrust-analyzer cargo syn 2.0.110 crate/::Armrust-analyzer cargo syn 2.0.110 expr/Arm#) { |
| 876 | let transformed = self.0rust-analyzer cargo delegate 0.13.5 MatchVisitor#0.(&arm.bodyrust-analyzer cargo syn 2.0.110 expr/Arm#body.); |
| 877 | arm.bodyrust-analyzer cargo syn 2.0.110 expr/Arm#body. = parse_quoterust-analyzer cargo syn 2.0.110 parse_quote/parse_quote!!(#transformed); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | #[proc_macro] |
| 882 | pub fn delegaterust-analyzer cargo delegate 0.13.5 delegate!(tokens: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# { |
| 883 | let block: DelegationBlockrust-analyzer cargo delegate 0.13.5 DelegationBlock# = synrust-analyzer cargo syn 2.0.110 crate/::parse_macro_inputrust-analyzer cargo syn 2.0.110 parse_macro_input/parse_macro_input!!(tokens); |
| 884 | let sections = block.segmentsrust-analyzer cargo delegate 0.13.5 DelegationBlock#segments..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|delegator| { |
| 885 | let delegated_expr = &delegator.delegatorrust-analyzer cargo delegate 0.13.5 DelegatedSegment#delegator.; |
| 886 | let functions = delegator.methodsrust-analyzer cargo delegate 0.13.5 DelegatedSegment#methods..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|method| { |
| 887 | let input = &method.methodrust-analyzer cargo delegate 0.13.5 DelegatedMethod#method.; |
| 888 | let mut signature = input.sigrust-analyzer cargo syn 2.0.110 item/TraitItemFn#sig..clonerust-analyzer cargo syn 2.0.110 gen/clone/impl#[`crate::Signature`][Clone]clone().(); |
| 889 | if let Exprrust-analyzer cargo syn 2.0.110 expr/Expr#::Closurerust-analyzer cargo syn 2.0.110 expr/Expr#Closure#(closure) = delegated_expr { |
| 890 | let additional_inputs: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#> = closure |
| 891 | .inputsrust-analyzer cargo syn 2.0.110 expr/ExprClosure#inputs. |
| 892 | .iterrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]iter().() |
| 893 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|input| { |
| 894 | if let synrust-analyzer cargo syn 2.0.110 crate/::Patrust-analyzer cargo syn 2.0.110 pat/Pat#::Typerust-analyzer cargo syn 2.0.110 pat/Pat#Type#(pat_type) = input { |
| 895 | synrust-analyzer cargo syn 2.0.110 crate/::parse_quoterust-analyzer cargo syn 2.0.110 parse_quote/parse_quote!!(#pat_type) |
| 896 | } else { |
| 897 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!( |
| 898 | "Use a type pattern (`a: u32`) for delegation closure arguments" |
| 899 | ); |
| 900 | } |
| 901 | }) |
| 902 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 903 | let mut origin_inputs = memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/take().(&mut signature.inputsrust-analyzer cargo syn 2.0.110 item/Signature#inputs.).into_iterrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`][IntoIterator]into_iter().(); |
| 904 | // When delegating methods, `first_input` should be self or similar receivers |
| 905 | // Then we need to move it to first |
| 906 | // When delegating associated methods, it may be a trivial argument or does not even exist |
| 907 | // We just keep the origin order. |
| 908 | let first_input = origin_inputs.nextrust-analyzer cargo syn 2.0.110 punctuated/impl#[`IntoIter<T>`][Iterator]next().(); |
| 909 | match first_input { |
| 910 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#::Receiverrust-analyzer cargo syn 2.0.110 item/FnArg#Receiver#(receiver)) => { |
| 911 | signature.inputsrust-analyzer cargo syn 2.0.110 item/Signature#inputs..pushrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push().(FnArgrust-analyzer cargo syn 2.0.110 item/FnArg#::Receiverrust-analyzer cargo syn 2.0.110 item/FnArg#Receiver#(receiver)); |
| 912 | signature.inputsrust-analyzer cargo syn 2.0.110 item/Signature#inputs..extendrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`][`Extend<T>`]extend().(additional_inputs); |
| 913 | } |
| 914 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(first_input) => { |
| 915 | signature.inputsrust-analyzer cargo syn 2.0.110 item/Signature#inputs..extendrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`][`Extend<T>`]extend().(additional_inputs); |
| 916 | signature.inputsrust-analyzer cargo syn 2.0.110 item/Signature#inputs..pushrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push().(first_input); |
| 917 | } |
| 918 | _ => { |
| 919 | signature.inputsrust-analyzer cargo syn 2.0.110 item/Signature#inputs..extendrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`][`Extend<T>`]extend().(additional_inputs); |
| 920 | } |
| 921 | } |
| 922 | signature.inputsrust-analyzer cargo syn 2.0.110 item/Signature#inputs..extendrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`][`Extend<T>`]extend().(origin_inputs); |
| 923 | } |
| 924 | let attributes = parse_method_attributesrust-analyzer cargo delegate 0.13.5 attributes/parse_method_attributes().(&method.attributesrust-analyzer cargo delegate 0.13.5 DelegatedMethod#attributes., input); |
| 925 | let attributes = combine_attributesrust-analyzer cargo delegate 0.13.5 attributes/combine_attributes().(attributes, &delegator.segment_attrsrust-analyzer cargo delegate 0.13.5 DelegatedSegment#segment_attrs.); |
| 926 | if input.defaultrust-analyzer cargo syn 2.0.110 item/TraitItemFn#default..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 927 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!( |
| 928 | "Do not include implementation of delegated functions ({})", |
| 929 | signature.identrust-analyzer cargo syn 2.0.110 item/Signature#ident. |
| 930 | ); |
| 931 | } |
| 932 | |
| 933 | // Generate an argument vector from Punctuated list. |
| 934 | let args: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Exprrust-analyzer cargo syn 2.0.110 expr/Expr#> = method.argumentsrust-analyzer cargo delegate 0.13.5 DelegatedMethod#arguments..clonerust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`][Clone]clone().().into_iterrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 935 | |
| 936 | // Get name (or index) of the target method or field |
| 937 | let name = match &attributes.target_specifierrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#target_specifier. { |
| 938 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(target) => target.get_memberrust-analyzer cargo delegate 0.13.5 attributes/impl#[TargetSpecifier]get_member().(&input.sigrust-analyzer cargo syn 2.0.110 item/TraitItemFn#sig..identrust-analyzer cargo syn 2.0.110 item/Signature#ident.), |
| 939 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => input.sigrust-analyzer cargo syn 2.0.110 item/TraitItemFn#sig..identrust-analyzer cargo syn 2.0.110 item/Signature#ident..clonerust-analyzer cargo proc-macro2 1.0.103 impl#[Ident][Clone]clone().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 940 | }; |
| 941 | |
| 942 | let inline = if has_inline_attributerust-analyzer cargo delegate 0.13.5 has_inline_attribute().(&attributes.attributesrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#attributes.) { |
| 943 | quoterust-analyzer cargo quote 1.0.42 quote!!() |
| 944 | } else { |
| 945 | quoterust-analyzer cargo quote 1.0.42 quote!! { #[inline] } |
| 946 | }; |
| 947 | let visibility = &method.visibilityrust-analyzer cargo delegate 0.13.5 DelegatedMethod#visibility.; |
| 948 | |
| 949 | let is_method = method.methodrust-analyzer cargo delegate 0.13.5 DelegatedMethod#method..sigrust-analyzer cargo syn 2.0.110 item/TraitItemFn#sig..receiverrust-analyzer cargo syn 2.0.110 item/impl#[Signature]receiver().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().(); |
| 950 | let associated_const = &attributes.associated_constantrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#associated_constant.; |
| 951 | let expr_attr = &attributes.expr_attrrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#expr_attr.; |
| 952 | |
| 953 | // Use the body of a closure (like `|k: u32| <body>`) as the delegation expression |
| 954 | let delegated_body = if let Exprrust-analyzer cargo syn 2.0.110 expr/Expr#::Closurerust-analyzer cargo syn 2.0.110 expr/Expr#Closure#(closure) = delegated_expr { |
| 955 | &closure.bodyrust-analyzer cargo syn 2.0.110 expr/ExprClosure#body. |
| 956 | } else { |
| 957 | delegated_expr |
| 958 | }; |
| 959 | |
| 960 | let span = input.spanrust-analyzer cargo syn 2.0.110 spanned/impl#[T][Spanned]span().(); |
| 961 | let generate_await = attributes |
| 962 | .generate_awaitrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#generate_await. |
| 963 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| method.methodrust-analyzer cargo delegate 0.13.5 DelegatedMethod#method..sigrust-analyzer cargo syn 2.0.110 item/TraitItemFn#sig..asyncnessrust-analyzer cargo syn 2.0.110 item/Signature#asyncness..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()); |
| 964 | |
| 965 | // fn method<'a, A, B> -> method::<A, B> |
| 966 | let generic_params = &method.methodrust-analyzer cargo delegate 0.13.5 DelegatedMethod#method..sigrust-analyzer cargo syn 2.0.110 item/TraitItemFn#sig..genericsrust-analyzer cargo syn 2.0.110 item/Signature#generics..paramsrust-analyzer cargo syn 2.0.110 generics/Generics#params.; |
| 967 | let generics = if generic_params.is_emptyrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]is_empty().() { |
| 968 | quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! {} |
| 969 | } else { |
| 970 | let span = generic_params.spanrust-analyzer cargo syn 2.0.110 spanned/impl#[T][Spanned]span().(); |
| 971 | let mut params: synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Punctuated#< |
| 972 | proc_macro2rust-analyzer cargo proc-macro2 1.0.103 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.103 TokenStream#, |
| 973 | synrust-analyzer cargo syn 2.0.110 crate/::Tokenrust-analyzer cargo syn 2.0.110 token/Token!![,], |
| 974 | > = synrust-analyzer cargo syn 2.0.110 crate/::punctuatedrust-analyzer cargo syn 2.0.110 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.110 punctuated/Punctuated#::newrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]new().(); |
| 975 | for param in generic_params.iterrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]iter().() { |
| 976 | let token = match param { |
| 977 | GenericParamrust-analyzer cargo syn 2.0.110 generics/GenericParam#::Lifetimerust-analyzer cargo syn 2.0.110 generics/GenericParam#Lifetime#(_) => { |
| 978 | // Do not pass lifetimes to generic arguments explicitly to avoid |
| 979 | // things like https://doc.rust-lang.org/error_codes/E0794.html |
| 980 | // See https://github.com/Kobzol/rust-delegate/issues/85. |
| 981 | continue; |
| 982 | } |
| 983 | GenericParamrust-analyzer cargo syn 2.0.110 generics/GenericParam#::Typerust-analyzer cargo syn 2.0.110 generics/GenericParam#Type#(t) => { |
| 984 | let token = &t.identrust-analyzer cargo syn 2.0.110 generics/TypeParam#ident.; |
| 985 | let span = t.spanrust-analyzer cargo syn 2.0.110 spanned/impl#[T][Spanned]span().(); |
| 986 | quoterust-analyzer cargo quote 1.0.42 crate/::quote_spannedrust-analyzer cargo quote 1.0.42 quote_spanned!! {span=> #token } |
| 987 | } |
| 988 | GenericParamrust-analyzer cargo syn 2.0.110 generics/GenericParam#::Construst-analyzer cargo syn 2.0.110 generics/GenericParam#Const#(c) => { |
| 989 | let token = &c.identrust-analyzer cargo syn 2.0.110 generics/ConstParam#ident.; |
| 990 | let span = c.spanrust-analyzer cargo syn 2.0.110 spanned/impl#[T][Spanned]span().(); |
| 991 | quoterust-analyzer cargo quote 1.0.42 crate/::quote_spannedrust-analyzer cargo quote 1.0.42 quote_spanned!! {span=> #token } |
| 992 | } |
| 993 | }; |
| 994 | params.pushrust-analyzer cargo syn 2.0.110 punctuated/impl#[`Punctuated<T, P>`]push().(token); |
| 995 | } |
| 996 | quoterust-analyzer cargo quote 1.0.42 crate/::quote_spannedrust-analyzer cargo quote 1.0.42 quote_spanned!! {span=> ::<#params> } |
| 997 | }; |
| 998 | |
| 999 | let modify_expr = |expr: &Exprrust-analyzer cargo syn 2.0.110 expr/Expr#| { |
| 1000 | let body = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(target_trait) = &attributes.target_traitrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#target_trait. { |
| 1001 | quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { #target_trait::#name#generics(#expr, #(#args),*) } |
| 1002 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(AssociatedConstantrust-analyzer cargo delegate 0.13.5 attributes/AssociatedConstant# { |
| 1003 | const_namerust-analyzer cargo delegate 0.13.5 attributes/AssociatedConstant#const_name., |
| 1004 | trait_pathrust-analyzer cargo delegate 0.13.5 attributes/AssociatedConstant#trait_path., |
| 1005 | }) = associated_const |
| 1006 | { |
| 1007 | let return_type = &signature.outputrust-analyzer cargo syn 2.0.110 item/Signature#output.; |
| 1008 | quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! {{ |
| 1009 | const fn get_const<T: #trait_path>(t: &T) #return_type { |
| 1010 | <T as #trait_path>::#const_name |
| 1011 | } |
| 1012 | get_const(#expr) |
| 1013 | }} |
| 1014 | } else if is_method { |
| 1015 | match &attributes.target_specifierrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#target_specifier. { |
| 1016 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(TargetSpecifierrust-analyzer cargo delegate 0.13.5 attributes/TargetSpecifier#::Methodrust-analyzer cargo delegate 0.13.5 attributes/TargetSpecifier#Method#(_)) => { |
| 1017 | quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { #expr.#name#generics(#(#args),*) } |
| 1018 | } |
| 1019 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(TargetSpecifierrust-analyzer cargo delegate 0.13.5 attributes/TargetSpecifier#::Fieldrust-analyzer cargo delegate 0.13.5 attributes/TargetSpecifier#Field#(target)) => { |
| 1020 | let reference = target.reference_tokensrust-analyzer cargo delegate 0.13.5 attributes/impl#[GetFieldAttribute]reference_tokens().(); |
| 1021 | quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { #reference#expr.#name } |
| 1022 | } |
| 1023 | } |
| 1024 | } else { |
| 1025 | quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { #expr::#name#generics(#(#args),*) } |
| 1026 | }; |
| 1027 | |
| 1028 | let mut body = if generate_await { |
| 1029 | quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { #body.await } |
| 1030 | } else { |
| 1031 | body |
| 1032 | }; |
| 1033 | |
| 1034 | for expression in &attributes.expressionsrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#expressions. { |
| 1035 | match expression { |
| 1036 | ReturnExpressionrust-analyzer cargo delegate 0.13.5 attributes/ReturnExpression#::Intorust-analyzer cargo delegate 0.13.5 attributes/ReturnExpression#Into#(type_name) => { |
| 1037 | body = match type_name { |
| 1038 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name) => { |
| 1039 | quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { ::core::convert::Into::<#name>::into(#body) } |
| 1040 | } |
| 1041 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { ::core::convert::Into::into(#body) }, |
| 1042 | }; |
| 1043 | } |
| 1044 | ReturnExpressionrust-analyzer cargo delegate 0.13.5 attributes/ReturnExpression#::TryIntorust-analyzer cargo delegate 0.13.5 attributes/ReturnExpression#TryInto# => { |
| 1045 | body = quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { ::core::convert::TryInto::try_into(#body) }; |
| 1046 | } |
| 1047 | ReturnExpressionrust-analyzer cargo delegate 0.13.5 attributes/ReturnExpression#::Unwraprust-analyzer cargo delegate 0.13.5 attributes/ReturnExpression#Unwrap# => { |
| 1048 | body = quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { #body.unwrap() }; |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | body |
| 1053 | }; |
| 1054 | let mut body = if let Exprrust-analyzer cargo syn 2.0.110 expr/Expr#::Matchrust-analyzer cargo syn 2.0.110 expr/Expr#Match#(expr_match) = delegated_body { |
| 1055 | let mut expr_match = expr_match.clonerust-analyzer cargo syn 2.0.110 gen/clone/impl#[`crate::ExprMatch`][Clone]clone().(); |
| 1056 | MatchVisitorrust-analyzer cargo delegate 0.13.5 MatchVisitor#(modify_expr).visit_expr_match_mutrust-analyzer cargo syn 2.0.110 gen/visit_mut/VisitMut#visit_expr_match_mut().(&mut expr_match); |
| 1057 | expr_match.into_token_streamrust-analyzer cargo quote 1.0.42 to_tokens/ToTokens#into_token_stream().() |
| 1058 | } else { |
| 1059 | modify_expr(delegated_body) |
| 1060 | }; |
| 1061 | |
| 1062 | if let synrust-analyzer cargo syn 2.0.110 crate/::ReturnTyperust-analyzer cargo syn 2.0.110 ty/ReturnType#::Defaultrust-analyzer cargo syn 2.0.110 ty/ReturnType#Default# = &signature.outputrust-analyzer cargo syn 2.0.110 item/Signature#output. { |
| 1063 | body = quoterust-analyzer cargo quote 1.0.42 crate/::quoterust-analyzer cargo quote 1.0.42 quote!! { #body; }; |
| 1064 | }; |
| 1065 | |
| 1066 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(expr_template) = expr_attr { |
| 1067 | body = expr_template.expand_templaterust-analyzer cargo delegate 0.13.5 attributes/impl#[TemplateExpr]expand_template().(&body); |
| 1068 | } |
| 1069 | |
| 1070 | let attrs = &attributes.attributesrust-analyzer cargo delegate 0.13.5 attributes/MethodAttributes#attributes.; |
| 1071 | quoterust-analyzer cargo quote 1.0.42 crate/::quote_spannedrust-analyzer cargo quote 1.0.42 quote_spanned!! {span=> |
| 1072 | #(#attrs)* |
| 1073 | #inline |
| 1074 | #visibility #signature { |
| 1075 | #body |
| 1076 | } |
| 1077 | } |
| 1078 | }); |
| 1079 | |
| 1080 | quoterust-analyzer cargo quote 1.0.42 quote!! { #(#functions)* } |
| 1081 | }); |
| 1082 | |
| 1083 | let result = quoterust-analyzer cargo quote 1.0.42 quote!! { |
| 1084 | #(#sections)* |
| 1085 | }; |
| 1086 | result.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 1087 | } |
| 1088 | |
| 1089 | // we cannot use `Attributes::parse_outer` directly, because it does not allow keywords to appear |
| 1090 | // in meta path positions, i.e., it does not accept `#[await(true)]`. |
| 1091 | // related issue: https://github.com/dtolnay/syn/issues/1458 |
| 1092 | fn tolerant_outer_attributesrust-analyzer cargo delegate 0.13.5 tolerant_outer_attributes().(input: ParseStreamrust-analyzer cargo syn 2.0.110 parse/ParseStream#) -> synrust-analyzer cargo syn 2.0.110 crate/::Resultrust-analyzer cargo syn 2.0.110 error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<synrust-analyzer cargo syn 2.0.110 crate/::Attributerust-analyzer cargo syn 2.0.110 attr/Attribute#>> { |
| 1093 | use proc_macro2rust-analyzer cargo proc-macro2 1.0.103 crate/::{Delimiterrust-analyzer cargo proc-macro2 1.0.103 Delimiter#, TokenTreerust-analyzer cargo proc-macro2 1.0.103 TokenTree#}; |
| 1094 | use synrust-analyzer cargo syn 2.0.110 crate/::{ |
| 1095 | bracketedrust-analyzer cargo syn 2.0.110 group/bracketed!, |
| 1096 | extrust-analyzer cargo syn 2.0.110 ext/::IdentExtrust-analyzer cargo syn 2.0.110 ext/IdentExt#, |
| 1097 | parserust-analyzer cargo syn 2.0.110 parse/::discouragedrust-analyzer cargo syn 2.0.110 parse/discouraged/::Speculativerust-analyzer cargo syn 2.0.110 parse/discouraged/Speculative#, |
| 1098 | tokenrust-analyzer cargo syn 2.0.110 token/::{Bracerust-analyzer cargo syn 2.0.110 token/Brace#, Bracketrust-analyzer cargo syn 2.0.110 token/Bracket#, Parenrust-analyzer cargo syn 2.0.110 token/Paren#}, |
| 1099 | AttrStylerust-analyzer cargo syn 2.0.110 attr/AttrStyle#, Attributerust-analyzer cargo syn 2.0.110 attr/Attribute#, ExprLitrust-analyzer cargo syn 2.0.110 expr/ExprLit#, Litrust-analyzer cargo syn 2.0.110 lit/Lit#, MacroDelimiterrust-analyzer cargo syn 2.0.110 mac/MacroDelimiter#, MetaListrust-analyzer cargo syn 2.0.110 attr/MetaList#, MetaNameValuerust-analyzer cargo syn 2.0.110 attr/MetaNameValue#, Pathrust-analyzer cargo syn 2.0.110 path/Path#, Resultrust-analyzer cargo syn 2.0.110 error/Result#, |
| 1100 | Tokenrust-analyzer cargo syn 2.0.110 token/Token!, |
| 1101 | }; |
| 1102 | |
| 1103 | fn tolerant_attrrust-analyzer cargo delegate 0.13.5 tolerant_attr().(input: ParseStreamrust-analyzer cargo syn 2.0.110 parse/ParseStream#) -> Resultrust-analyzer cargo syn 2.0.110 error/Result#<Attributerust-analyzer cargo syn 2.0.110 attr/Attribute#> { |
| 1104 | let content; |
| 1105 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Attributerust-analyzer cargo syn 2.0.110 attr/Attribute# { |
| 1106 | pound_tokenrust-analyzer cargo syn 2.0.110 attr/Attribute#pound_token.: input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 1107 | stylerust-analyzer cargo syn 2.0.110 attr/Attribute#style.: AttrStylerust-analyzer cargo syn 2.0.110 attr/AttrStyle#::Outerrust-analyzer cargo syn 2.0.110 attr/AttrStyle#Outer#, |
| 1108 | bracket_tokenrust-analyzer cargo syn 2.0.110 attr/Attribute#bracket_token.: bracketedrust-analyzer cargo syn 2.0.110 group/bracketed!!(content in input), |
| 1109 | metarust-analyzer cargo syn 2.0.110 attr/Attribute#meta.: content.callrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]call().(tolerant_metarust-analyzer cargo delegate 0.13.5 tolerant_meta().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 1110 | }) |
| 1111 | } |
| 1112 | |
| 1113 | // adapted from `impl Parse for Meta` |
| 1114 | fn tolerant_metarust-analyzer cargo delegate 0.13.5 tolerant_meta().(input: ParseStreamrust-analyzer cargo syn 2.0.110 parse/ParseStream#) -> Resultrust-analyzer cargo syn 2.0.110 error/Result#<Metarust-analyzer cargo syn 2.0.110 attr/Meta#> { |
| 1115 | // Try to parse as Meta |
| 1116 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(meta) = input.callrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]call().(Metarust-analyzer cargo syn 2.0.110 attr/Meta#::parserust-analyzer cargo syn 2.0.110 attr/parsing/impl#[Meta][Parse]parse().) { |
| 1117 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(meta) |
| 1118 | } else { |
| 1119 | // If it's not possible, try to parse it as any identifier, to support #[await] |
| 1120 | let path = Pathrust-analyzer cargo syn 2.0.110 path/Path#::fromrust-analyzer cargo syn 2.0.110 path/impl#[Path][`From<T>`]from().(input.callrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]call().(Identrust-analyzer cargo proc-macro2 1.0.103 Ident#::parse_anyrust-analyzer cargo syn 2.0.110 ext/impl#[Ident][IdentExt]parse_any().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 1121 | if input.peekrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]peek().(Parenrust-analyzer cargo syn 2.0.110 token/Paren().) || input.peekrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]peek().(Bracketrust-analyzer cargo syn 2.0.110 token/Bracket().) || input.peekrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]peek().(Bracerust-analyzer cargo syn 2.0.110 token/Brace().) { |
| 1122 | // adapted from the private `syn::attr::parse_meta_after_path` |
| 1123 | input.steprust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]step().(|cursor| { |
| 1124 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((TokenTreerust-analyzer cargo proc-macro2 1.0.103 TokenTree#::Grouprust-analyzer cargo proc-macro2 1.0.103 TokenTree#Group#(g), rest)) = cursor.token_treerust-analyzer cargo syn 2.0.110 buffer/impl#[`Cursor<'a>`]token_tree().() { |
| 1125 | let span = g.delim_spanrust-analyzer cargo proc-macro2 1.0.103 impl#[Group]delim_span().(); |
| 1126 | let delimiter = match g.delimiterrust-analyzer cargo proc-macro2 1.0.103 impl#[Group]delimiter().() { |
| 1127 | Delimiterrust-analyzer cargo proc-macro2 1.0.103 Delimiter#::Parenthesisrust-analyzer cargo proc-macro2 1.0.103 Delimiter#Parenthesis# => MacroDelimiterrust-analyzer cargo syn 2.0.110 mac/MacroDelimiter#::Parenrust-analyzer cargo syn 2.0.110 mac/MacroDelimiter#Paren#(Parenrust-analyzer cargo syn 2.0.110 token/Paren().(span)), |
| 1128 | Delimiterrust-analyzer cargo proc-macro2 1.0.103 Delimiter#::Bracerust-analyzer cargo proc-macro2 1.0.103 Delimiter#Brace# => MacroDelimiterrust-analyzer cargo syn 2.0.110 mac/MacroDelimiter#::Bracerust-analyzer cargo syn 2.0.110 mac/MacroDelimiter#Brace#(Bracerust-analyzer cargo syn 2.0.110 token/Brace().(span)), |
| 1129 | Delimiterrust-analyzer cargo proc-macro2 1.0.103 Delimiter#::Bracketrust-analyzer cargo proc-macro2 1.0.103 Delimiter#Bracket# => MacroDelimiterrust-analyzer cargo syn 2.0.110 mac/MacroDelimiter#::Bracketrust-analyzer cargo syn 2.0.110 mac/MacroDelimiter#Bracket#(Bracketrust-analyzer cargo syn 2.0.110 token/Bracket().(span)), |
| 1130 | Delimiterrust-analyzer cargo proc-macro2 1.0.103 Delimiter#::Nonerust-analyzer cargo proc-macro2 1.0.103 Delimiter#None# => { |
| 1131 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(cursor.errorrust-analyzer cargo syn 2.0.110 parse/impl#[`StepCursor<'c, 'a>`]error().("expected delimiter")); |
| 1132 | } |
| 1133 | }; |
| 1134 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(( |
| 1135 | Metarust-analyzer cargo syn 2.0.110 attr/Meta#::Listrust-analyzer cargo syn 2.0.110 attr/Meta#List#(MetaListrust-analyzer cargo syn 2.0.110 attr/MetaList# { |
| 1136 | pathrust-analyzer cargo syn 2.0.110 attr/MetaList#path., |
| 1137 | delimiterrust-analyzer cargo syn 2.0.110 attr/MetaList#delimiter., |
| 1138 | tokensrust-analyzer cargo syn 2.0.110 attr/MetaList#tokens.: g.streamrust-analyzer cargo proc-macro2 1.0.103 impl#[Group]stream().(), |
| 1139 | }), |
| 1140 | rest, |
| 1141 | )) |
| 1142 | } else { |
| 1143 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(cursor.errorrust-analyzer cargo syn 2.0.110 parse/impl#[`StepCursor<'c, 'a>`]error().("expected delimiter")) |
| 1144 | } |
| 1145 | }) |
| 1146 | } else if input.peekrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]peek().(Tokenrust-analyzer cargo syn 2.0.110 token/Token!![=]) { |
| 1147 | // adapted from the private `syn::attr::parse_meta_name_value_after_path` |
| 1148 | let eq_token = input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 1149 | let ahead = input.forkrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]fork().(); |
| 1150 | let value = match ahead.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().::<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Litrust-analyzer cargo syn 2.0.110 lit/Lit#>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 1151 | // this branch is probably for speeding up the parsing for doc comments etc. |
| 1152 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(lit) if ahead.is_emptyrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]is_empty().() => { |
| 1153 | input.advance_torust-analyzer cargo syn 2.0.110 parse/discouraged/impl#[`ParseBuffer<'a>`][Speculative]advance_to().(&ahead); |
| 1154 | Exprrust-analyzer cargo syn 2.0.110 expr/Expr#::Litrust-analyzer cargo syn 2.0.110 expr/Expr#Lit#(ExprLitrust-analyzer cargo syn 2.0.110 expr/ExprLit# { |
| 1155 | attrsrust-analyzer cargo syn 2.0.110 expr/ExprLit#attrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 1156 | litrust-analyzer cargo syn 2.0.110 expr/ExprLit#lit., |
| 1157 | }) |
| 1158 | } |
| 1159 | _ => input.parserust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 1160 | }; |
| 1161 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Metarust-analyzer cargo syn 2.0.110 attr/Meta#::NameValuerust-analyzer cargo syn 2.0.110 attr/Meta#NameValue#(MetaNameValuerust-analyzer cargo syn 2.0.110 attr/MetaNameValue# { |
| 1162 | pathrust-analyzer cargo syn 2.0.110 attr/MetaNameValue#path., |
| 1163 | eq_tokenrust-analyzer cargo syn 2.0.110 attr/MetaNameValue#eq_token., |
| 1164 | valuerust-analyzer cargo syn 2.0.110 attr/MetaNameValue#value., |
| 1165 | })) |
| 1166 | } else { |
| 1167 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Metarust-analyzer cargo syn 2.0.110 attr/Meta#::Pathrust-analyzer cargo syn 2.0.110 attr/Meta#Path#(path)) |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | let mut attrs = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 1173 | while input.peekrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]peek().(Tokenrust-analyzer cargo syn 2.0.110 token/Token!![#]) { |
| 1174 | attrs.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(input.callrust-analyzer cargo syn 2.0.110 parse/impl#[`ParseBuffer<'a>`]call().(tolerant_attrrust-analyzer cargo delegate 0.13.5 tolerant_attr().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 1175 | } |
| 1176 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(attrs) |
| 1177 | } |