Debian Codegraph @

src/downgrade_derive/fields.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_macro2rust-analyzer cargo proc-macro2 1.0.106 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.106 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/::{Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#, FieldsNamedrust-analyzer cargo syn 2.0.117 data/FieldsNamed#, FieldsUnnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#, Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, Typerust-analyzer cargo syn 2.0.117 ty/Type#};
6
7 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().;
8
9 /// Parts needed to derive Downgrade and Upgrade implementation.
10 pub struct DowngradeStructPartsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts# {
11 /// Inner part of weak type declaration
12 pub weak_fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#weak_fields.: TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#,
13 /// Term needed to finish declaration. It is usually blank but is `;` for tuple structs.
14 pub end_of_structrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#end_of_struct.: TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#,
15 /// Destructuring pattern
16 pub destructrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#destruct.: TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#,
17 /// Downgrade code
18 pub downgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#downgrade.: TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#,
19 /// Upgrade code
20 pub upgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#upgrade.: TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#,
21 }
22
23 /// This function generates parts needed to derive Downgrade and Upgrade
24 /// implementations.
25 ///
26 /// # Example
27 ///
28 /// Let's assume following types are declared.
29 ///
30 /// ```rust,ignore
31 /// struct Unnamed(X, Y);
32 ///
33 /// struct Named {
34 /// x: X,
35 /// y: Y,
36 /// }
37 ///
38 /// enum Choice {
39 /// This(X, Y),
40 /// That { x: X, y: Y },
41 /// }
42 /// ```
43 ///
44 /// ## weak_fields
45 ///
46 /// For the struct `Unnamed` and for a enum's variant `Choice::This`
47 /// it will be `(<X as Downgrade>::Weak, <Y as Downgrade>::Weak)`.
48 /// For the struct `Named` and for a enum's variant `Choice::That`
49 /// it will be `{ x: <X as Downgrade>::Weak, y: <Y as Downgrade>::Weak, }`.
50 ///
51 /// ## end_of_struct
52 ///
53 /// It is a semicolon (`;`) for an `Unnamed` and is blank for the rest.
54 ///
55 /// ## destruct
56 ///
57 /// For the struct `Unnamed` and for a enum's variant `Choice::This`
58 /// it will be `(ref _0, ref _1)`.
59 /// For the struct `Named` and for a enum's variant `Choice::That`
60 /// it will be `{ ref x, ref y }`.
61 /// So it can be used as a destructuring pattern for values of both types,
62 /// strong and weak.
63 ///
64 /// ```rust,ignore
65 /// let Unnamed (ref _0, ref _1) = <expression>;
66 /// let Named { ref x, ref y } = <expression>;
67 ///
68 /// match <expression> {
69 /// Choice::This (ref _0, ref _1) => ... ,
70 /// Choice::That { ref x, ref y } => ... ,
71 /// }
72 /// ```
73 ///
74 /// # downgrade
75 ///
76 /// ```rust,ignore
77 /// (
78 /// glib::clone::Downgrade::downgrade(_0),
79 /// glib::clone::Downgrade::downgrade(_1),
80 /// )
81 ///
82 /// {
83 /// x: glib::clone::Downgrade::downgrade(x),
84 /// y: glib::clone::Downgrade::downgrade(y),
85 /// }
86 /// ```
87 ///
88 /// # upgrade
89 ///
90 /// ```rust,ignore
91 /// (
92 /// glib::clone::Upgrade::upgrade(_0)?,
93 /// glib::clone::Upgrade::upgrade(_1)?,
94 /// )
95 ///
96 /// {
97 /// x: glib::clone::Upgrade::upgrade(x)?,
98 /// y: glib::clone::Upgrade::upgrade(y)?,
99 /// }
100 /// ```
101 pub fn derive_downgrade_fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/derive_downgrade_fields().(fields: synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#) -> DowngradeStructPartsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts# {
102 let glib = crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new().();
103 match fields {
104 Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.117 data/Fields#Unnamed#(FieldsUnnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed# { unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed., .. }) => {
105 let fields: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Typerust-analyzer cargo syn 2.0.117 ty/Type#> = unnamed
106 .into_pairsrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]into_pairs().()
107 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|pair| pair.into_valuerust-analyzer cargo syn 2.0.117 punctuated/impl#[`Pair<T, P>`]into_value().())
108 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| field.tyrust-analyzer cargo syn 2.0.117 data/Field#ty.)
109 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
110
111 let weak_fields: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = fields
112 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
113 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|ty| {
114 quoterust-analyzer cargo quote 1.0.44 quote!! {
115 <#ty as #glib::clone::Downgrade>::Weak
116 }
117 })
118 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
119
120 let field_ident: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Identrust-analyzer cargo proc-macro2 1.0.106 Ident#> =
121 (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#fields.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|i| format_identrust-analyzer cargo quote 1.0.44 format/format_ident!!("_{}", i)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
122
123 DowngradeStructPartsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts# {
124 weak_fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#weak_fields.: quoterust-analyzer cargo quote 1.0.44 quote!! {
125 (#(
126 #weak_fields
127 ),*)
128 },
129 end_of_structrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#end_of_struct.: quoterust-analyzer cargo quote 1.0.44 quote!!(;),
130 destructrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#destruct.: quoterust-analyzer cargo quote 1.0.44 quote!! {
131 (#(
132 #field_ident
133 ),*)
134 },
135 downgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#downgrade.: quoterust-analyzer cargo quote 1.0.44 quote!! {
136 (#(
137 #glib::clone::Downgrade::downgrade(#field_ident)
138 ),*)
139 },
140 upgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#upgrade.: quoterust-analyzer cargo quote 1.0.44 quote!! {
141 (#(
142 #glib::clone::Upgrade::upgrade(#field_ident)?
143 ),*)
144 },
145 }
146 }
147 Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Namedrust-analyzer cargo syn 2.0.117 data/Fields#Named#(FieldsNamedrust-analyzer cargo syn 2.0.117 data/FieldsNamed# { namedrust-analyzer cargo syn 2.0.117 data/FieldsNamed#named., .. }) => {
148 let fields: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, Typerust-analyzer cargo syn 2.0.117 ty/Type#)> = named
149 .into_pairsrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]into_pairs().()
150 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|pair| pair.into_valuerust-analyzer cargo syn 2.0.117 punctuated/impl#[`Pair<T, P>`]into_value().())
151 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| (field.identrust-analyzer cargo syn 2.0.117 data/Field#ident..expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Field ident is specified"), field.tyrust-analyzer cargo syn 2.0.117 data/Field#ty.))
152 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
153
154 let weak_fields: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = fields
155 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
156 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(ident, ty)| {
157 quoterust-analyzer cargo quote 1.0.44 quote!! {
158 #ident: <#ty as #glib::clone::Downgrade>::Weak
159 }
160 })
161 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
162
163 let field_ident: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = fields.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(ident, _ty)| ident).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
164
165 DowngradeStructPartsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts# {
166 weak_fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#weak_fields.: quoterust-analyzer cargo quote 1.0.44 quote!! {
167 {#(
168 #weak_fields
169 ),*}
170 },
171 end_of_structrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#end_of_struct.: quoterust-analyzer cargo quote 1.0.44 quote!!(),
172 destructrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#destruct.: quoterust-analyzer cargo quote 1.0.44 quote!! {
173 {#(
174 #field_ident
175 ),*}
176 },
177 downgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#downgrade.: quoterust-analyzer cargo quote 1.0.44 quote!! {
178 {#(
179 #field_ident: #glib::clone::Downgrade::downgrade(#field_ident)
180 ),*}
181 },
182 upgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#upgrade.: quoterust-analyzer cargo quote 1.0.44 quote!! {
183 {#(
184 #field_ident: #glib::clone::Upgrade::upgrade(#field_ident)?
185 ),*}
186 },
187 }
188 }
189 Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unitrust-analyzer cargo syn 2.0.117 data/Fields#Unit# => DowngradeStructPartsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts# {
190 weak_fieldsrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#weak_fields.: quoterust-analyzer cargo quote 1.0.44 quote!! {},
191 end_of_structrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#end_of_struct.: quoterust-analyzer cargo quote 1.0.44 quote!! { ; },
192 destructrust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#destruct.: quoterust-analyzer cargo quote 1.0.44 quote!! {},
193 downgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#downgrade.: quoterust-analyzer cargo quote 1.0.44 quote!! {},
194 upgraderust-analyzer cargo glib-macros 0.22.6 downgrade_derive/fields/DowngradeStructParts#upgrade.: quoterust-analyzer cargo quote 1.0.44 quote!! {},
195 },
196 }
197 }