From e1d6fd830f3872379af74c21a80389a9ab44ef26 Mon Sep 17 00:00:00 2001 From: Kristupas Antanavicius Date: Fri, 11 Oct 2024 16:28:13 +0300 Subject: [PATCH] Fix empty struct (#94) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kristupas Antanavičius --- Cargo.lock | 11 +++++++++++ bindgen/templates/RecordTemplate.cs | 4 ++-- .../UniffiCS.BindingTests/TestIssue75.cs | 16 ++++++++++++++++ .../{TestRegressions.cs => TestIssue76.cs} | 2 +- fixtures/Cargo.toml | 1 + fixtures/regressions/issue-75/Cargo.toml | 18 ++++++++++++++++++ fixtures/regressions/issue-75/README.md | 3 +++ fixtures/regressions/issue-75/src/lib.rs | 9 +++++++++ fixtures/src/lib.rs | 1 + 9 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 dotnet-tests/UniffiCS.BindingTests/TestIssue75.cs rename dotnet-tests/UniffiCS.BindingTests/{TestRegressions.cs => TestIssue76.cs} (93%) create mode 100644 fixtures/regressions/issue-75/Cargo.toml create mode 100644 fixtures/regressions/issue-75/README.md create mode 100644 fixtures/regressions/issue-75/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 41ff316..871ebcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -528,6 +528,16 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "issue-75" +version = "1.0.0" +dependencies = [ + "once_cell", + "thiserror", + "uniffi", + "uniffi_macros", +] + [[package]] name = "issue-76" version = "1.0.0" @@ -1208,6 +1218,7 @@ name = "uniffi-bindgen-cs-fixtures" version = "0.1.0" dependencies = [ "global-methods-class-name", + "issue-75", "issue-76", "null-to-empty-string", "uniffi-cs-custom-types-builtin", diff --git a/bindgen/templates/RecordTemplate.cs b/bindgen/templates/RecordTemplate.cs index 4cd9cb0..1331f5c 100644 --- a/bindgen/templates/RecordTemplate.cs +++ b/bindgen/templates/RecordTemplate.cs @@ -55,9 +55,9 @@ class {{ rec|ffi_converter_name }}: FfiConverterRustBuffer<{{ type_name }}> { } public override int AllocationSize({{ type_name }} value) { - return + return 0 {%- for field in rec.fields() %} - {{ field|allocation_size_fn }}(value.{{ field.name()|var_name }}){% if !loop.last %} +{% endif%} + + {{ field|allocation_size_fn }}(value.{{ field.name()|var_name }}) {%- endfor -%}; } diff --git a/dotnet-tests/UniffiCS.BindingTests/TestIssue75.cs b/dotnet-tests/UniffiCS.BindingTests/TestIssue75.cs new file mode 100644 index 0000000..272b090 --- /dev/null +++ b/dotnet-tests/UniffiCS.BindingTests/TestIssue75.cs @@ -0,0 +1,16 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +using uniffi.issue_75; + +namespace UniffiCS.BindingTests; + +public class ClassIssue75 +{ + [Fact] + public void TestIssue75() + { + new EmptyDictionary(); + } +} diff --git a/dotnet-tests/UniffiCS.BindingTests/TestRegressions.cs b/dotnet-tests/UniffiCS.BindingTests/TestIssue76.cs similarity index 93% rename from dotnet-tests/UniffiCS.BindingTests/TestRegressions.cs rename to dotnet-tests/UniffiCS.BindingTests/TestIssue76.cs index 17f4a24..3ef37d1 100644 --- a/dotnet-tests/UniffiCS.BindingTests/TestRegressions.cs +++ b/dotnet-tests/UniffiCS.BindingTests/TestIssue76.cs @@ -6,7 +6,7 @@ namespace UniffiCS.BindingTests; -public class TestRegressions +public class ClassIssue76 { [Fact] public void TestIssue76() diff --git a/fixtures/Cargo.toml b/fixtures/Cargo.toml index e2049a7..02fc6d5 100644 --- a/fixtures/Cargo.toml +++ b/fixtures/Cargo.toml @@ -10,6 +10,7 @@ crate-type = ["cdylib", "lib"] [dependencies] global-methods-class-name = { path = "global-methods-class-name" } +issue-75 = { path = "regressions/issue-75" } issue-76 = { path = "regressions/issue-76" } null-to-empty-string = { path = "null-to-empty-string" } uniffi-cs-custom-types-builtin = { path = "custom-types-builtin" } diff --git a/fixtures/regressions/issue-75/Cargo.toml b/fixtures/regressions/issue-75/Cargo.toml new file mode 100644 index 0000000..5a9a87b --- /dev/null +++ b/fixtures/regressions/issue-75/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "issue-75" +version = "1.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["lib", "cdylib"] +name = "issue_75" + +[dependencies] +once_cell = "1.12" +thiserror = "1.0" +uniffi = {path = "../../../3rd-party/uniffi-rs/uniffi", features=["build"]} +uniffi_macros = {path = "../../../3rd-party/uniffi-rs/uniffi_macros"} + +[build-dependencies] +uniffi = {path = "../../../3rd-party/uniffi-rs/uniffi", features=["bindgen-tests"]} diff --git a/fixtures/regressions/issue-75/README.md b/fixtures/regressions/issue-75/README.md new file mode 100644 index 0000000..fd2df1b --- /dev/null +++ b/fixtures/regressions/issue-75/README.md @@ -0,0 +1,3 @@ +# https://github.com/NordSecurity/uniffi-bindgen-cs/issues/75 + +Empty struct causes invalid code to be generated in `RecordTemplate.cs::AllocationSize` diff --git a/fixtures/regressions/issue-75/src/lib.rs b/fixtures/regressions/issue-75/src/lib.rs new file mode 100644 index 0000000..08eb0ae --- /dev/null +++ b/fixtures/regressions/issue-75/src/lib.rs @@ -0,0 +1,9 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#[derive(uniffi::Record)] +pub struct EmptyDictionary { +} + +uniffi::setup_scaffolding!(); diff --git a/fixtures/src/lib.rs b/fixtures/src/lib.rs index b596e1e..54a3940 100644 --- a/fixtures/src/lib.rs +++ b/fixtures/src/lib.rs @@ -23,5 +23,6 @@ mod uniffi_fixtures { uniffi_cs_disposable::uniffi_reexport_scaffolding!(); uniffi_cs_optional_parameters::uniffi_reexport_scaffolding!(); stringify::uniffi_reexport_scaffolding!(); + issue_75::uniffi_reexport_scaffolding!(); issue_76::uniffi_reexport_scaffolding!(); }