Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codgen: run the visitor on global functions as well #1313

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions src/custom_type_glib_priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly this change is not great because it ends up using the glib::Priority where it doesn't make sense. Well there was only one case in gtk3-rs and another in gtk4-rs. E.g, https://docs.gtk.org/gtk3/method.TextTag.set_priority.html. Although, it drops a bunch of manual code from gtk4-rs, could be a fair trade, not sure :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will test it with gstreamer once the send on callbacks changes lands

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
}
}
6 changes: 6 additions & 0 deletions src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down