Skip to content

Commit

Permalink
Merge pull request #48 from NordSecurity/fix_empty_type_generation
Browse files Browse the repository at this point in the history
Fix invalid `allocation_size` code generation when dealing with empty records
  • Loading branch information
Lipt0nas authored Oct 2, 2024
2 parents 6902e78 + f7665ae commit dfbb94b
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion bindgen/src/bindings/cpp/templates/rec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ class_name }}
}

int32_t {{ ffi_converter_name }}::allocation_size(const {{ class_name }} &val) {
{% if rec.fields().is_empty() %}
return 0;
{% else %}
return {% for field in rec.fields() %}
{{ field|allocation_size_fn}}(val.{{ field.name()|var_name() }}){% if !loop.last %} +{% else -%};{%- endif %}
{%- endfor %}
}
{% endif %}
}
2 changes: 2 additions & 0 deletions cpp-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ test_case(uniffi_docstring)
test_case(trait_methods)
test_case(custom_types_builtin)
test_case(enum_style_test)
test_case(empty_type)

scaffolding_test_case(arithmetic)
scaffolding_test_case(callbacks)
Expand All @@ -101,6 +102,7 @@ scaffolding_test_case(traits)
scaffolding_test_case(coverall)
scaffolding_test_case(custom_types_builtin)
scaffolding_test_case(enum_style_test)
scaffolding_test_case(empty_type)

add_library(uniffi_fixtures SHARED
${SCAFFOLDING_LIB_FILES})
Expand Down
9 changes: 9 additions & 0 deletions cpp-tests/scaffolding_tests/empty_type/lib_empty_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <lib_empty_type.hpp>

empty_type::Empty empty_type::get_empty_type() {
return Empty {};
}

void empty_type::send_empty_type(Empty e) {}

#include <empty_type_cpp_scaffolding.cpp>
11 changes: 11 additions & 0 deletions cpp-tests/scaffolding_tests/empty_type/lib_empty_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <cstdint>

namespace {
namespace empty_type {
struct Empty {};

Empty get_empty_type();

void send_empty_type(Empty e);
}
}
11 changes: 11 additions & 0 deletions cpp-tests/tests/empty_type/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "test_common.hpp"

#include <empty_type.hpp>

int main() {
auto empty = empty_type::get_empty_type();

empty_type::send_empty_type(empty);

return 0;
}
1 change: 1 addition & 0 deletions fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ uniffi-fixture-time = { git = "https://github.com/NordSecurity/uniffi-rs.git", t
uniffi-fixture-trait-methods = { git = "https://github.com/NordSecurity/uniffi-rs.git", tag = "v0.3.1+v0.25.0" }
uniffi-custom-types-builtin = { path = "custom-types-builtin" }
uniffi-enum-style-test = { path = "enum-style-test" }
uniffi-empty-type = { path = "empty-type" }
14 changes: 14 additions & 0 deletions fixtures/empty-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "uniffi-empty-type"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["lib", "cdylib"]
name = "uniffi_empty_type"

[dependencies]
uniffi = { workspace = true }

[build-dependencies]
uniffi = { workspace = true, features = ["build"] }
3 changes: 3 additions & 0 deletions fixtures/empty-type/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
uniffi::generate_scaffolding("src/empty_type.udl").unwrap();
}
6 changes: 6 additions & 0 deletions fixtures/empty-type/src/empty_type.udl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dictionary Empty {};

namespace empty_type{
Empty get_empty_type();
void send_empty_type(Empty e);
};
9 changes: 9 additions & 0 deletions fixtures/empty-type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub struct Empty {}

pub fn get_empty_type() -> Empty {
Empty {}
}

pub fn send_empty_type(empty: Empty) {}

uniffi::include_scaffolding!("empty_type");
1 change: 1 addition & 0 deletions fixtures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ mod uniffi_fixtures {

uniffi_cpp_custom_types_builtin::uniffi_reexport_scaffolding!();
uniffi_enum_style_test::uniffi_reexport_scaffolding!();
uniffi_empty_type::uniffi_reexport_scaffolding!();
}

0 comments on commit dfbb94b

Please sign in to comment.