From aea639012b87d76377fb3c926692f2374b081d4c Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Mon, 24 Jan 2022 19:49:25 +0100 Subject: [PATCH 1/3] codgen: run the visitor on global functions as well otherwise glib::Priority is not replaced for them and we have to manually implement them in such case The same change is needed for Builder pattern --- src/visitors.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/visitors.rs b/src/visitors.rs index b20fbdc18..f1b8afcea 100644 --- a/src/visitors.rs +++ b/src/visitors.rs @@ -13,6 +13,12 @@ impl Namespace { return false; } } + // Replace types in global functions as well + for func in self.functions.iter_mut() { + if !visitor.visit_function_mut(func) { + return false; + } + } true } } From 17b3f7b275c88eff77544fb3e4c151da3cb38e9f Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Mon, 24 Jan 2022 20:04:55 +0100 Subject: [PATCH 2/3] codegen: handle functions returning a priority as well --- src/custom_type_glib_priority.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/custom_type_glib_priority.rs b/src/custom_type_glib_priority.rs index ce4b7a988..75f1f88a5 100644 --- a/src/custom_type_glib_priority.rs +++ b/src/custom_type_glib_priority.rs @@ -43,14 +43,16 @@ struct ReplaceToPriority { impl FunctionsMutVisitor for ReplaceToPriority { fn visit_function_mut(&mut self, func: &mut Function) -> bool { - if !func.name.ends_with("_async") { - return true; - } for par in &mut func.parameters { if par.typ == self.tid_int && par.name.ends_with("priority") { par.typ = self.tid_priority; } } + if func.ret.typ == self.tid_int + && (func.ret.name.ends_with("priority") || func.name.ends_with("priority")) + { + func.ret.typ = self.tid_priority; + } true } } From 436607905d9ff0b29d708febad231d6b875f8737 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Mon, 24 Jan 2022 20:20:05 +0100 Subject: [PATCH 3/3] codegen: handle Priority for Builder pattern as well Currently, the generated builder pattern doesn't modify the priority properties to make use of glib::Priority. We can't do something like what is done for functions parameters/returns as the builder properties are computed during the analysis phase. --- src/codegen/object.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/codegen/object.rs b/src/codegen/object.rs index 8a3c22e56..b2d8bb653 100644 --- a/src/codegen/object.rs +++ b/src/codegen/object.rs @@ -348,9 +348,29 @@ fn generate_builder(w: &mut dyn Write, env: &Env, analysis: &analysis::object::I )?; writeln!(w, "#[must_use = \"The builder must be built to be used\"]")?; writeln!(w, "pub struct {}Builder {{", analysis.name)?; + + let tid_int = env + .library + .find_type(0, "*.gint") + .expect("No fundamental type *.gint"); + + let glib_ns_id = env + .library + .find_namespace("GLib") + .expect("Missing `GLib` namespace in add_glib_priority!"); + let priority_id = env + .library + .find_type(glib_ns_id, "Priority") + .expect("No Priority type"); + for (builder_props, super_tid) in &analysis.builder_properties { for property in builder_props { - match RustType::try_new(env, property.typ) { + let property_type = if property.name.ends_with("priority") && property.typ == tid_int { + priority_id + } else { + property.typ + }; + match RustType::try_new(env, property_type) { Ok(type_string) => { let type_string = match type_string.as_str() { s if nameutil::is_gstring(s) => "String", @@ -364,7 +384,7 @@ fn generate_builder(w: &mut dyn Write, env: &Env, analysis: &analysis::object::I } else { library::ParameterDirection::Out }; - let mut param_type = RustType::builder(env, property.typ) + let mut param_type = RustType::builder(env, property_type) .direction(direction) .ref_mode(property.set_in_ref_mode) .try_build()