Skip to content

Commit

Permalink
Merge pull request #77 from nihohit/no-class
Browse files Browse the repository at this point in the history
Allow generating bindings without a class.
  • Loading branch information
neuecc authored Jul 8, 2024
2 parents 068e1e7 + b5fc58f commit cb400ba
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
12 changes: 12 additions & 0 deletions csbindgen-tests/src/only_enums_and_structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[repr(C)]
pub enum Foo {
Bar = 1,
Baz = 2,
}

#[repr(C)]
pub struct Vec3 {
x: f32,
y: f32,
z: f32,
}
25 changes: 17 additions & 8 deletions csbindgen/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,22 @@ pub fn emit_csharp(
}
}

// use empty string if the generated class is empty.
let class_string = if method_list_string.is_empty() && const_string.is_empty() {
String::new()
} else {
format!(
"{accessibility} static unsafe partial class {class_name}
{{
{dll_name}
{const_string}
{method_list_string}
}}"
)
};

let file_header = if options.csharp_file_header.len() > 0 {
options.csharp_file_header.to_string() + "\n"
} else {
Expand All @@ -368,14 +384,7 @@ using System.Runtime.InteropServices;
namespace {namespace}
{{
{accessibility} static unsafe partial class {class_name}
{{
{dll_name}
{const_string}
{method_list_string}
}}
{class_string}
{structs_string}
{enum_string}
Expand Down
27 changes: 27 additions & 0 deletions csbindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,31 @@ mod tests {
file.write_all(new_toml.as_bytes()).unwrap();
file.flush().unwrap();
}

fn compare_and_delete_files(original_file_path: &str, generated_file_path: &str) {
let original = fs::read_to_string(original_file_path)
.expect("Should have been able to read original file");

let generated = fs::read_to_string(generated_file_path)
.expect("Should have been able to read generated file");

assert_eq!(original, generated);

fs::remove_file(generated_file_path).unwrap();
}

#[test]
fn test_emit_without_class() {
let generated_file_path = "dotnet-sandbox/only_enums_and_structs_bindgen.cs";
Builder::new()
.always_included_types(["Vec3", "Foo"])
.input_bindgen_file("csbindgen-tests/src/only_enums_and_structs.rs")
.generate_csharp_file(generated_file_path)
.unwrap();

compare_and_delete_files(
"dotnet-sandbox/only_enums_and_structs_original.cs",
generated_file_path,
);
}
}
31 changes: 31 additions & 0 deletions dotnet-sandbox/only_enums_and_structs_original.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <auto-generated>
// This code is generated by csbindgen.
// DON'T CHANGE THIS DIRECTLY.
// </auto-generated>
#pragma warning disable CS8500
#pragma warning disable CS8981
using System;
using System.Runtime.InteropServices;


namespace CsBindgen
{


[StructLayout(LayoutKind.Sequential)]
internal unsafe partial struct Vec3
{
public float x;
public float y;
public float z;
}


internal enum Foo : uint
{
Bar = 1,
Baz = 2,
}


}

0 comments on commit cb400ba

Please sign in to comment.