Skip to content

Commit

Permalink
Simplify cfg union calculation (#2965)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Apr 2, 2024
1 parent 52ee594 commit 994dc75
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
19 changes: 5 additions & 14 deletions crates/libs/bindgen/src/rust/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@ impl Cfg {
pub fn add_feature(&mut self, feature: &'static str) {
self.types.entry(feature).or_default();
}
pub fn union(&self, other: &Self) -> Self {
let mut union = Self::default();
self.types.keys().for_each(|feature| {
union.types.entry(feature).or_default();
});
other.types.keys().for_each(|feature| {
union.types.entry(feature).or_default();
});
self.arches.iter().for_each(|arch| {
union.arches.insert(arch);
});
other.arches.iter().for_each(|arch| {
union.arches.insert(arch);
});
pub fn union(&self, mut other: Self) -> Self {
let mut union = self.clone();
union.types.append(&mut other.types);
union.core_types.append(&mut other.core_types);
union.arches.append(&mut other.arches);
union
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/rust/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ fn gen_conversions(writer: &Writer, def: metadata::TypeDef, ident: &TokenStream,

let into = writer.type_name(&interface.ty);
write!(&mut hierarchy, ", {into}").unwrap();
hierarchy_cfg = hierarchy_cfg.union(&cfg::type_cfg(writer, &interface.ty));
hierarchy_cfg = hierarchy_cfg.union(cfg::type_cfg(writer, &interface.ty));
hierarchy_added = true;
}

for def in metadata::type_def_bases(def) {
let into = writer.type_def_name(def, &[]);
write!(&mut hierarchy, ", {into}").unwrap();
hierarchy_cfg = hierarchy_cfg.union(&cfg::type_def_cfg(writer, def, &[]));
hierarchy_cfg = hierarchy_cfg.union(cfg::type_def_cfg(writer, def, &[]));
hierarchy_added = true;
}

Expand Down
6 changes: 3 additions & 3 deletions crates/libs/bindgen/src/rust/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ pub fn gen_index(writer: &Writer) -> String {
cfg = match item {
Item::Type(def) => {
index_item.name = def.name().to_string();
cfg.union(&type_def_cfg(writer, def, &[]))
cfg.union(type_def_cfg(writer, def, &[]))
}
Item::Const(field) => {
index_item.name = field.name().to_string();
cfg.union(&field_cfg(writer, field))
cfg.union(field_cfg(writer, field))
}
Item::Fn(method, _) => {
index_item.name = method.name().to_string();
cfg.union(&signature_cfg(writer, method))
cfg.union(signature_cfg(writer, method))
}
};

Expand Down
8 changes: 4 additions & 4 deletions crates/libs/bindgen/src/rust/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn gen_win_interface(writer: &Writer, def: metadata::TypeDef) -> TokenStream {
let into = writer.type_name(ty);

write!(&mut hierarchy, ", {into}").unwrap();
hierarchy_cfg = hierarchy_cfg.union(&cfg::type_cfg(writer, ty));
hierarchy_cfg = hierarchy_cfg.union(cfg::type_cfg(writer, ty));
}

hierarchy.push_str(");");
Expand All @@ -124,7 +124,7 @@ fn gen_win_interface(writer: &Writer, def: metadata::TypeDef) -> TokenStream {
} else {
for ty in &vtables {
let into = writer.type_name(ty);
let cfg = writer.cfg_features(&cfg.union(&cfg::type_cfg(writer, ty)));
let cfg = writer.cfg_features(&cfg.union(cfg::type_cfg(writer, ty)));
tokens.combine(&quote! {
#cfg
impl<#constraints> windows_core::CanInto<#into> for #ident {}
Expand All @@ -141,7 +141,7 @@ fn gen_win_interface(writer: &Writer, def: metadata::TypeDef) -> TokenStream {
let into = writer.type_name(&interface.ty);

write!(&mut hierarchy, ", {into}").unwrap();
hierarchy_cfg = hierarchy_cfg.union(&cfg::type_cfg(writer, &interface.ty));
hierarchy_cfg = hierarchy_cfg.union(cfg::type_cfg(writer, &interface.ty));
}

hierarchy.push_str(");");
Expand All @@ -150,7 +150,7 @@ fn gen_win_interface(writer: &Writer, def: metadata::TypeDef) -> TokenStream {
} else {
for interface in &interfaces {
let into = writer.type_name(&interface.ty);
let cfg = writer.cfg_features(&cfg.union(&cfg::type_cfg(writer, &interface.ty)));
let cfg = writer.cfg_features(&cfg.union(cfg::type_cfg(writer, &interface.ty)));
tokens.combine(&quote! {
#cfg
impl<#constraints> windows_core::CanInto<#into> for #ident { const QUERY: bool = true; }
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/rust/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn gen_struct_with_name(writer: &Writer, def: metadata::TypeDef, struct_name: &s
}

let flags = def.flags();
let cfg = cfg.union(&cfg::type_def_cfg(writer, def, &[]));
let cfg = cfg.union(cfg::type_def_cfg(writer, def, &[]));

let repr = if let Some(layout) = def.class_layout() {
let packing = Literal::usize_unsuffixed(layout.packing_size());
Expand Down

0 comments on commit 994dc75

Please sign in to comment.