Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-unrolled csharp arrays and nested structs #135

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ examples/complex/bindings/c/cmake-build-debug
*.actual
*.generated
__pycache__
**/.DS_Store
2 changes: 1 addition & 1 deletion crates/backend_csharp/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Default for Config {
namespace_mappings: NamespaceMappings::new("My.Company"),
namespace_id: String::new(),
visibility_types: CSharpVisibility::AsDeclared,
unroll_struct_arrays: true,
unroll_struct_arrays: false,
write_types: WriteTypes::NamespaceAndInteroptopusGlobal,
rename_symbols: false,
debug: false,
Expand Down
295 changes: 285 additions & 10 deletions crates/backend_csharp/src/writer.rs

Large diffs are not rendered by default.

68 changes: 66 additions & 2 deletions crates/reference_project/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::patterns::result::{Error, FFIError};
use crate::types::{
ambiguous1, ambiguous2, common, Array, Callbacku8u8, EnumDocumented, EnumRenamedXYZ, Generic, Generic2, Generic3, Generic4, Packed1, Packed2, Phantom,
StructDocumented, StructRenamedXYZ, Transparent, Tupled, Vec3f32, Visibility1, Visibility2, Weird1, Weird2,
ambiguous1, ambiguous2, common, Array, Callbacku8u8, CharArray, EnumDocumented, EnumRenamedXYZ, FixedString, Generic, Generic2, Generic3, Generic4, NestedArray,
Packed1, Packed2, Phantom, StructDocumented, StructRenamedXYZ, Transparent, Tupled, Vec3f32, Visibility1, Visibility2, Weird1, Weird2,
};
use interoptopus::patterns::option::FFIOption;
use interoptopus::patterns::result::panics_and_errors_to_ffi_enum;
Expand Down Expand Up @@ -202,6 +202,70 @@ pub fn array_1(x: Array) -> u8 {
x.data[0]
}

#[ffi_function]
pub fn array_2() -> Array {
Array {
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
}
}

#[ffi_function]
pub fn array_3(arr: &mut Array) {
arr.data[0] = 42;
}

#[ffi_function]
pub fn nested_array_1() -> NestedArray {
NestedArray {
field_enum: EnumRenamedXYZ::X,
field_vec: Vec3f32 { x: 1.0, y: 2.0, z: 3.0 },
field_bool: true,
field_int: 42,
field_array: [1, 2, 3, 4, 5],
field_struct: Array {
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
},
}
}

#[ffi_function]
pub fn nested_array_2(result: &mut NestedArray) {
result.field_enum = EnumRenamedXYZ::X;
result.field_vec = Vec3f32 { x: 1.0, y: 2.0, z: 3.0 };
result.field_bool = true;
result.field_int = 42;
result.field_array = [1, 2, 3, 4, 5];
result.field_struct = Array {
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
};
}

#[ffi_function]
pub fn nested_array_3(input: NestedArray) -> u8 {
input.field_struct.data[1]
}

#[ffi_function]
pub fn char_array_1() -> CharArray {
let mut result = CharArray {
str: FixedString { data: [0; 32] },
};

result.str.data[..14].copy_from_slice(b"Hello, World!\0");

result
}

#[ffi_function]
pub fn char_array_2(arr: CharArray) -> CharArray {
arr
}

#[ffi_function]
pub fn char_array_3(arr: &CharArray) -> u8 {
arr.str.data[0]
}

#[ffi_function]
pub fn renamed(x: StructRenamedXYZ) -> EnumRenamedXYZ {
x.e
Expand Down
8 changes: 8 additions & 0 deletions crates/reference_project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ pub fn ffi_inventory() -> Inventory {
.register(function!(functions::generic_3))
.register(function!(functions::generic_4))
.register(function!(functions::array_1))
.register(function!(functions::array_2))
.register(function!(functions::array_3))
.register(function!(functions::nested_array_1))
.register(function!(functions::nested_array_2))
.register(function!(functions::nested_array_3))
.register(function!(functions::char_array_1))
.register(function!(functions::char_array_2))
.register(function!(functions::char_array_3))
.register(function!(functions::documented))
.register(function!(functions::ambiguous_1))
.register(function!(functions::ambiguous_2))
Expand Down
28 changes: 28 additions & 0 deletions crates/reference_project/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! All supported type patterns.

use interoptopus::lang::c::{ArrayType, CType};
use interoptopus::lang::rust::CTypeInfo;
use interoptopus::patterns::slice::FFISlice;
use interoptopus::patterns::string::CStrPointer;
use interoptopus::patterns::TypePattern;
use interoptopus::{callback, ffi_type};
use std::fmt::Debug;
use std::marker::PhantomData;
Expand Down Expand Up @@ -86,6 +88,32 @@ pub struct Array {
pub data: [u8; 16],
}

#[ffi_type]
pub struct NestedArray {
pub field_enum: EnumRenamedXYZ,
pub field_vec: Vec3f32,
pub field_bool: bool,
pub field_int: i32,
pub field_array: [u16; 5],
pub field_struct: Array,
}

#[repr(transparent)]
pub struct FixedString<const N: usize> {
pub data: [u8; N],
}

unsafe impl<const N: usize> CTypeInfo for FixedString<N> {
fn type_info() -> CType {
CType::Array(ArrayType::new(CType::Pattern(TypePattern::CChar), N))
}
}

#[ffi_type]
pub struct CharArray {
pub str: FixedString<32>,
}

#[ffi_type]
pub struct GenericArray<T>
where
Expand Down
2 changes: 2 additions & 0 deletions examples/hello_world/bindings/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#pragma warning disable 0105
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Runtime.CompilerServices;
using My.Company;
#pragma warning restore 0105
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#pragma warning disable 0105
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Runtime.CompilerServices;
using My.Company;
#pragma warning restore 0105
Expand Down
2 changes: 2 additions & 0 deletions examples/unity_hot_reload/unity/Assets/MyRustLib/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#pragma warning disable 0105
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Runtime.CompilerServices;
using My.Company;
#pragma warning restore 0105
Expand Down
31 changes: 31 additions & 0 deletions tests/tests/c_reference_project/reference_project.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ typedef struct array
uint8_t data[16];
} array;

typedef struct chararray
{
char str[32];
} chararray;

typedef struct container
{
local foreign;
Expand Down Expand Up @@ -283,6 +288,16 @@ typedef struct optionvec

typedef void (*mycallbackvoid)(const void* ptr);

typedef struct nestedarray
{
enumrenamed field_enum;
vec3f32 field_vec;
bool field_bool;
int32_t field_int;
uint16_t field_array[5];
array field_struct;
} nestedarray;

///A pointer to an array of data someone else owns which may not be modified.
typedef struct sliceuseasciistringpattern
{
Expand Down Expand Up @@ -391,6 +406,22 @@ uint8_t generic_4(const generic4* x);

uint8_t array_1(array x);

array array_2();

void array_3(array* arr);

nestedarray nested_array_1();

void nested_array_2(nestedarray* result);

uint8_t nested_array_3(nestedarray input);

chararray char_array_1();

chararray char_array_2(chararray arr);

uint8_t char_array_3(const chararray* arr);

/// This function has documentation.
enumdocumented documented(structdocumented x);

Expand Down
Loading