From cee92d813079d4600b6c1330901ff4673c10ab5d Mon Sep 17 00:00:00 2001 From: startewho Date: Fri, 8 Sep 2023 15:31:23 +0800 Subject: [PATCH 1/3] add fliter --- README.md | 8 ++++---- csbindgen-tests/build.rs | 6 +++--- csbindgen/src/builder.rs | 14 ++++++++------ csbindgen/src/emitter.rs | 2 +- csbindgen/src/lib.rs | 5 ++--- csbindgen/src/parser.rs | 5 ++++- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a4657e3..3828c71 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ csbindgen::Builder::default() .csharp_use_function_pointer(true) // optional, default: true .csharp_disable_emit_dll_name(false) // optional, default: false .csharp_imported_namespaces("MyLib") // optional, default: empty - .csharp_generate_const (false) // optional, default: false + .csharp_generate_const_fliter (|_|false) // optional, default: `|_|false` .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal") // optional, default: "" .generate_csharp_file("../dotnet-sandbox/NativeMethods.cs") // required .unwrap(); @@ -196,7 +196,7 @@ namespace {csharp_namespace} #endif } - {csharp_generate_const} + {csharp_generate_const_fliter} [DllImport(__DllName, EntryPoint = "{csharp_entry_point_prefix}LZ4_versionNumber", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern int {csharp_method_prefix}LZ4_versionNumber(); @@ -207,7 +207,7 @@ namespace {csharp_namespace} `csharp_disable_emit_dll_name` is optional, if set to true then don't emit `const string __DllName`. It is useful for generate same class-name from different builder. -`csharp_generate_const` is optional, if set to true then generate C# `const` field from Rust `const`. +`csharp_generate_const_fliter` is optional, if set a fliter fun, then generate fliter C# `const` field from Rust `const`. `input_extern_file` and `input_bindgen_file` allow mulitple call, if you need to add dependent struct, use this. @@ -272,7 +272,7 @@ csbindgen::Builder::default() .csharp_method_prefix("") // optional, default: "" .csharp_use_function_pointer(true) // optional, default: true .csharp_imported_namespaces("MyLib") // optional, default: empty - .csharp_generate_const (false) // optional, default: false + .csharp_generate_const_filter(|_|false) // optional, default:|_|false .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal") // optional, default: "" .generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs") // required .unwrap(); diff --git a/csbindgen-tests/build.rs b/csbindgen-tests/build.rs index ad1921c..5562adc 100644 --- a/csbindgen-tests/build.rs +++ b/csbindgen-tests/build.rs @@ -61,7 +61,7 @@ fn main() -> Result<(), Box> { //.csharp_c_long_convert("int") //.csharp_c_ulong_convert("uint") // .csharp_use_function_pointer(true) - .csharp_generate_const(true) + .csharp_generate_const_filter(|_|true) .generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs") .unwrap(); @@ -89,7 +89,7 @@ fn main() -> Result<(), Box> { .csharp_dll_name("csbindgen_tests") .csharp_use_function_pointer(true) //.csharp_use_function_pointer(false) - .csharp_generate_const(true) + .csharp_generate_const_filter(|_|true) .generate_csharp_file("../dotnet-sandbox/NativeMethods.cs") .unwrap(); @@ -123,7 +123,7 @@ fn main() -> Result<(), Box> { .rust_file_header("use super::bullet3::*;") .csharp_class_name("LibBullet3") .csharp_dll_name("libbullet3") - .csharp_generate_const(true) + .csharp_generate_const_filter(|_|true) .generate_to_file("src/bullet3_ffi.rs", "../dotnet-sandbox/bullet3_bindgen.cs")?; csbindgen::Builder::new() diff --git a/csbindgen/src/builder.rs b/csbindgen/src/builder.rs index 0d9ca0d..82fc5a2 100644 --- a/csbindgen/src/builder.rs +++ b/csbindgen/src/builder.rs @@ -30,7 +30,7 @@ pub struct BindgenOptions { pub csharp_if_dll_name: String, pub csharp_use_function_pointer: bool, pub csharp_imported_namespaces: Vec, - pub csharp_generate_const: bool, + pub csharp_generate_const_filter: fn(const_name: &str) -> bool, } impl Default for Builder { @@ -54,7 +54,7 @@ impl Default for Builder { csharp_if_dll_name: "".to_string(), csharp_use_function_pointer: true, csharp_imported_namespaces: vec![], - csharp_generate_const: false, + csharp_generate_const_filter: |_| false, }, } } @@ -85,6 +85,8 @@ impl Builder { self } + + /// add original extern call type prefix to rust wrapper, /// `return {rust_method_type_path}::foo()` pub fn rust_method_type_path>(mut self, rust_method_type_path: T) -> Builder { @@ -177,15 +179,15 @@ impl Builder { self } - /// conifure C# generate function pointer as delegate* or Func/Action, default is true(generate delegate*) + /// configure C# generate function pointer as delegate* or Func/Action, default is true(generate delegate*) pub fn csharp_use_function_pointer(mut self, csharp_use_function_pointer: bool) -> Builder { self.options.csharp_use_function_pointer = csharp_use_function_pointer; self } - /// conifure C# generate const, default is false - pub fn csharp_generate_const(mut self, csharp_generate_const: bool) -> Builder { - self.options.csharp_generate_const = csharp_generate_const; + /// configure C# generate const filter, default `|_| false` + pub fn csharp_generate_const_filter(mut self, csharp_generate_const_filter: fn(const_name: &str) -> bool) -> Builder { + self.options.csharp_generate_const_filter = csharp_generate_const_filter; self } diff --git a/csbindgen/src/emitter.rs b/csbindgen/src/emitter.rs index 72fd35a..0212942 100644 --- a/csbindgen/src/emitter.rs +++ b/csbindgen/src/emitter.rs @@ -291,7 +291,7 @@ pub fn emit_csharp( enum_string.push('\n'); } - let mut const_string = String::new(); + let mut const_string: String = String::new(); for item in consts { let mut type_name = item.rust_type.to_csharp_string( options, diff --git a/csbindgen/src/lib.rs b/csbindgen/src/lib.rs index 25dc2a2..c3d267e 100644 --- a/csbindgen/src/lib.rs +++ b/csbindgen/src/lib.rs @@ -49,9 +49,8 @@ pub(crate) fn generate( collect_struct(&file_ast, &mut structs); collect_enum(&file_ast, &mut enums); - if options.csharp_generate_const { - collect_const(&file_ast, &mut consts); - } + collect_const(&file_ast, &mut consts,options.csharp_generate_const_filter); + } // collect using_types diff --git a/csbindgen/src/parser.rs b/csbindgen/src/parser.rs index d58847c..1a737d4 100644 --- a/csbindgen/src/parser.rs +++ b/csbindgen/src/parser.rs @@ -251,11 +251,13 @@ fn collect_fields_unnamed(fields: &syn::FieldsUnnamed) -> Vec { result } -pub fn collect_const(ast: &syn::File, result: &mut Vec) { +pub fn collect_const(ast: &syn::File, result: &mut Vec,fliter:fn(const_name: &str) -> bool) { for item in depth_first_module_walk(&ast.items) { if let Item::Const(ct) = item { // pub const Ident: ty = expr let const_name = ct.ident.to_string(); + if fliter(const_name.as_str()) { + let t = parse_type(&ct.ty); if let syn::Expr::Lit(lit_expr) = &*ct.expr { @@ -291,6 +293,7 @@ pub fn collect_const(ast: &syn::File, result: &mut Vec) { }); } } + } } } From c4781ee65397b3c482b9ca089b90ea008fde420a Mon Sep 17 00:00:00 2001 From: startewho Date: Fri, 8 Sep 2023 15:37:29 +0800 Subject: [PATCH 2/3] spell error --- README.md | 6 +++--- csbindgen/src/parser.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3828c71..3682e04 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ csbindgen::Builder::default() .csharp_use_function_pointer(true) // optional, default: true .csharp_disable_emit_dll_name(false) // optional, default: false .csharp_imported_namespaces("MyLib") // optional, default: empty - .csharp_generate_const_fliter (|_|false) // optional, default: `|_|false` + .csharp_generate_const_filter (|_|false) // optional, default: `|_|false` .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal") // optional, default: "" .generate_csharp_file("../dotnet-sandbox/NativeMethods.cs") // required .unwrap(); @@ -196,7 +196,7 @@ namespace {csharp_namespace} #endif } - {csharp_generate_const_fliter} + {csharp_generate_const_filter} [DllImport(__DllName, EntryPoint = "{csharp_entry_point_prefix}LZ4_versionNumber", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern int {csharp_method_prefix}LZ4_versionNumber(); @@ -207,7 +207,7 @@ namespace {csharp_namespace} `csharp_disable_emit_dll_name` is optional, if set to true then don't emit `const string __DllName`. It is useful for generate same class-name from different builder. -`csharp_generate_const_fliter` is optional, if set a fliter fun, then generate fliter C# `const` field from Rust `const`. +`csharp_generate_const_filter` is optional, if set a fliter fun, then generate filter C# `const` field from Rust `const`. `input_extern_file` and `input_bindgen_file` allow mulitple call, if you need to add dependent struct, use this. diff --git a/csbindgen/src/parser.rs b/csbindgen/src/parser.rs index 1a737d4..04ad215 100644 --- a/csbindgen/src/parser.rs +++ b/csbindgen/src/parser.rs @@ -251,12 +251,12 @@ fn collect_fields_unnamed(fields: &syn::FieldsUnnamed) -> Vec { result } -pub fn collect_const(ast: &syn::File, result: &mut Vec,fliter:fn(const_name: &str) -> bool) { +pub fn collect_const(ast: &syn::File, result: &mut Vec,filter:fn(const_name: &str) -> bool) { for item in depth_first_module_walk(&ast.items) { if let Item::Const(ct) = item { // pub const Ident: ty = expr let const_name = ct.ident.to_string(); - if fliter(const_name.as_str()) { + if filter(const_name.as_str()) { let t = parse_type(&ct.ty); From 82c91e13f5f45e1a2dcfc9a56dc4379d8bebb131 Mon Sep 17 00:00:00 2001 From: startewho Date: Fri, 8 Sep 2023 15:43:44 +0800 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3682e04..26f8461 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ namespace {csharp_namespace} `csharp_disable_emit_dll_name` is optional, if set to true then don't emit `const string __DllName`. It is useful for generate same class-name from different builder. -`csharp_generate_const_filter` is optional, if set a fliter fun, then generate filter C# `const` field from Rust `const`. +`csharp_generate_const_filter` is optional, if set a filter fun, then generate filter C# `const` field from Rust `const`. `input_extern_file` and `input_bindgen_file` allow mulitple call, if you need to add dependent struct, use this.