From 5186f42ac18f2ef123eb2e5109c9e4af3d3e03fc Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 23 Sep 2021 04:16:48 +0000 Subject: [PATCH] Define a macro for constructing a TypeResolver for a given module This is used to resolve types referenced in modules other than the current one. --- c-bindings-gen/src/main.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 9cd32fe..5bc27eb 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -212,6 +212,18 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $($pat: pat)|* => $ } } } } +macro_rules! get_module_type_resolver { + ($module: expr, $crate_libs: expr, $crate_types: expr) => { { + let module: &str = &$module; + let mut module_iter = module.rsplitn(2, "::"); + module_iter.next().unwrap(); + let module = module_iter.next().unwrap(); + let imports = ImportResolver::new(module.splitn(2, "::").next().unwrap(), &$crate_types.lib_ast.dependencies, + module, &$crate_types.lib_ast.modules.get(module).unwrap().items); + TypeResolver::new(module, imports, $crate_types) + } } +} + /// Prints a C-mapped trait object containing a void pointer and a jump table for each function in /// the original trait. /// Implements the native Rust trait and relevant parent traits for the new C-mapped trait. @@ -510,12 +522,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty }, (s, i) => { if let Some(supertrait) = types.crate_types.traits.get(s) { - let mut module_iter = s.rsplitn(2, "::"); - module_iter.next().unwrap(); - let supertrait_module = module_iter.next().unwrap(); - let imports = ImportResolver::new(supertrait_module.splitn(2, "::").next().unwrap(), &types.crate_types.lib_ast.dependencies, - supertrait_module, &types.crate_types.lib_ast.modules.get(supertrait_module).unwrap().items); - let resolver = TypeResolver::new(&supertrait_module, imports, types.crate_types); + let resolver = get_module_type_resolver!(s, types.crate_libs, types.crate_types); writeln!(w, "impl {} for {} {{", s, trait_name).unwrap(); impl_trait_for_c!(supertrait, format!(".{}", i), &resolver); writeln!(w, "}}").unwrap(); -- 2.39.5