@@ -11,6 +11,7 @@ use crate::{
1111 env:: Env ,
1212 file_saver:: * ,
1313 library:: Member ,
14+ nameutil:: use_glib_type,
1415 version:: Version ,
1516} ;
1617
@@ -43,6 +44,8 @@ mod trampoline;
4344mod trampoline_from_glib;
4445mod visibility;
4546pub use visibility:: Visibility ;
47+
48+ use self :: general:: { allow_deprecated, cfg_condition_no_doc} ;
4649mod trampoline_to_glib;
4750pub mod translate_from_glib;
4851pub mod translate_to_glib;
@@ -168,3 +171,82 @@ pub fn generate_default_impl<
168171 Ok ( ( ) )
169172 }
170173}
174+
175+ pub fn generate_variant_impls (
176+ w : & mut dyn Write ,
177+ env : & Env ,
178+ config : & GObject ,
179+ type_name : & str ,
180+ type_version : Option < Version > ,
181+ deprecated_version : Option < Version > ,
182+ static_variant_type_str : & str ,
183+ from_variant_impl : & str ,
184+ to_variant_impl : & str ,
185+ ) -> Result < ( ) > {
186+ let assert = if env. config . generate_safety_asserts {
187+ "skip_assert_initialized!();\n \t \t "
188+ } else {
189+ ""
190+ } ;
191+ let gvariant = use_glib_type ( env, "Variant" ) ;
192+ let tovariant = use_glib_type ( env, "ToVariant" ) ;
193+
194+ version_condition ( w, env, None , type_version, false , 0 ) ?;
195+ cfg_condition_no_doc ( w, config. cfg_condition . as_ref ( ) , false , 0 ) ?;
196+ allow_deprecated ( w, deprecated_version, false , 0 ) ?;
197+ writeln ! (
198+ w,
199+ "impl {staticvarianttype} for {type_name} {{
200+ fn static_variant_type() -> std::borrow::Cow<'static, {variantty}> {{
201+ {assert}std::borrow::Cow::Borrowed(unsafe {{
202+ {variantty}::from_str_unchecked(\" {static_variant_type_str}\" )
203+ }})
204+ }}
205+ }}" ,
206+ staticvarianttype = use_glib_type( env, "StaticVariantType" ) ,
207+ variantty = use_glib_type( env, "VariantTy" ) ,
208+ ) ?;
209+ writeln ! ( w) ?;
210+
211+ version_condition ( w, env, None , type_version, false , 0 ) ?;
212+ cfg_condition_no_doc ( w, config. cfg_condition . as_ref ( ) , false , 0 ) ?;
213+ allow_deprecated ( w, deprecated_version, false , 0 ) ?;
214+ writeln ! (
215+ w,
216+ "impl {fromvariant} for {type_name} {{
217+ fn from_variant(variant: &{gvariant}) -> Option<Self> {{
218+ {assert}{from_variant_impl}
219+ }}
220+ }}" ,
221+ fromvariant = use_glib_type( env, "FromVariant" ) ,
222+ ) ?;
223+ writeln ! ( w) ?;
224+
225+ version_condition ( w, env, None , type_version, false , 0 ) ?;
226+ cfg_condition_no_doc ( w, config. cfg_condition . as_ref ( ) , false , 0 ) ?;
227+ allow_deprecated ( w, deprecated_version, false , 0 ) ?;
228+ writeln ! (
229+ w,
230+ "impl {tovariant} for {type_name} {{
231+ fn to_variant(&self) -> {gvariant} {{
232+ {assert}{to_variant_impl}
233+ }}
234+ }}"
235+ ) ?;
236+ writeln ! ( w) ?;
237+
238+ version_condition ( w, env, None , type_version, false , 0 ) ?;
239+ cfg_condition_no_doc ( w, config. cfg_condition . as_ref ( ) , false , 0 ) ?;
240+ allow_deprecated ( w, deprecated_version, false , 0 ) ?;
241+ writeln ! (
242+ w,
243+ "impl From<{type_name}> for {gvariant} {{
244+ fn from(v: {type_name}) -> Self {{
245+ {assert}{tovariant}::to_variant(&v)
246+ }}
247+ }}" ,
248+ ) ?;
249+ writeln ! ( w) ?;
250+
251+ Ok ( ( ) )
252+ }
0 commit comments