derive_more_impl/
index.rs

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