Debian Codegraph @

src/downgrade_derive/enums.rs

raw symbols SCIP
1 // Take a look at the license at the top of the repository in the LICENSE file.
2
3 use proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#;
4 use quoterust-analyzer cargo quote 1.0.44 crate/::{format_identrust-analyzer cargo quote 1.0.44 format/format_ident!, quoterust-analyzer cargo quote 1.0.44 quote!};
5 use synrust-analyzer cargo syn 2.0.117 crate/::{Genericsrust-analyzer cargo syn 2.0.117 generics/Generics#, Identrust-analyzer cargo proc-macro2 1.0.106 Ident#};
6
7 use superrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/::fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/::{DowngradeStructPartsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#, derive_downgrade_fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/derive_downgrade_fields().};
8 use craterust-analyzer cargo glib-macros 0.22.6 crate/::utilsrust-analyzer cargo glib-macros 0.22.6 utils/::crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new().;
9
10 /// This function derives a weak type for a given strong enum and
11 /// implementations of `Downgrade` and `Upgrade` traits.
12 ///
13 /// # Example
14 ///
15 /// ```rust,ignore
16 /// #[derive(glib::Downgrade)]
17 /// enum Choice {
18 /// This(X, Y),
19 /// That { x: X, y: Y },
20 /// }
21 /// ```
22 ///
23 /// Here is what will be derived:
24 ///
25 /// ```rust,ignore
26 /// enum ChoiceWeak {
27 /// This(<X as Downgrade>::Weak, <Y as Downgrade>::Weak),
28 /// That {
29 /// x: <X as Downgrade>::Weak,
30 /// y: <Y as Downgrade>::Weak,
31 /// },
32 /// }
33 ///
34 /// impl glib::clone::Downgrade for Choice {
35 /// type Weak = ChoiceWeak;
36 ///
37 /// fn downgrade(&self) -> Self::Weak {
38 /// match self {
39 /// Self::This(ref _0, ref _1) => Self::Weak::This(
40 /// glib::clone::Downgrade::downgrade(_0),
41 /// glib::clone::Downgrade::downgrade(_1),
42 /// ),
43 /// Self::That { ref x, ref y } => Self::Weak::That(
44 /// glib::clone::Downgrade::downgrade(x),
45 /// glib::clone::Downgrade::downgrade(y),
46 /// ),
47 /// }
48 /// }
49 /// }
50 ///
51 /// impl glib::clone::Upgrade for ChoiceWeak {
52 /// type Strong = Choice;
53 ///
54 /// fn upgrade(&self) -> Option<Self::Strong> {
55 /// Some(match self {
56 /// Self::This(ref _0, ref _1) => Self::Strong::This(
57 /// glib::clone::Upgrade::upgrade(_0)?,
58 /// glib::clone::Upgrade::upgrade(_1)?,
59 /// ),
60 /// Self::That { ref x, ref y } => Self::Strong::That(
61 /// glib::clone::Upgrade::upgrade(x)?,
62 /// glib::clone::Upgrade::upgrade(y)?,
63 /// ),
64 /// })
65 /// }
66 /// }
67 /// ```
68 pub fn derive_downgrade_for_enumrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/enums/derive_downgrade_for_enum().(
69 ident: Identrust-analyzer cargo proc-macro2 1.0.106 Ident#,
70 generics: Genericsrust-analyzer cargo syn 2.0.117 generics/Generics#,
71 data_enum: synrust-analyzer cargo syn 2.0.117 crate/::DataEnumrust-analyzer cargo syn 2.0.117 derive/DataEnum#,
72 ) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
73 let glib = crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new().();
74 let weak_type = format_identrust-analyzer cargo quote 1.0.44 format/format_ident!!("{}Weak", ident);
75
76 let variants: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, DowngradeStructPartsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#)> = data_enum
77 .variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants.
78 .into_iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`][IntoIterator]into_iter().()
79 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|variant| (variant.identrust-analyzer cargo syn 2.0.117 data/Variant#ident., derive_downgrade_fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/derive_downgrade_fields().(variant.fieldsrust-analyzer cargo syn 2.0.117 data/Variant#fields.)))
80 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
81
82 let weak_variants: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = variants
83 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
84 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(ident, parts)| {
85 let weak_fields = &parts.weak_fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#weak_fields.;
86 quoterust-analyzer cargo quote 1.0.44 quote!! {
87 #ident #weak_fields
88 }
89 })
90 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
91
92 let downgrade_variants: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = variants
93 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
94 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(ident, parts)| {
95 let destruct = &parts.destructrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#destruct.;
96 let downgrade = &parts.downgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#downgrade.;
97 quoterust-analyzer cargo quote 1.0.44 quote!! {
98 Self::#ident #destruct => Self::Weak::#ident #downgrade
99 }
100 })
101 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
102
103 let upgrade_variants: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = variants
104 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
105 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(ident, parts)| {
106 let destruct = &parts.destructrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#destruct.;
107 let upgrade = &parts.upgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#upgrade.;
108 quoterust-analyzer cargo quote 1.0.44 quote!! {
109 Self::#ident #destruct => Self::Strong::#ident #upgrade
110 }
111 })
112 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
113
114 let derived = quoterust-analyzer cargo quote 1.0.44 quote!! {
115 pub enum #weak_type #generics {#(
116 #weak_variants
117 ),*}
118
119 impl #generics #glib::clone::Downgrade for #ident #generics {
120 type Weak = #weak_type #generics;
121
122 fn downgrade(&self) -> Self::Weak {
123 match self {#(
124 #downgrade_variants
125 ),*}
126 }
127 }
128
129 impl #generics #glib::clone::Upgrade for #weak_type #generics {
130 type Strong = #ident #generics;
131
132 fn upgrade(&self) -> ::core::option::Option<Self::Strong> {
133 ::core::option::Option::Some(match self {#(
134 #upgrade_variants
135 ),*})
136 }
137 }
138 };
139
140 derived.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
141 }