1 | use proc_macro2::TokenStream; |
2 | use quote::quote; |
3 | use syn::{Field, Ident, Index}; |
4 | |
5 | pub fn tuple_exprs(fields: &[&Field], method_ident: &Ident) -> Vec<TokenStream> { |
6 | let mut exprs: Vec = vec![]; |
7 | |
8 | for i: usize in 0..fields.len() { |
9 | let i: Index = Index::from(i); |
10 | // generates `self.0.add(rhs.0)` |
11 | let expr: TokenStream = quote!(self.#i.#method_ident(rhs.#i)); |
12 | exprs.push(expr); |
13 | } |
14 | exprs |
15 | } |
16 | |
17 | pub fn struct_exprs(fields: &[&Field], method_ident: &Ident) -> Vec<TokenStream> { |
18 | let mut exprs: Vec = vec![]; |
19 | |
20 | for field: &&Field in fields { |
21 | // It's safe to unwrap because struct fields always have an identifier |
22 | let field_id: &Ident = field.ident.as_ref().unwrap(); |
23 | // generates `x: self.x.add(rhs.x)` |
24 | let expr: TokenStream = quote!(self.#field_id.#method_ident(rhs.#field_id)); |
25 | exprs.push(expr) |
26 | } |
27 | exprs |
28 | } |
29 | |