Skip to content

Commit

Permalink
Refactor impl Default generation
Browse files Browse the repository at this point in the history
Move the detection of a default constructor function in the analysis.
Move the logic about using Object::new into object.
  • Loading branch information
pbor committed Mar 5, 2023
1 parent 0334ced commit e666d62
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
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<>
&& 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) {
// 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

0 comments on commit e666d62

Please sign in to comment.