Skip to content

Commit dfbb94b

Browse files
authored
Merge pull request #48 from NordSecurity/fix_empty_type_generation
Fix invalid `allocation_size` code generation when dealing with empty records
2 parents 6902e78 + f7665ae commit dfbb94b

File tree

12 files changed

+80
-1
lines changed

12 files changed

+80
-1
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen/src/bindings/cpp/templates/rec.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ class_name }}
3333
}
3434

3535
int32_t {{ ffi_converter_name }}::allocation_size(const {{ class_name }} &val) {
36+
{% if rec.fields().is_empty() %}
37+
return 0;
38+
{% else %}
3639
return {% for field in rec.fields() %}
3740
{{ field|allocation_size_fn}}(val.{{ field.name()|var_name() }}){% if !loop.last %} +{% else -%};{%- endif %}
3841
{%- endfor %}
39-
}
42+
{% endif %}
43+
}

cpp-tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ test_case(uniffi_docstring)
8787
test_case(trait_methods)
8888
test_case(custom_types_builtin)
8989
test_case(enum_style_test)
90+
test_case(empty_type)
9091

9192
scaffolding_test_case(arithmetic)
9293
scaffolding_test_case(callbacks)
@@ -101,6 +102,7 @@ scaffolding_test_case(traits)
101102
scaffolding_test_case(coverall)
102103
scaffolding_test_case(custom_types_builtin)
103104
scaffolding_test_case(enum_style_test)
105+
scaffolding_test_case(empty_type)
104106

105107
add_library(uniffi_fixtures SHARED
106108
${SCAFFOLDING_LIB_FILES})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <lib_empty_type.hpp>
2+
3+
empty_type::Empty empty_type::get_empty_type() {
4+
return Empty {};
5+
}
6+
7+
void empty_type::send_empty_type(Empty e) {}
8+
9+
#include <empty_type_cpp_scaffolding.cpp>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <cstdint>
2+
3+
namespace {
4+
namespace empty_type {
5+
struct Empty {};
6+
7+
Empty get_empty_type();
8+
9+
void send_empty_type(Empty e);
10+
}
11+
}

cpp-tests/tests/empty_type/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "test_common.hpp"
2+
3+
#include <empty_type.hpp>
4+
5+
int main() {
6+
auto empty = empty_type::get_empty_type();
7+
8+
empty_type::send_empty_type(empty);
9+
10+
return 0;
11+
}

fixtures/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ uniffi-fixture-time = { git = "https://github.com/NordSecurity/uniffi-rs.git", t
2222
uniffi-fixture-trait-methods = { git = "https://github.com/NordSecurity/uniffi-rs.git", tag = "v0.3.1+v0.25.0" }
2323
uniffi-custom-types-builtin = { path = "custom-types-builtin" }
2424
uniffi-enum-style-test = { path = "enum-style-test" }
25+
uniffi-empty-type = { path = "empty-type" }

fixtures/empty-type/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "uniffi-empty-type"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["lib", "cdylib"]
8+
name = "uniffi_empty_type"
9+
10+
[dependencies]
11+
uniffi = { workspace = true }
12+
13+
[build-dependencies]
14+
uniffi = { workspace = true, features = ["build"] }

fixtures/empty-type/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
uniffi::generate_scaffolding("src/empty_type.udl").unwrap();
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dictionary Empty {};
2+
3+
namespace empty_type{
4+
Empty get_empty_type();
5+
void send_empty_type(Empty e);
6+
};

0 commit comments

Comments
 (0)