Skip to content

Commit

Permalink
Add generic parameters to builder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Oct 19, 2019
1 parent 241d790 commit 490c36f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/analysis/bounds.rs
Original file line number Diff line number Diff line change
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
11 changes: 10 additions & 1 deletion src/analysis/class_builder.rs
Original file line number Diff line number Diff line change
@@ -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,21 @@ fn analyze_property(
if !for_builder {
return None;
}
if let Ok(ref s) = used_rust_type(env, prop.typ, false) {
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);
}
}

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 +124,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 490c36f

Please sign in to comment.