| 1 |
use crate::utils::{add_where_clauses_for_new_ident, SingleFieldData, State}; |
| 2 |
use proc_macro2::TokenStream; |
| 3 |
use quote::{format_ident, quote}; |
| 4 |
use syn::{parse::Result, DeriveInput}; |
| 5 |
|
| 6 |
/// Provides the hook to expand `#[derive(IndexMut)]` into an implementation of `IndexMut` |
| 7 |
pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { |
| 8 |
let index_type = format_ident!("__IdxT"); |
| 9 |
let mut state = State::with_field_ignore(input, trait_name, "index_mut".into())?; |
| 10 |
state.add_trait_path_type_param(quote! { #index_type }); |
| 11 |
let SingleFieldData { |
| 12 |
field, |
| 13 |
field_type, |
| 14 |
input_type, |
| 15 |
trait_path_with_params, |
| 16 |
casted_trait, |
| 17 |
member, |
| 18 |
.. |
| 19 |
} = state.assert_single_enabled_field(); |
| 20 |
|
| 21 |
let type_where_clauses = quote! { |
| 22 |
where #field_type: #trait_path_with_params |
| 23 |
}; |
| 24 |
|
| 25 |
let new_generics = add_where_clauses_for_new_ident( |
| 26 |
&input.generics, |
| 27 |
&[field], |
| 28 |
&index_type, |
| 29 |
type_where_clauses, |
| 30 |
true, |
| 31 |
); |
| 32 |
|
| 33 |
let (impl_generics, _, where_clause) = new_generics.split_for_impl(); |
| 34 |
let (_, ty_generics, _) = input.generics.split_for_impl(); |
| 35 |
Ok(quote! { |
| 36 |
#[automatically_derived] |
| 37 |
impl #impl_generics #trait_path_with_params for #input_type #ty_generics #where_clause { |
| 38 |
#[inline] |
| 39 |
fn index_mut(&mut self, idx: #index_type) -> &mut Self::Output { |
| 40 |
#casted_trait::index_mut(&mut #member, idx) |
| 41 |
} |
| 42 |
} |
| 43 |
}) |
| 44 |
} |