Debian Codegraph @

src/trait_handlers/deref/deref_enum.rs

raw symbols SCIP
1 use quoterust-analyzer cargo quote 1.0.47 crate/::{format_identrust-analyzer cargo quote 1.0.47 format/format_ident!, quoterust-analyzer cargo quote 1.0.47 quote!};
2 use synrust-analyzer cargo syn 2.0.119 crate/::{spannedrust-analyzer cargo syn 2.0.119 spanned/::Spannedrust-analyzer cargo syn 2.0.119 spanned/Spanned#, 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#, Fieldsrust-analyzer cargo syn 2.0.119 data/Fields#, Identrust-analyzer cargo proc-macro2 1.0.107 Ident#, Metarust-analyzer cargo syn 2.0.119 attr/Meta#, Typerust-analyzer cargo syn 2.0.119 ty/Type#};
3
4 use superrust-analyzer cargo educe 0.6.0 trait_handlers/deref/::{
5 modelsrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/::{FieldAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/field_attribute/FieldAttributeBuilder#, TypeAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/type_attribute/TypeAttributeBuilder#},
6 TraitHandlerrust-analyzer cargo educe 0.6.0 trait_handlers/TraitHandler#,
7 };
8 use craterust-analyzer cargo educe 0.6.0 crate/::{commonrust-analyzer cargo educe 0.6.0 common/::r#typerust-analyzer cargo educe 0.6.0 common/`r#type`/::dereferencerust-analyzer cargo educe 0.6.0 common/`r#type`/dereference()., panicrust-analyzer cargo educe 0.6.0 panic/, supported_traitsrust-analyzer cargo educe 0.6.0 supported_traits/::Traitrust-analyzer cargo educe 0.6.0 supported_traits/Trait#};
9
10 pub(craterust-analyzer cargo educe 0.6.0 crate/) struct DerefEnumHandlerrust-analyzer cargo educe 0.6.0 trait_handlers/deref/deref_enum/DerefEnumHandler#;
11
12 impl TraitHandlerrust-analyzer cargo educe 0.6.0 trait_handlers/TraitHandler# for DerefEnumHandlerrust-analyzer cargo educe 0.6.0 trait_handlers/deref/deref_enum/DerefEnumHandler# {
13 #[inline]
14 fn trait_meta_handlerrust-analyzer cargo educe 0.6.0 trait_handlers/deref/deref_enum/impl#[DerefEnumHandler][TraitHandler]trait_meta_handler().(
15 ast: &DeriveInputrust-analyzer cargo syn 2.0.119 derive/DeriveInput#,
16 token_stream: &mut proc_macro2rust-analyzer cargo proc-macro2 1.0.107 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#,
17 traits: &[Traitrust-analyzer cargo educe 0.6.0 supported_traits/Trait#],
18 meta: &Metarust-analyzer cargo syn 2.0.119 attr/Meta#,
19 ) -> synrust-analyzer cargo syn 2.0.119 crate/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<()> {
20 let _ = TypeAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/type_attribute/TypeAttributeBuilder# {
21 enable_flagrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/type_attribute/TypeAttributeBuilder#enable_flag.: true
22 }
23 .build_from_deref_metarust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/type_attribute/impl#[TypeAttributeBuilder]build_from_deref_meta().(meta)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
24
25 let mut target_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().();
26 let mut arms_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().();
27
28 if let Datarust-analyzer cargo syn 2.0.119 derive/Data#::Enumrust-analyzer cargo syn 2.0.119 derive/Data#Enum#(data) = &ast.datarust-analyzer cargo syn 2.0.119 derive/DeriveInput#data. {
29 type Variantsrust-analyzer cargo educe 0.6.0 trait_handlers/deref/deref_enum/Variants#<'a> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(&'a Identrust-analyzer cargo proc-macro2 1.0.107 Ident#, bool, usize, Identrust-analyzer cargo proc-macro2 1.0.107 Ident#, &'a Typerust-analyzer cargo syn 2.0.119 ty/Type#)>;
30
31 let mut variants: Variantsrust-analyzer cargo educe 0.6.0 trait_handlers/deref/deref_enum/Variants# = 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().();
32
33 for variant in data.variantsrust-analyzer cargo syn 2.0.119 derive/DataEnum#variants..iterrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]iter().() {
34 let _ = TypeAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/type_attribute/TypeAttributeBuilder# {
35 enable_flagrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/type_attribute/TypeAttributeBuilder#enable_flag.: false
36 }
37 .build_from_attributesrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/type_attribute/impl#[TypeAttributeBuilder]build_from_attributes().(&variant.attrsrust-analyzer cargo syn 2.0.119 data/Variant#attrs., traits)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
38
39 if let Fieldsrust-analyzer cargo syn 2.0.119 data/Fields#::Unitrust-analyzer cargo syn 2.0.119 data/Fields#Unit# = &variant.fieldsrust-analyzer cargo syn 2.0.119 data/Variant#fields. {
40 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(panicrust-analyzer cargo educe 0.6.0 panic/::trait_not_support_unit_variantrust-analyzer cargo educe 0.6.0 panic/trait_not_support_unit_variant().(
41 meta.pathrust-analyzer cargo syn 2.0.119 attr/impl#[Meta]path().().get_identrust-analyzer cargo syn 2.0.119 path/impl#[Path]get_ident().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
42 variant,
43 ));
44 }
45
46 let fields = &variant.fieldsrust-analyzer cargo syn 2.0.119 data/Variant#fields.;
47
48 let (index, field) = 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 {
49 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().();
50
51 let _ = FieldAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/field_attribute/FieldAttributeBuilder# {
52 enable_flagrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/field_attribute/FieldAttributeBuilder#enable_flag.: true
53 }
54 .build_from_attributesrust-analyzer cargo educe 0.6.0 trait_handlers/deref/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().;
55
56 (0usize, field)
57 } else {
58 let mut deref_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#)> = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
59
60 for (index, field) in variant.fieldsrust-analyzer cargo syn 2.0.119 data/Variant#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().() {
61 let field_attribute = FieldAttributeBuilderrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/field_attribute/FieldAttributeBuilder# {
62 enable_flagrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/field_attribute/FieldAttributeBuilder#enable_flag.: true
63 }
64 .build_from_attributesrust-analyzer cargo educe 0.6.0 trait_handlers/deref/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().;
65
66 if field_attribute.flagrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/field_attribute/FieldAttribute#flag. {
67 if deref_field.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() {
68 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/deref/::panicrust-analyzer cargo educe 0.6.0 trait_handlers/deref/panic/::multiple_deref_fields_of_variantrust-analyzer cargo educe 0.6.0 trait_handlers/deref/panic/multiple_deref_fields_of_variant().(
69 field_attribute.spanrust-analyzer cargo educe 0.6.0 trait_handlers/deref/models/field_attribute/FieldAttribute#span.,
70 variant,
71 ));
72 }
73
74 deref_field = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((index, field));
75 }
76 }
77
78 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(deref_field) = deref_field {
79 deref_field
80 } else {
81 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/deref/::panicrust-analyzer cargo educe 0.6.0 trait_handlers/deref/panic/::no_deref_field_of_variantrust-analyzer cargo educe 0.6.0 trait_handlers/deref/panic/no_deref_field_of_variant().(meta.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(), variant));
82 }
83 };
84
85 let (field_name, is_tuple): (Identrust-analyzer cargo proc-macro2 1.0.107 Ident#, bool) = match 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().() {
86 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ident) => (ident.clonerust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][Clone]clone().(), false),
87 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => (format_identrust-analyzer cargo quote 1.0.47 format/format_ident!!("_{}", index), true),
88 };
89
90 variants.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((&variant.identrust-analyzer cargo syn 2.0.119 data/Variant#ident., is_tuple, index, field_name, &field.tyrust-analyzer cargo syn 2.0.119 data/Field#ty.));
91 }
92
93 if variants.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
94 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/deref/::panicrust-analyzer cargo educe 0.6.0 trait_handlers/deref/panic/::no_deref_fieldrust-analyzer cargo educe 0.6.0 trait_handlers/deref/panic/no_deref_field().(meta.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().()));
95 }
96
97 let ty = variants[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..4;
98 let dereference_ty = dereferencerust-analyzer cargo educe 0.6.0 common/`r#type`/dereference().(ty);
99
100 target_token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!(#dereference_ty));
101
102 for (variant_ident, is_tuple, index, field_name, _) in variants {
103 let mut pattern_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().();
104
105 if is_tuple {
106 for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#index {
107 pattern_token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!(_,));
108 }
109
110 pattern_token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!( #field_name, .. ));
111
112 arms_token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(
113 quoterust-analyzer cargo quote 1.0.47 quote!!( Self::#variant_ident ( #pattern_token_stream ) => #field_name, ),
114 );
115 } else {
116 pattern_token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!!( #field_name, .. ));
117
118 arms_token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(
119 quoterust-analyzer cargo quote 1.0.47 quote!!( Self::#variant_ident { #pattern_token_stream } => #field_name, ),
120 );
121 }
122 }
123 }
124
125 let ident = &ast.identrust-analyzer cargo syn 2.0.119 derive/DeriveInput#ident.;
126
127 let (impl_generics, ty_generics, where_clause) = 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().();
128
129 token_stream.extendrust-analyzer cargo proc-macro2 1.0.107 impl#[TokenStream][`Extend<TokenTree>`]extend().(quoterust-analyzer cargo quote 1.0.47 quote!! {
130 impl #impl_generics ::core::ops::Deref for #ident #ty_generics #where_clause {
131 type Target = #target_token_stream;
132
133 #[inline]
134 fn deref(&self) -> &Self::Target {
135 match self {
136 #arms_token_stream
137 }
138 }
139 }
140 });
141
142 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
143 }
144 }