Skip to content

Commit

Permalink
codegen: Don't emit Rust Display impl when C function is available
Browse files Browse the repository at this point in the history
When get_name or to_string is available and adheres to the right
conditions a Display impl leveraging this C implementation is generated.
This will clash with a pure Rust impl if generate_display_trait is true
which should be disabled to prefer the C function call instead.
  • Loading branch information
MarijnS95 committed Dec 4, 2020
1 parent 733768d commit 68bdcf8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{function, trait_impls};
use crate::{
analysis::enums::Info,
analysis::special_functions::Type,
codegen::general::{
self, cfg_deprecated, derives, version_condition, version_condition_no_doc,
version_condition_string,
Expand Down Expand Up @@ -123,10 +124,13 @@ fn generate_enum(
.collect::<Vec<_>>();

if !functions.is_empty() {
let static_tostring = analysis
.specials
.get(&crate::analysis::special_functions::Type::Display)
.and_then(|f| if f.returns_static_ref { Some(f) } else { None });
let static_tostring = analysis.specials.get(&Type::Display).and_then(|f| {
if f.returns_static_ref {
Some(f)
} else {
None
}
});

writeln!(w)?;
version_condition(w, env, enum_.version, false, 0)?;
Expand All @@ -152,7 +156,7 @@ fn generate_enum(

writeln!(w)?;

if config.generate_display_trait {
if config.generate_display_trait && !analysis.specials.contains_key(&Type::Display) {
// Generate Display trait implementation.
version_condition(w, env, enum_.version, false, 0)?;
writeln!(
Expand Down
14 changes: 9 additions & 5 deletions src/codegen/flags.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{function, trait_impls};
use crate::{
analysis::flags::Info,
analysis::special_functions::Type,
codegen::general::{
self, cfg_deprecated, derives, version_condition, version_condition_string,
},
Expand Down Expand Up @@ -96,10 +97,13 @@ fn generate_flags(
.collect::<Vec<_>>();

if !functions.is_empty() {
let static_tostring = analysis
.specials
.get(&crate::analysis::special_functions::Type::Display)
.and_then(|f| if f.returns_static_ref { Some(f) } else { None });
let static_tostring = analysis.specials.get(&Type::Display).and_then(|f| {
if f.returns_static_ref {
Some(f)
} else {
None
}
});

writeln!(w)?;
version_condition(w, env, flags.version, false, 0)?;
Expand All @@ -125,7 +129,7 @@ fn generate_flags(

writeln!(w)?;

if config.generate_display_trait {
if config.generate_display_trait && !analysis.specials.contains_key(&Type::Display) {
// Generate Display trait implementation.
version_condition(w, env, flags.version, false, 0)?;
writeln!(
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{child_properties, function, general, properties, signal, trait_impls};
use crate::{
analysis::special_functions::Type,
analysis::{
self,
rust_type::{rust_type, rust_type_full},
Expand Down Expand Up @@ -133,7 +134,7 @@ pub fn generate(
generate_trait(w, env, analysis)?;
}

if generate_display_trait {
if generate_display_trait && !analysis.specials.contains_key(&Type::Display) {
writeln!(w, "\nimpl fmt::Display for {} {{", analysis.name,)?;
// Generate Display trait implementation.
writeln!(
Expand Down

0 comments on commit 68bdcf8

Please sign in to comment.