Skip to content

Commit

Permalink
codegen/trait_impls: Omit version constraints if already guarded
Browse files Browse the repository at this point in the history
This removes unnecessary (duplicate) `#[cfg]` attributes on trait
implementations where the surrounding `mod` (when in a file that fully
defines a `struct`) already declares this constraint.  This does not
suffice for `flags`/`enums` where multiple types live in a single shared
file without surrounding constraint.  When the feature requirement is
already described it is superfluous (and noisy) to specify it again.
Besides, rustdoc omits duplicate feature requirements if it can show
this just once at the top of the page for the entire `struct`.
  • Loading branch information
MarijnS95 committed Mar 23, 2021
1 parent 0f8addc commit a3ec57d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ fn generate_enum(
&analysis.functions,
&analysis.specials,
None,
None,
)?;

writeln!(w)?;
Expand Down
1 change: 1 addition & 0 deletions src/codegen/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ fn generate_flags(
&analysis.functions,
&analysis.specials,
None,
None,
)?;

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

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 @@ -113,6 +113,7 @@ pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::record::Info)
&analysis.functions,
&analysis.specials,
None,
analysis.version,
)?;

if analysis.concurrency != library::Concurrency::None {
Expand Down
36 changes: 25 additions & 11 deletions src/codegen/trait_impls.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::general::version_condition;
use crate::{
analysis::{
functions::Info,
special_functions::{Infos, Type},
},
codegen::general::version_condition,
version::Version,
Env,
};
use std::io::{Result, Write};
Expand All @@ -15,21 +16,24 @@ pub fn generate(
functions: &[Info],
specials: &Infos,
trait_name: Option<&str>,
scope_version: Option<Version>,
) -> Result<()> {
for (type_, special_info) in specials.traits().iter() {
if let Some(info) = lookup(functions, &special_info.glib_name) {
match *type_ {
Type::Compare => {
if !specials.has_trait(Type::Equal) {
generate_eq_compare(w, env, type_name, info, trait_name)?;
generate_eq_compare(w, env, type_name, info, trait_name, scope_version)?;
}
generate_ord(w, env, type_name, info, trait_name)?;
generate_ord(w, env, type_name, info, trait_name, scope_version)?;
}
Type::Equal => {
generate_eq(w, env, type_name, info, trait_name)?;
generate_eq(w, env, type_name, info, trait_name, scope_version)?;
}
Type::Display => generate_display(w, env, type_name, info, trait_name)?,
Type::Hash => generate_hash(w, env, type_name, info, trait_name)?,
Type::Display => {
generate_display(w, env, type_name, info, trait_name, scope_version)?
}
Type::Hash => generate_hash(w, env, type_name, info, trait_name, scope_version)?,
_ => {}
}
}
Expand Down Expand Up @@ -80,9 +84,11 @@ fn generate_display(
type_name: &str,
func: &Info,
trait_name: Option<&str>,
scope_version: Option<Version>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;
let version = Version::if_stricter_than(func.version, scope_version);
version_condition(w, env, version, false, 0)?;

use crate::analysis::out_parameters::Mode;

Expand Down Expand Up @@ -121,9 +127,11 @@ fn generate_hash(
type_name: &str,
func: &Info,
trait_name: Option<&str>,
scope_version: Option<Version>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;
let version = Version::if_stricter_than(func.version, scope_version);
version_condition(w, env, version, false, 0)?;

let call = generate_call(&func.name, &[], trait_name);

Expand All @@ -147,9 +155,11 @@ fn generate_eq(
type_name: &str,
func: &Info,
trait_name: Option<&str>,
scope_version: Option<Version>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;
let version = Version::if_stricter_than(func.version, scope_version);
version_condition(w, env, version, false, 0)?;

let call = generate_call(&func.name, &["other"], trait_name);

Expand All @@ -175,9 +185,11 @@ fn generate_eq_compare(
type_name: &str,
func: &Info,
trait_name: Option<&str>,
scope_version: Option<Version>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;
let version = Version::if_stricter_than(func.version, scope_version);
version_condition(w, env, version, false, 0)?;

let call = generate_call(&func.name, &["other"], trait_name);

Expand All @@ -203,9 +215,11 @@ fn generate_ord(
type_name: &str,
func: &Info,
trait_name: Option<&str>,
scope_version: Option<Version>,
) -> Result<()> {
writeln!(w)?;
version_condition(w, env, func.version, false, 0)?;
let version = Version::if_stricter_than(func.version, scope_version);
version_condition(w, env, version, false, 0)?;

let call = generate_call(&func.name, &["other"], trait_name);

Expand Down

0 comments on commit a3ec57d

Please sign in to comment.