-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martynas Gurskas <[email protected]>
- Loading branch information
Showing
17 changed files
with
337 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{%- match config.custom_types.get(name.as_str()) %} | ||
{%- when Some with (type_config) %} | ||
{%- match type_config.type_name %} | ||
{%- when Some with (type_name) %} | ||
{%- let ffi_type_name = builtin|ffi_type|ffi_type_name %} | ||
{{ type_name }} uniffi::{{ ffi_converter_name }}::lift(RustBuffer buff) { | ||
auto builtin_val = {{ builtin|lift_fn }}(buff); | ||
|
||
return {{ type_config.into_custom.render("builtin_val") }}; | ||
} | ||
|
||
RustBuffer uniffi::{{ ffi_converter_name }}::lower(const {{ type_name }} &val) { | ||
auto builtin_val = {{ type_config.from_custom.render("val") }}; | ||
|
||
return {{ builtin|lower_fn }}(builtin_val); | ||
} | ||
|
||
{{ type_name }} uniffi::{{ ffi_converter_name }}::read(uniffi::RustStream &stream) { | ||
auto builtin_val = {{ builtin|read_fn }}(stream); | ||
|
||
return {{ type_config.into_custom.render("builtin_val") }}; | ||
} | ||
|
||
void uniffi::{{ ffi_converter_name }}::write(uniffi::RustStream &stream, const {{ type_name }} &val) { | ||
auto builtin_val = {{ type_config.from_custom.render("val") }}; | ||
|
||
{{ builtin|write_fn }}(stream, builtin_val); | ||
} | ||
|
||
int32_t uniffi::{{ ffi_converter_name }}::allocation_size(const {{ type_name }} &val) { | ||
auto builtin_val = {{ type_config.from_custom.render("val") }}; | ||
|
||
return {{ builtin|allocation_size_fn }}(builtin_val); | ||
} | ||
{%- else %} | ||
{%- endmatch %} | ||
{%- else %} | ||
{%- endmatch %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
{%- match config.custom_types.get(name.as_str()) %} | ||
{%- when Some with (type_config) %} | ||
{%- match type_config.imports %} | ||
{%- when Some with (includes) %} | ||
{%- for include in includes %} | ||
{{ self.add_include(include) }} | ||
{%- endfor %} | ||
{%- match type_config.type_name %} | ||
{%- when Some with (type_name) %} | ||
struct {{ typ|ffi_converter_name }} { | ||
static {{ name }} lift(RustBuffer); | ||
static RustBuffer lower(const {{ name }} &); | ||
static {{ name }} read(uniffi::RustStream &); | ||
static void write(RustStream &, const {{ name }} &); | ||
static int32_t allocation_size(const {{ name }} &); | ||
}; | ||
{%- else %} | ||
{%- endmatch %} | ||
{%- when None %} | ||
typedef {{ builtin|type_name }} {{ name }}; | ||
{%- endmatch %} | ||
{%- else -%} | ||
typedef struct {{ builtin|ffi_converter_name }} {{ typ|ffi_converter_name }}; | ||
{%- endmatch %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <test_common.hpp> | ||
|
||
#include <custom_types_builtin.hpp> | ||
|
||
void assert_custom_type(custom_types_builtin::CustomTypesBuiltin& type) { | ||
auto table = std::unordered_map<std::string, std::string>{{"hello", "world"}}; | ||
auto bytes = std::vector<uint8_t>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||
|
||
ASSERT_EQ(type.string, "Hello, world!"); | ||
ASSERT_EQ(type.custom_string.string, "Custom string"); | ||
ASSERT_EQ(type.array, std::vector<std::string>{"Hello, world!"}); | ||
ASSERT_EQ(type.bytes, bytes); | ||
ASSERT_EQ(type.table, table); | ||
ASSERT_EQ(type.boolean, true); | ||
ASSERT_EQ(type.int8, std::numeric_limits<int8_t>::max()); | ||
ASSERT_EQ(type.int16, std::numeric_limits<int16_t>::max()); | ||
ASSERT_EQ(type.int32, std::numeric_limits<int32_t>::max()); | ||
ASSERT_EQ(type.int64, std::numeric_limits<int64_t>::max()); | ||
ASSERT_EQ(type.uint8, std::numeric_limits<uint8_t>::max()); | ||
ASSERT_EQ(type.uint16, std::numeric_limits<uint16_t>::max()); | ||
ASSERT_EQ(type.uint32, std::numeric_limits<uint32_t>::max()); | ||
ASSERT_EQ(type.uint64, std::numeric_limits<uint64_t>::max()); | ||
ASSERT_EQ(type.flt, std::numeric_limits<float>::max()); | ||
ASSERT_EQ(type.dbl, std::numeric_limits<double>::max()); | ||
} | ||
|
||
int main() { | ||
auto custom_type = custom_types_builtin::get_custom_types_builtin(); | ||
assert_custom_type(custom_type); | ||
|
||
custom_type = custom_types_builtin::return_custom_types_builtin(custom_type); | ||
assert_custom_type(custom_type); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
struct CustomString { | ||
std::string string; | ||
|
||
CustomString(const std::string &string): string(string) { } | ||
|
||
std::string to_string() const { | ||
return this->string; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "uniffi-custom-types-builtin" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["lib", "cdylib"] | ||
name = "uniffi_cpp_custom_types_builtin" | ||
|
||
[dependencies] | ||
uniffi = { git = "https://github.com/NordSecurity/uniffi-rs.git", branch = "nordsec.0.25.0" } | ||
once_cell = "1.12" | ||
paste = "1.0" | ||
thiserror = "1.0" | ||
|
||
[build-dependencies] | ||
uniffi = { git = "https://github.com/NordSecurity/uniffi-rs.git", branch = "nordsec.0.25.0", features = ["build"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
uniffi::generate_scaffolding("src/custom_types_builtin.udl").unwrap(); | ||
} |
71 changes: 71 additions & 0 deletions
71
fixtures/custom-types-builtin/src/custom_types_builtin.udl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
[Custom] | ||
typedef string MyString; | ||
|
||
[Custom] | ||
typedef string CustomString; | ||
|
||
[Custom] | ||
typedef bytes Bytes; | ||
|
||
[Custom] | ||
typedef sequence<string> Array; | ||
|
||
[Custom] | ||
typedef record<string, string> Table; | ||
|
||
[Custom] | ||
typedef boolean Boolean; | ||
|
||
[Custom] | ||
typedef i8 Int8; | ||
|
||
[Custom] | ||
typedef i16 Int16; | ||
|
||
[Custom] | ||
typedef i32 Int32; | ||
|
||
[Custom] | ||
typedef i64 Int64; | ||
|
||
[Custom] | ||
typedef u8 UInt8; | ||
|
||
[Custom] | ||
typedef u16 UInt16; | ||
|
||
[Custom] | ||
typedef u32 UInt32; | ||
|
||
[Custom] | ||
typedef u64 UInt64; | ||
|
||
[Custom] | ||
typedef float Float; | ||
|
||
[Custom] | ||
typedef double Double; | ||
|
||
dictionary CustomTypesBuiltin { | ||
MyString string; | ||
CustomString custom_string; | ||
Array array; | ||
Bytes bytes; | ||
Table table; | ||
Boolean boolean; | ||
Int8 int8; | ||
Int16 int16; | ||
Int32 int32; | ||
Int64 int64; | ||
UInt8 uint8; | ||
UInt16 uint16; | ||
UInt32 uint32; | ||
UInt64 uint64; | ||
Float flt; | ||
Double dbl; | ||
}; | ||
|
||
namespace custom_types_builtin { | ||
CustomTypesBuiltin get_custom_types_builtin(); | ||
CustomTypesBuiltin return_custom_types_builtin(CustomTypesBuiltin custom_types); | ||
}; |
Oops, something went wrong.