| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#; |
| 2 | |
| 3 | use quoterust-analyzer cargo quote 1.0.47 crate/::quoterust-analyzer cargo quote 1.0.47 quote!; |
| 4 | use synrust-analyzer cargo syn 2.0.119 crate/::{Datarust-analyzer cargo syn 2.0.119 derive/Data#, DeriveInputrust-analyzer cargo syn 2.0.119 derive/DeriveInput#, Fieldrust-analyzer cargo syn 2.0.119 data/Field#, Metarust-analyzer cargo syn 2.0.119 attr/Meta#, Pathrust-analyzer cargo syn 2.0.119 path/Path#, Typerust-analyzer cargo syn 2.0.119 ty/Type#}; |
| 5 | |
| 6 | use superrust-analyzer cargo educe 0.6.0 trait_handlers/into/::{ |
| 7 | modelsrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/::{FieldAttributerust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/FieldAttribute#, FieldAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/FieldAttributeBuilder#, TypeAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/type_attribute/TypeAttributeBuilder#}, |
| 8 | TraitHandlerMultiplerust-analyzer cargo educe 0.6.0 trait_handlers/TraitHandlerMultiple#, |
| 9 | }; |
| 10 | use craterust-analyzer cargo educe 0.6.0 crate/::{commonrust-analyzer cargo educe 0.6.0 common/::ident_indexrust-analyzer cargo educe 0.6.0 common/ident_index/::IdentOrIndexrust-analyzer cargo educe 0.6.0 common/ident_index/IdentOrIndex#, Traitrust-analyzer cargo educe 0.6.0 supported_traits/Trait#}; |
| 11 | |
| 12 | pub(craterust-analyzer cargo educe 0.6.0 crate/) struct IntoStructHandlerrust-analyzer cargo educe 0.6.0 trait_handlers/into/into_struct/IntoStructHandler#; |
| 13 | |
| 14 | impl TraitHandlerMultiplerust-analyzer cargo educe 0.6.0 trait_handlers/TraitHandlerMultiple# for IntoStructHandlerrust-analyzer cargo educe 0.6.0 trait_handlers/into/into_struct/IntoStructHandler# { |
| 15 | #[inline] |
| 16 | fn trait_meta_handlerrust-analyzer cargo educe 0.6.0 trait_handlers/into/into_struct/impl#[IntoStructHandler][TraitHandlerMultiple]trait_meta_handler().( |
| 17 | ast: &DeriveInputrust-analyzer cargo syn 2.0.119 derive/DeriveInput#, |
| 18 | token_stream: &mut proc_macro2rust-analyzer cargo proc-macro2 1.0.107 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#, |
| 19 | traits: &[Traitrust-analyzer cargo educe 0.6.0 supported_traits/Trait#], |
| 20 | meta: &[Metarust-analyzer cargo syn 2.0.119 attr/Meta#], |
| 21 | ) -> synrust-analyzer cargo syn 2.0.119 crate/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<()> { |
| 22 | let type_attribute = TypeAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/type_attribute/TypeAttributeBuilder# { |
| 23 | enable_typesrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/type_attribute/TypeAttributeBuilder#enable_types.: true |
| 24 | } |
| 25 | .build_from_into_metarust-analyzer cargo educe 0.6.0 trait_handlers/into/models/type_attribute/impl#[TypeAttributeBuilder]build_from_into_meta().(meta)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 26 | |
| 27 | if let Datarust-analyzer cargo syn 2.0.119 derive/Data#::Structrust-analyzer cargo syn 2.0.119 derive/Data#Struct#(data) = &ast.datarust-analyzer cargo syn 2.0.119 derive/DeriveInput#data. { |
| 28 | let fields = &data.fieldsrust-analyzer cargo syn 2.0.119 derive/DataStruct#fields.; |
| 29 | |
| 30 | let field_attributes: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<usize, FieldAttributerust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/FieldAttribute#> = { |
| 31 | let mut map = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().(); |
| 32 | |
| 33 | for (index, field) in fields.iterrust-analyzer cargo syn 2.0.119 data/impl#[Fields]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 34 | let field_attribute = FieldAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/FieldAttributeBuilder# { |
| 35 | enable_typesrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/FieldAttributeBuilder#enable_types.: true |
| 36 | } |
| 37 | .build_from_attributesrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/impl#[FieldAttributeBuilder]build_from_attributes().(&field.attrsrust-analyzer cargo syn 2.0.119 data/Field#attrs., traits)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 38 | |
| 39 | for ty in field_attribute.typesrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/FieldAttribute#types..keysrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]keys().() { |
| 40 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().type_attribute.typesrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/type_attribute/TypeAttribute#types..contains_keyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]contains_key().(ty) { |
| 41 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(superrust-analyzer cargo educe 0.6.0 trait_handlers/into/::panicrust-analyzer cargo educe 0.6.0 trait_handlers/into/panic/::no_into_implrust-analyzer cargo educe 0.6.0 trait_handlers/into/panic/no_into_impl().(ty)); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | map.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(index, field_attribute); |
| 46 | } |
| 47 | |
| 48 | map |
| 49 | }; |
| 50 | |
| 51 | for (target_ty, bound) in type_attribute.typesrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/type_attribute/TypeAttribute#types. { |
| 52 | let mut into_types: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&Typerust-analyzer cargo syn 2.0.119 ty/Type#> = 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().(); |
| 53 | |
| 54 | let mut into_token_stream = proc_macro2rust-analyzer cargo proc-macro2 1.0.107 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#::newrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream]new().(); |
| 55 | |
| 56 | let (index, field, method) = { |
| 57 | let fields = &data.fieldsrust-analyzer cargo syn 2.0.119 derive/DataStruct#fields.; |
| 58 | |
| 59 | if fields.lenrust-analyzer cargo syn 2.0.119 data/impl#[Fields]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 { |
| 60 | let field = fields.into_iterrust-analyzer cargo syn 2.0.119 data/impl#[`&'a Fields`][IntoIterator]into_iter().().nextrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Iter<'a, T>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 61 | |
| 62 | let method = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(field_attribute) = field_attributes.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&0) { |
| 63 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(method) = field_attribute.typesrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/FieldAttribute#types..getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&target_ty) { |
| 64 | method.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().() |
| 65 | } else { |
| 66 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 67 | } |
| 68 | } else { |
| 69 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 70 | }; |
| 71 | |
| 72 | (0usize, field, method) |
| 73 | } else { |
| 74 | let mut into_field: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(usize, &Fieldrust-analyzer cargo syn 2.0.119 data/Field#, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Pathrust-analyzer cargo syn 2.0.119 path/Path#>)> = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 75 | |
| 76 | for (index, field) in fields.iterrust-analyzer cargo syn 2.0.119 data/impl#[Fields]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 77 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(field_attribute) = field_attributes.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&index) { |
| 78 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((key, method)) = |
| 79 | field_attribute.typesrust-analyzer cargo educe 0.6.0 trait_handlers/into/models/field_attribute/FieldAttribute#types..get_key_valuerust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get_key_value().(&target_ty) |
| 80 | { |
| 81 | if into_field.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 82 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(superrust-analyzer cargo educe 0.6.0 trait_handlers/into/::panicrust-analyzer cargo educe 0.6.0 trait_handlers/into/panic/::multiple_into_fieldsrust-analyzer cargo educe 0.6.0 trait_handlers/into/panic/multiple_into_fields().(key)); |
| 83 | } |
| 84 | |
| 85 | into_field = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((index, field, method.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().())); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if into_field.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { |
| 91 | // search the same type |
| 92 | for (index, field) in fields.iterrust-analyzer cargo syn 2.0.119 data/impl#[Fields]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 93 | let field_ty = superrust-analyzer cargo educe 0.6.0 trait_handlers/into/::commonrust-analyzer cargo educe 0.6.0 trait_handlers/into/common/::to_hash_typerust-analyzer cargo educe 0.6.0 trait_handlers/into/common/to_hash_type().(&field.tyrust-analyzer cargo syn 2.0.119 data/Field#ty.); |
| 94 | |
| 95 | if target_ty.eqrust-analyzer cargo educe 0.6.0 common/tools/hash_type/impl#[HashType][`PartialEq<Self>`]eq().(&field_ty) { |
| 96 | if into_field.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 97 | // multiple candidates |
| 98 | into_field = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 99 | |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | into_field = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((index, field, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(into_field) = into_field { |
| 109 | into_field |
| 110 | } else { |
| 111 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(superrust-analyzer cargo educe 0.6.0 trait_handlers/into/::panicrust-analyzer cargo educe 0.6.0 trait_handlers/into/panic/::no_into_fieldrust-analyzer cargo educe 0.6.0 trait_handlers/into/panic/no_into_field().(&target_ty)); |
| 112 | } |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | let field_name = IdentOrIndexrust-analyzer cargo educe 0.6.0 common/ident_index/IdentOrIndex#::from_ident_with_indexrust-analyzer cargo educe 0.6.0 common/ident_index/impl#[IdentOrIndex]from_ident_with_index().(field.identrust-analyzer cargo syn 2.0.119 data/Field#ident..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().(), index); |
| 117 | |
| 118 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(method) = method { |
| 119 | into_token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!( #method(self.#field_name) )); |
| 120 | } else { |
| 121 | let ty = &field.tyrust-analyzer cargo syn 2.0.119 data/Field#ty.; |
| 122 | |
| 123 | let field_ty = superrust-analyzer cargo educe 0.6.0 trait_handlers/into/::commonrust-analyzer cargo educe 0.6.0 trait_handlers/into/common/::to_hash_typerust-analyzer cargo educe 0.6.0 trait_handlers/into/common/to_hash_type().(ty); |
| 124 | |
| 125 | if target_ty.eqrust-analyzer cargo educe 0.6.0 common/tools/hash_type/impl#[HashType][`PartialEq<Self>`]eq().(&field_ty) { |
| 126 | into_token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!( self.#field_name )); |
| 127 | } else { |
| 128 | into_types.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ty); |
| 129 | |
| 130 | into_token_stream |
| 131 | .extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!( ::core::convert::Into::into(self.#field_name) )); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | let ident = &ast.identrust-analyzer cargo syn 2.0.119 derive/DeriveInput#ident.; |
| 136 | |
| 137 | let bound = bound.into_where_predicates_by_generic_parameters_check_typesrust-analyzer cargo educe 0.6.0 common/bound/impl#[Bound]into_where_predicates_by_generic_parameters_check_types().( |
| 138 | &ast.genericsrust-analyzer cargo syn 2.0.119 derive/DeriveInput#generics..paramsrust-analyzer cargo syn 2.0.119 generics/Generics#params., |
| 139 | &synrust-analyzer cargo syn 2.0.119 crate/::parse2rust-analyzer cargo syn 2.0.119 parse2().(quoterust-analyzer cargo quote 1.0.47 quote!!(::core::convert::Into<#target_ty>)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 140 | &into_types, |
| 141 | &[], |
| 142 | ); |
| 143 | |
| 144 | // clone generics in order to not to affect other Into<T> implementations |
| 145 | let mut generics = ast.genericsrust-analyzer cargo syn 2.0.119 derive/DeriveInput#generics..clonerust-analyzer cargo syn 2.0.119 gen/clone/impl#[`crate::Generics`][Clone]clone().(); |
| 146 | |
| 147 | let where_clause = generics.make_where_clauserust-analyzer cargo syn 2.0.119 generics/impl#[Generics]make_where_clause().(); |
| 148 | |
| 149 | for where_predicate in bound { |
| 150 | where_clause.predicatesrust-analyzer cargo syn 2.0.119 generics/WhereClause#predicates..pushrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]push().(where_predicate); |
| 151 | } |
| 152 | |
| 153 | let (impl_generics, ty_generics, _) = ast.genericsrust-analyzer cargo syn 2.0.119 derive/DeriveInput#generics..split_for_implrust-analyzer cargo syn 2.0.119 generics/impl#[Generics]split_for_impl().(); |
| 154 | |
| 155 | token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!! { |
| 156 | impl #impl_generics ::core::convert::Into<#target_ty> for #ident #ty_generics #where_clause { |
| 157 | #[inline] |
| 158 | fn into(self) -> #target_ty { |
| 159 | #into_token_stream |
| 160 | } |
| 161 | } |
| 162 | }); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 167 | } |
| 168 | } |