From 8a837b8973960abeb7ecc72ca3096e43196c71d7 Mon Sep 17 00:00:00 2001 From: Sandeep Nambiar Date: Tue, 25 Jul 2023 04:52:42 +0200 Subject: [PATCH 1/2] feat: add extra namespaces to the exported file --- csbindgen/src/builder.rs | 15 ++++++++++++++- csbindgen/src/emitter.rs | 8 +++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/csbindgen/src/builder.rs b/csbindgen/src/builder.rs index 4ff3427..77f8403 100644 --- a/csbindgen/src/builder.rs +++ b/csbindgen/src/builder.rs @@ -29,6 +29,7 @@ pub struct BindgenOptions { pub csharp_if_symbol: String, pub csharp_if_dll_name: String, pub csharp_use_function_pointer: bool, + pub csharp_imported_namespaces: Vec, } impl Default for Builder { @@ -51,6 +52,7 @@ impl Default for Builder { csharp_if_symbol: "".to_string(), csharp_if_dll_name: "".to_string(), csharp_use_function_pointer: true, + csharp_imported_namespaces: vec![], }, } } @@ -63,7 +65,9 @@ impl Builder { /// Add an input .rs file(such as generated from bindgen) to generate binding. pub fn input_bindgen_file>(mut self, input_bindgen_file: T) -> Builder { - self.options.input_bindgen_files.push(input_bindgen_file.as_ref().to_path_buf()); + self.options + .input_bindgen_files + .push(input_bindgen_file.as_ref().to_path_buf()); self } @@ -109,6 +113,15 @@ impl Builder { self } + /// configure C# extra import namespace, + /// "using {csharp_namespace};" + pub fn csharp_import_namespace>(mut self, csharp_namespace: T) -> Builder { + self.options + .csharp_imported_namespaces + .push(csharp_namespace.into()); + self + } + /// configure C# class name(default is `NativeMethods`), /// `public static unsafe partial class {csharp_class_name}` pub fn csharp_class_name>(mut self, csharp_class_name: T) -> Builder { diff --git a/csbindgen/src/emitter.rs b/csbindgen/src/emitter.rs index 9c5d7f9..dc7c6e1 100644 --- a/csbindgen/src/emitter.rs +++ b/csbindgen/src/emitter.rs @@ -46,7 +46,7 @@ pub fn emit_rust_method(list: &Vec, options: &BindgenOptions) -> S " #[no_mangle] pub unsafe extern \"C\" fn {method_prefix}{method_name}( -{parameters} +{parameters} ){return_line} {{ {method_type_path2}{method_name}( @@ -290,6 +290,11 @@ pub fn emit_csharp( enum_string.push('\n'); } + let mut imported_namespaces = String::new(); + for name in &options.csharp_imported_namespaces { + imported_namespaces.push_str_ln(format!("using {name};").as_str()); + } + let result = format!( "// // This code is generated by csbindgen. @@ -299,6 +304,7 @@ pub fn emit_csharp( #pragma warning disable CS8981 using System; using System.Runtime.InteropServices; +{imported_namespaces} namespace {namespace} {{ From 148caab599de690efad04946bb87ed12b2a6ca31 Mon Sep 17 00:00:00 2001 From: Sandeep Nambiar Date: Tue, 25 Jul 2023 04:57:24 +0200 Subject: [PATCH 2/2] chore: undo formatting (unrelated change) --- csbindgen/src/builder.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/csbindgen/src/builder.rs b/csbindgen/src/builder.rs index 77f8403..8d0374c 100644 --- a/csbindgen/src/builder.rs +++ b/csbindgen/src/builder.rs @@ -65,9 +65,7 @@ impl Builder { /// Add an input .rs file(such as generated from bindgen) to generate binding. pub fn input_bindgen_file>(mut self, input_bindgen_file: T) -> Builder { - self.options - .input_bindgen_files - .push(input_bindgen_file.as_ref().to_path_buf()); + self.options.input_bindgen_files.push(input_bindgen_file.as_ref().to_path_buf()); self }