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

Refactor impl Default generation #1446

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 15 additions & 1 deletion src/analysis/info_base.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{imports::Imports, *};
use crate::{codegen::Visibility, library, version::Version};
use crate::{codegen::Visibility, config::gobjects::GStatus, library, version::Version};

#[derive(Debug, Default)]
pub struct InfoBase {
Expand Down Expand Up @@ -38,4 +38,18 @@ impl InfoBase {
.filter(|f| f.status.need_generate() && f.kind == library::FunctionKind::Function)
.collect()
}

pub fn default_constructor(&self) -> Option<&functions::Info> {
self.functions.iter().find(|f| {
!f.hidden
&& f.status.need_generate()
&& f.kind == library::FunctionKind::Constructor
&& f.status == GStatus::Generate
// For now we only look for new() with no params
&& f.name == "new"
&& f.parameters.rust_parameters.is_empty()
// Cannot generate Default implementation for Option<>
Copy link
Member

Choose a reason for hiding this comment

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

Should also do the same for Result?

&& f.ret.parameter.as_ref().map_or(false, |x| !*x.lib_par.nullable)
})
}
}
50 changes: 13 additions & 37 deletions src/codegen/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,47 +966,23 @@ pub fn write_vec<T: Display>(w: &mut dyn Write, v: &[T]) -> Result<()> {
Ok(())
}

pub fn declare_default_from_new(
pub fn declare_default(
w: &mut dyn Write,
env: &Env,
name: &str,
functions: &[analysis::functions::Info],
has_builder: bool,
func: &analysis::functions::Info,
) -> Result<()> {
if let Some(func) = functions.iter().find(|f| {
!f.hidden
&& f.status.need_generate()
&& f.name == "new"
// Cannot generate Default implementation for Option<>
&& f.ret.parameter.as_ref().map_or(false, |x| !*x.lib_par.nullable)
}) {
if func.parameters.rust_parameters.is_empty() {
writeln!(w)?;
version_condition(w, env, None, func.version, false, 0)?;
writeln!(
w,
"impl Default for {name} {{
fn default() -> Self {{
Self::new()
}}
}}"
)?;
} else if has_builder {
// create an alternative default implementation the uses `glib::object::Object::new()`
writeln!(w)?;
version_condition(w, env, None, func.version, false, 0)?;
writeln!(
w,
"impl Default for {name} {{
fn default() -> Self {{
glib::object::Object::new::<Self>()
}}
}}"
)?;
}
}

Ok(())
writeln!(w)?;
version_condition(w, env, None, func.version, false, 0)?;
writeln!(
w,
"impl Default for {name} {{
fn default() -> Self {{
Self::{}()
}}
}}",
&func.name
)
}

/// Escapes string in format suitable for placing inside double quotes.
Expand Down
30 changes: 22 additions & 8 deletions src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,7 @@ pub fn generate(
}

writeln!(w, "}}")?;

general::declare_default_from_new(
w,
env,
&analysis.name,
&analysis.functions,
has_builder_properties(&analysis.builder_properties),
)?;
generate_default(w, env, analysis)?;
}

trait_impls::generate(
Expand Down Expand Up @@ -340,6 +333,27 @@ pub fn generate(
Ok(())
}

fn generate_default(w: &mut dyn Write, env: &Env, analysis: &analysis::object::Info) -> Result<()> {
let name = &analysis.name;
if let Some(func) = analysis.default_constructor() {
general::declare_default(w, env, name, func)
} else if has_builder_properties(&analysis.builder_properties) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason why with this patch RadioButton etc get picked up is because before we were falling back to object::new only if there was a new() function, but it did not have 0 params.

That seems rather odd and with the new code I did not get any false positive in core and gtk3

// create an alternative default implementation the uses `glib::object::Object::new()`
writeln!(w)?;
version_condition(w, env, None, analysis.version, false, 0)?;
writeln!(
w,
"impl Default for {name} {{
fn default() -> Self {{
glib::object::Object::new::<Self>()
}}
}}"
)
} else {
Ok(())
}
}

fn generate_builder(w: &mut dyn Write, env: &Env, analysis: &analysis::object::Info) -> Result<()> {
let glib_crate_name = if env.namespaces.is_glib_crate {
"crate"
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::record::Info)
writeln!(w, "}}")?;
}

general::declare_default_from_new(w, env, &analysis.name, &analysis.functions, false)?;
if let Some(func) = analysis.default_constructor() {
general::declare_default(w, env, &analysis.name, func)?;
}

trait_impls::generate(
w,
Expand Down