Skip to content

Commit

Permalink
WIP: codegen: Add version condition on special function traits
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Nov 16, 2020
1 parent d33f27a commit 7b7bec9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
let config = &env.config.objects[&enum_analysis.full_name];
let enum_ = enum_analysis.type_(&env.library);

if let Some(cfg) = version_condition_string(env, enum_.version, false, 0) {
mod_rs.push(cfg);
}
mod_rs.push(format!("pub use self::enums::{};", enum_.name));
let condition = version_condition_string(env, enum_.version, false, 0);
generate_enum(env, w, enum_, config)?;

let functions = enum_analysis.functions();
Expand All @@ -59,7 +56,13 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
&enum_analysis.functions,
&enum_analysis.specials,
None,
condition.as_deref(),
)?;

if let Some(cfg) = condition {
mod_rs.push(cfg);
}
mod_rs.push(format!("pub use self::enums::{};", enum_.name));
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn generate(
} else {
None
},
None // TODO
)?;

if !analysis.builder_properties.is_empty() {
Expand Down
1 change: 1 addition & 0 deletions src/codegen/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::record::Info)
&analysis.functions,
&analysis.specials,
None,
None, // TODO
)?;

if analysis.concurrency != library::Concurrency::None {
Expand Down
11 changes: 8 additions & 3 deletions src/codegen/trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn generate(
functions: &[Info],
specials: &Infos,
trait_name: Option<&str>,
condition: Option<&str>,
) -> Result<()> {
for (type_, name) in specials.iter() {
if let Some(info) = lookup(functions, name) {
Expand All @@ -23,7 +24,7 @@ pub fn generate(
Type::Equal => {
generate_eq(w, type_name, info, trait_name)?;
}
Type::ToString => generate_display(w, type_name, info, trait_name)?,
Type::ToString => generate_display(w, type_name, info, trait_name, condition)?,
Type::Hash => generate_hash(w, type_name, info, trait_name)?,
_ => {}
}
Expand Down Expand Up @@ -74,6 +75,7 @@ fn generate_display(
type_name: &str,
func: &Info,
trait_name: Option<&str>,
condition: Option<&str>,
) -> Result<()> {
use crate::analysis::out_parameters::Mode;

Expand All @@ -91,10 +93,13 @@ fn generate_display(
format!("f.write_str(&{})", call)
};

if let Some(condition) = condition {
writeln!(w, "{}", condition)?;
}

writeln!(
w,
"
impl fmt::Display for {type_name} {{
"impl fmt::Display for {type_name} {{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{
{body}
Expand Down

0 comments on commit 7b7bec9

Please sign in to comment.