add fliter

This commit is contained in:
startewho 2023-09-08 15:31:23 +08:00
parent a0e4804b3b
commit cee92d8130
6 changed files with 22 additions and 18 deletions

View File

@ -172,7 +172,7 @@ csbindgen::Builder::default()
.csharp_use_function_pointer(true) // optional, default: true .csharp_use_function_pointer(true) // optional, default: true
.csharp_disable_emit_dll_name(false) // optional, default: false .csharp_disable_emit_dll_name(false) // optional, default: false
.csharp_imported_namespaces("MyLib") // optional, default: empty .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: "" .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal") // optional, default: ""
.generate_csharp_file("../dotnet-sandbox/NativeMethods.cs") // required .generate_csharp_file("../dotnet-sandbox/NativeMethods.cs") // required
.unwrap(); .unwrap();
@ -196,7 +196,7 @@ namespace {csharp_namespace}
#endif #endif
} }
{csharp_generate_const} {csharp_generate_const_fliter}
[DllImport(__DllName, EntryPoint = "{csharp_entry_point_prefix}LZ4_versionNumber", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [DllImport(__DllName, EntryPoint = "{csharp_entry_point_prefix}LZ4_versionNumber", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int {csharp_method_prefix}LZ4_versionNumber(); 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_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. `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_method_prefix("") // optional, default: ""
.csharp_use_function_pointer(true) // optional, default: true .csharp_use_function_pointer(true) // optional, default: true
.csharp_imported_namespaces("MyLib") // optional, default: empty .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: "" .csharp_dll_name_if("UNITY_IOS && !UNITY_EDITOR", "__Internal") // optional, default: ""
.generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs") // required .generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs") // required
.unwrap(); .unwrap();

View File

@ -61,7 +61,7 @@ fn main() -> Result<(), Box<dyn Error>> {
//.csharp_c_long_convert("int") //.csharp_c_long_convert("int")
//.csharp_c_ulong_convert("uint") //.csharp_c_ulong_convert("uint")
// .csharp_use_function_pointer(true) // .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") .generate_to_file("src/lz4_ffi.rs", "../dotnet-sandbox/lz4_bindgen.cs")
.unwrap(); .unwrap();
@ -89,7 +89,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.csharp_dll_name("csbindgen_tests") .csharp_dll_name("csbindgen_tests")
.csharp_use_function_pointer(true) .csharp_use_function_pointer(true)
//.csharp_use_function_pointer(false) //.csharp_use_function_pointer(false)
.csharp_generate_const(true) .csharp_generate_const_filter(|_|true)
.generate_csharp_file("../dotnet-sandbox/NativeMethods.cs") .generate_csharp_file("../dotnet-sandbox/NativeMethods.cs")
.unwrap(); .unwrap();
@ -123,7 +123,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.rust_file_header("use super::bullet3::*;") .rust_file_header("use super::bullet3::*;")
.csharp_class_name("LibBullet3") .csharp_class_name("LibBullet3")
.csharp_dll_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")?; .generate_to_file("src/bullet3_ffi.rs", "../dotnet-sandbox/bullet3_bindgen.cs")?;
csbindgen::Builder::new() csbindgen::Builder::new()

View File

@ -30,7 +30,7 @@ pub struct BindgenOptions {
pub csharp_if_dll_name: String, pub csharp_if_dll_name: String,
pub csharp_use_function_pointer: bool, pub csharp_use_function_pointer: bool,
pub csharp_imported_namespaces: Vec<String>, pub csharp_imported_namespaces: Vec<String>,
pub csharp_generate_const: bool, pub csharp_generate_const_filter: fn(const_name: &str) -> bool,
} }
impl Default for Builder { impl Default for Builder {
@ -54,7 +54,7 @@ impl Default for Builder {
csharp_if_dll_name: "".to_string(), csharp_if_dll_name: "".to_string(),
csharp_use_function_pointer: true, csharp_use_function_pointer: true,
csharp_imported_namespaces: vec![], csharp_imported_namespaces: vec![],
csharp_generate_const: false, csharp_generate_const_filter: |_| false,
}, },
} }
} }
@ -85,6 +85,8 @@ impl Builder {
self self
} }
/// add original extern call type prefix to rust wrapper, /// add original extern call type prefix to rust wrapper,
/// `return {rust_method_type_path}::foo()` /// `return {rust_method_type_path}::foo()`
pub fn rust_method_type_path<T: Into<String>>(mut self, rust_method_type_path: T) -> Builder { pub fn rust_method_type_path<T: Into<String>>(mut self, rust_method_type_path: T) -> Builder {
@ -177,15 +179,15 @@ impl Builder {
self 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 { 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.options.csharp_use_function_pointer = csharp_use_function_pointer;
self self
} }
/// conifure C# generate const, default is false /// configure C# generate const filter, default `|_| false`
pub fn csharp_generate_const(mut self, csharp_generate_const: bool) -> Builder { pub fn csharp_generate_const_filter(mut self, csharp_generate_const_filter: fn(const_name: &str) -> bool) -> Builder {
self.options.csharp_generate_const = csharp_generate_const; self.options.csharp_generate_const_filter = csharp_generate_const_filter;
self self
} }

View File

@ -291,7 +291,7 @@ pub fn emit_csharp(
enum_string.push('\n'); enum_string.push('\n');
} }
let mut const_string = String::new(); let mut const_string: String = String::new();
for item in consts { for item in consts {
let mut type_name = item.rust_type.to_csharp_string( let mut type_name = item.rust_type.to_csharp_string(
options, options,

View File

@ -49,9 +49,8 @@ pub(crate) fn generate(
collect_struct(&file_ast, &mut structs); collect_struct(&file_ast, &mut structs);
collect_enum(&file_ast, &mut enums); collect_enum(&file_ast, &mut enums);
if options.csharp_generate_const { collect_const(&file_ast, &mut consts,options.csharp_generate_const_filter);
collect_const(&file_ast, &mut consts);
}
} }
// collect using_types // collect using_types

View File

@ -251,11 +251,13 @@ fn collect_fields_unnamed(fields: &syn::FieldsUnnamed) -> Vec<FieldMember> {
result result
} }
pub fn collect_const(ast: &syn::File, result: &mut Vec<RustConst>) { pub fn collect_const(ast: &syn::File, result: &mut Vec<RustConst>,fliter:fn(const_name: &str) -> bool) {
for item in depth_first_module_walk(&ast.items) { for item in depth_first_module_walk(&ast.items) {
if let Item::Const(ct) = item { if let Item::Const(ct) = item {
// pub const Ident: ty = expr // pub const Ident: ty = expr
let const_name = ct.ident.to_string(); let const_name = ct.ident.to_string();
if fliter(const_name.as_str()) {
let t = parse_type(&ct.ty); let t = parse_type(&ct.ty);
if let syn::Expr::Lit(lit_expr) = &*ct.expr { if let syn::Expr::Lit(lit_expr) = &*ct.expr {
@ -293,6 +295,7 @@ pub fn collect_const(ast: &syn::File, result: &mut Vec<RustConst>) {
} }
} }
} }
}
pub fn collect_enum(ast: &syn::File, result: &mut Vec<RustEnum>) { pub fn collect_enum(ast: &syn::File, result: &mut Vec<RustEnum>) {
for item in depth_first_module_walk(&ast.items) { for item in depth_first_module_walk(&ast.items) {