Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler namespace type collection #2901

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions crates/libs/bindgen/src/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,32 +178,35 @@ fn namespace(writer: &Writer, tree: &Tree) -> String {
continue;
}
let name = type_name.name;
let kind = def.kind();
match kind {
let mut kind = def.kind();

let tokens = match kind {
metadata::TypeKind::Class => {
if def.flags().contains(metadata::TypeAttributes::WindowsRuntime) {
types.entry(kind).or_default().insert(name, classes::writer(writer, def));
classes::writer(writer, def)
} else {
quote! {}
}
}
metadata::TypeKind::Interface => types.entry(kind).or_default().entry(name).or_default().combine(&interfaces::writer(writer, def)),
metadata::TypeKind::Enum => types.entry(kind).or_default().entry(name).or_default().combine(&enums::writer(writer, def)),
metadata::TypeKind::Interface => interfaces::writer(writer, def),
metadata::TypeKind::Enum => enums::writer(writer, def),
metadata::TypeKind::Struct => {
if def.fields().next().is_none() {
if let Some(guid) = metadata::type_def_guid(def) {
let ident = to_ident(name);
let value = writer.guid(&guid);
let guid = writer.type_name(&metadata::Type::GUID);
let constant = quote! {
pub const #ident: #guid = #value;
};
types.entry(metadata::TypeKind::Class).or_default().entry(name).or_default().combine(&constant);
continue;
if let Some(guid) = clsid(def) {
kind = metadata::TypeKind::Class;
let ident = to_ident(name);
let value = writer.guid(&guid);
let guid = writer.type_name(&metadata::Type::GUID);
quote! {
pub const #ident: #guid = #value;
}
} else {
structs::writer(writer, def)
}
types.entry(kind).or_default().entry(name).or_default().combine(&structs::writer(writer, def));
}
metadata::TypeKind::Delegate => types.entry(kind).or_default().entry(name).or_default().combine(&delegates::writer(writer, def)),
}
metadata::TypeKind::Delegate => delegates::writer(writer, def),
};

types.entry(kind).or_default().entry(name).or_default().combine(&tokens);
}
metadata::Item::Fn(def, namespace) => {
let name = def.name();
Expand Down Expand Up @@ -233,6 +236,13 @@ fn namespace(writer: &Writer, tree: &Tree) -> String {
tokens.into_string()
}

fn clsid(def: metadata::TypeDef) -> Option<metadata::Guid> {
if def.fields().next().is_none() {
return metadata::type_def_guid(def);
}
None
}

fn namespace_impl(writer: &Writer, tree: &Tree) -> String {
let writer = &mut writer.clone();
writer.namespace = tree.namespace;
Expand Down
6 changes: 6 additions & 0 deletions crates/libs/core/src/imp/factory_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ impl<C, I> FactoryCache<C, I> {
}
}

impl<C, I> Default for FactoryCache<C, I> {
fn default() -> Self {
Self::new()
}
}

impl<C: crate::RuntimeName, I: Interface> FactoryCache<C, I> {
pub fn call<R, F: FnOnce(&I) -> crate::Result<R>>(&self, callback: F) -> crate::Result<R> {
loop {
Expand Down