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

Add generic parameters to builder methods #853

Merged
merged 2 commits into from Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/analysis/bounds.rs
Expand Up @@ -170,6 +170,7 @@ impl Bounds {
_ => None,
}
}

fn get_to_glib_extra(bound_type: &BoundType) -> String {
use self::BoundType::*;
match *bound_type {
Expand All @@ -178,6 +179,7 @@ impl Bounds {
_ => String::new(),
}
}

pub fn add_parameter(
&mut self,
name: &str,
Expand Down Expand Up @@ -217,6 +219,7 @@ impl Bounds {
false
}
}

pub fn get_parameter_alias_info(&self, name: &str) -> Option<(char, BoundType)> {
self.used
.iter()
Expand All @@ -229,6 +232,7 @@ impl Bounds {
})
.map(|t| (t.alias, t.bound_type.clone()))
}

pub fn get_base_alias(&self, alias: char) -> Option<char> {
if alias == TYPE_PARAMETERS_START {
return None;
Expand All @@ -245,6 +249,7 @@ impl Bounds {
}
})
}

pub fn update_imports(&self, imports: &mut Imports) {
//TODO: import with versions
use self::BoundType::*;
Expand All @@ -256,12 +261,15 @@ impl Bounds {
}
}
}

pub fn is_empty(&self) -> bool {
self.used.is_empty()
}

pub fn iter(&self) -> Iter<'_, Bound> {
self.used.iter()
}

pub fn iter_lifetimes(&self) -> Iter<'_, char> {
self.lifetimes.iter()
}
Expand Down
14 changes: 12 additions & 2 deletions src/analysis/class_builder.rs
@@ -1,5 +1,6 @@
use crate::{
analysis::{
bounds::Bounds,
imports::Imports,
properties::{get_property_ref_modes, Property},
rust_type::*,
Expand Down Expand Up @@ -98,14 +99,22 @@ fn analyze_property(
if !for_builder {
return None;
}
if let Ok(ref s) = used_rust_type(env, prop.typ, false) {
imports.set_defaults(prop_version, &None);
let type_str = used_rust_type(env, prop.typ, false);
if let Ok(ref s) = type_str {
if !s.contains("GString") {
imports.add_used_type_with_version(s, prop_version);
imports.add_used_type(s);
}
}

let (get_out_ref_mode, set_in_ref_mode, nullable) = get_property_ref_modes(env, prop);

let mut bounds = Bounds::default();
if let Some(bound) = Bounds::type_for(env, prop.typ, nullable) {
imports.add("glib::object::IsA");
bounds.add_parameter(&name, &type_str.into_string(), bound, false);
}

Some(Property {
name: name.clone(),
var_name: String::new(),
Expand All @@ -116,6 +125,7 @@ fn analyze_property(
get_out_ref_mode,
set_in_ref_mode,
set_bound: None,
bounds,
version: prop_version,
deprecated_version: prop.deprecated_version,
})
Expand Down
5 changes: 4 additions & 1 deletion src/analysis/properties.rs
@@ -1,6 +1,6 @@
use crate::{
analysis::{
bounds::PropertyBound,
bounds::{Bounds, PropertyBound},
imports::Imports,
ref_mode::RefMode,
rust_type::*,
Expand All @@ -26,6 +26,7 @@ pub struct Property {
pub nullable: library::Nullable,
pub get_out_ref_mode: RefMode,
pub set_in_ref_mode: RefMode,
pub bounds: Bounds,
pub set_bound: Option<PropertyBound>,
pub version: Option<Version>,
pub deprecated_version: Option<Version>,
Expand Down Expand Up @@ -192,6 +193,7 @@ fn analyze_property(
get_out_ref_mode,
set_in_ref_mode,
set_bound: None,
bounds: Bounds::default(),
version: prop_version,
deprecated_version: prop.deprecated_version,
})
Expand Down Expand Up @@ -230,6 +232,7 @@ fn analyze_property(
get_out_ref_mode,
set_in_ref_mode,
set_bound,
bounds: Bounds::default(),
version: prop_version,
deprecated_version: prop.deprecated_version,
})
Expand Down
23 changes: 16 additions & 7 deletions src/codegen/object.rs
Expand Up @@ -171,11 +171,19 @@ fn generate_builder(w: &mut dyn Write, env: &Env, analysis: &analysis::object::I
library::Concurrency::None,
)
.into_string();
let (param_type_override, conversion) = match &param_type[..] {
"&str" => (None, ".to_string()"),
"&[&str]" => (Some("Vec<String>"), ""),
typ if typ.starts_with('&') => (None, ".clone()"),
_ => (None, ""),
let (param_type_override, bounds, conversion) = match &param_type[..] {
"&str" => (None, String::new(), ".to_string()"),
"&[&str]" => (Some("Vec<String>".to_string()), String::new(), ""),
_ if !property.bounds.is_empty() => {
let (bounds, _) = function::bounds(&property.bounds, &[], false, false);
let alias = property
.bounds
.get_parameter_alias_info(&property.name)
.map(|(alias, _)| format!("&{}", alias));
(alias, bounds, ".clone().upcast()")
}
typ if typ.starts_with('&') => (None, String::new(), ".clone()"),
_ => (None, String::new(), ""),
};
if let Some(param_type_override) = param_type_override {
param_type = param_type_override.to_string();
Expand All @@ -191,14 +199,15 @@ fn generate_builder(w: &mut dyn Write, env: &Env, analysis: &analysis::object::I
.map(|version| format!("{}\n", version))
.unwrap_or_default();
methods.push(format!(
"\n{prefix} pub fn {name}(mut self, {name}: {param_type}) -> Self {{
"\n{prefix} pub fn {name}{bounds}(mut self, {name}: {param_type}) -> Self {{
self.{name} = Some({name}{conversion});
self
}}",
prefix = prefix,
param_type = param_type,
name = name,
conversion = conversion
conversion = conversion,
bounds = bounds
));
properties.push(property);
}
Expand Down
8 changes: 6 additions & 2 deletions src/codegen/sys/build.rs
Expand Up @@ -140,7 +140,10 @@ fn find() -> Result<(), Error> {
}"##
)?;
if ns.name == "GObject" {
writeln!(w,"{}", r##" Err(err) => {
writeln!(
w,
"{}",
r##" Err(err) => {
#[cfg(target_os = "macos")]
{
let _ = writeln!(
Expand All @@ -154,7 +157,8 @@ fn find() -> Result<(), Error> {
}
}
}
"##)
"##
)
} else {
writeln!(
w,
Expand Down