-
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.
Scaffolding: add support for custom types
Signed-off-by: Martynas Gurskas <[email protected]>
- Loading branch information
Showing
13 changed files
with
246 additions
and
7 deletions.
There are no files selected for viewing
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
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,36 @@ | ||
{%- let converter = typ|custom_converter_name -%} | ||
{{ type_name }} {{ converter }}::lift(RustBuffer buff) { | ||
auto stream = RustStream(&buff); | ||
auto builtin_val = {{ builtin|read_fn }}(stream); | ||
|
||
return Uniffi{{ converter }}::into_custom(builtin_val); | ||
} | ||
|
||
RustBuffer {{ converter }}::lower(const {{ type_name }} &val) { | ||
auto buf = rustbuffer_alloc(allocation_size(val)); | ||
auto stream = RustStream(&buf); | ||
auto builtin_val = Uniffi{{ converter }}::from_custom(val); | ||
|
||
{{ builtin|write_fn }}(stream, builtin_val); | ||
|
||
return std::move(buf); | ||
} | ||
|
||
{{ type_name }} {{ converter }}::read(RustStream &stream) { | ||
auto builtin_val = {{ builtin|read_fn }}(stream); | ||
|
||
return Uniffi{{ converter }}::into_custom(builtin_val); | ||
} | ||
|
||
void {{ converter }}::write(RustStream &stream, const {{ type_name }} &val) { | ||
auto builtin_val = Uniffi{{ converter }}::from_custom(val); | ||
|
||
{{ builtin|write_fn }}(stream, builtin_val); | ||
} | ||
|
||
int32_t {{ converter }}::allocation_size(const {{ type_name }} &val) { | ||
auto builtin_val = Uniffi{{ converter }}::from_custom(val); | ||
|
||
return {{ builtin|allocation_size_fn }}(builtin_val); | ||
} | ||
|
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,8 @@ | ||
struct {{ typ|custom_converter_name }} { | ||
static {{ typ|type_name }} lift(RustBuffer); | ||
static RustBuffer lower(const {{ typ|type_name }} &); | ||
static {{ typ|type_name }} read(RustStream &); | ||
static void write(RustStream &, const {{ typ|type_name }} &); | ||
static int32_t allocation_size(const {{ typ|type_name }} &); | ||
}; | ||
typedef struct {{ typ|custom_converter_name }} {{ typ|ffi_converter_name }}; |
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
18 changes: 18 additions & 0 deletions
18
cpp-tests/scaffolding_tests/custom_types/lib_custom_types.cpp
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,18 @@ | ||
#include "lib_custom_types.hpp" | ||
|
||
custom_types::CustomTypesDemo custom_types::get_custom_types_demo(std::optional<custom_types::CustomTypesDemo> demo) { | ||
return demo.value_or(custom_types::CustomTypesDemo{ | ||
.url = "http://example.com/", | ||
.handle = 123, | ||
.time_interval_ms = 456000, | ||
.time_interval_sec_dbl = 456.0, | ||
.time_interval_sec_flt = 777.0f, | ||
}); | ||
} | ||
|
||
custom_types::ExampleCustomType custom_types::get_example_custom_type() { | ||
UniffiCustomTypeConverterUrl url_converter; | ||
return "abadidea"; | ||
} | ||
|
||
#include <custom_types_cpp_scaffolding.cpp> |
84 changes: 84 additions & 0 deletions
84
cpp-tests/scaffolding_tests/custom_types/lib_custom_types.hpp
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,84 @@ | ||
#include <cstdint> | ||
#include <optional> | ||
#include <string> | ||
|
||
namespace { | ||
namespace custom_types { | ||
typedef std::string ExampleCustomType; | ||
struct UniffiCustomTypeConverterExampleCustomType { | ||
static ExampleCustomType into_custom(std::string val) { | ||
return val; | ||
} | ||
|
||
static std::string from_custom(ExampleCustomType val) { | ||
return val; | ||
} | ||
}; | ||
|
||
typedef std::string Url; | ||
struct UniffiCustomTypeConverterUrl { | ||
static Url into_custom(std::string val) { | ||
return val; | ||
} | ||
|
||
static std::string from_custom(Url val) { | ||
return val; | ||
} | ||
}; | ||
|
||
typedef int64_t Handle; | ||
struct UniffiCustomTypeConverterHandle { | ||
static Handle into_custom(int64_t val) { | ||
return val; | ||
} | ||
|
||
static int64_t from_custom(Handle val) { | ||
return val; | ||
} | ||
}; | ||
|
||
typedef int64_t TimeIntervalMs; | ||
struct UniffiCustomTypeConverterTimeIntervalMs { | ||
static TimeIntervalMs into_custom(int64_t val) { | ||
return val; | ||
} | ||
|
||
static int64_t from_custom(TimeIntervalMs val) { | ||
return val; | ||
} | ||
}; | ||
|
||
typedef double TimeIntervalSecDbl; | ||
struct UniffiCustomTypeConverterTimeIntervalSecDbl { | ||
static TimeIntervalSecDbl into_custom(double val) { | ||
return val; | ||
} | ||
|
||
static double from_custom(TimeIntervalSecDbl val) { | ||
return val; | ||
} | ||
}; | ||
|
||
typedef float TimeIntervalSecFlt; | ||
struct UniffiCustomTypeConverterTimeIntervalSecFlt { | ||
static TimeIntervalSecFlt into_custom(float val) { | ||
return val; | ||
} | ||
|
||
static float from_custom(TimeIntervalSecFlt val) { | ||
return val; | ||
} | ||
}; | ||
|
||
struct CustomTypesDemo { | ||
Url url; | ||
Handle handle; | ||
TimeIntervalMs time_interval_ms; | ||
TimeIntervalSecDbl time_interval_sec_dbl; | ||
TimeIntervalSecFlt time_interval_sec_flt; | ||
}; | ||
|
||
CustomTypesDemo get_custom_types_demo(std::optional<CustomTypesDemo> demo); | ||
ExampleCustomType get_example_custom_type(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
cpp-tests/scaffolding_tests/custom_types_builtin/lib_custom_types_builtin.cpp
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,30 @@ | ||
#include "lib_custom_types_builtin.hpp" | ||
|
||
#include <limits> | ||
|
||
custom_types_builtin::CustomTypesBuiltin custom_types_builtin::get_custom_types_builtin() { | ||
return custom_types_builtin::CustomTypesBuiltin{ | ||
.string = "Hello, world!", | ||
.custom_string = custom_types_builtin::CustomString{"Custom string"}, | ||
.array = std::vector<std::string>{"Hello, world!"}, | ||
.bytes = std::vector<uint8_t>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, | ||
.table = std::unordered_map<std::string, std::string>{{"hello", "world"}}, | ||
.boolean = true, | ||
.int8 = std::numeric_limits<int8_t>::max(), | ||
.int16 = std::numeric_limits<int16_t>::max(), | ||
.int32 = std::numeric_limits<int32_t>::max(), | ||
.int64 = std::numeric_limits<int64_t>::max(), | ||
.uint8 = std::numeric_limits<uint8_t>::max(), | ||
.uint16 = std::numeric_limits<uint16_t>::max(), | ||
.uint32 = std::numeric_limits<uint32_t>::max(), | ||
.uint64 = std::numeric_limits<uint64_t>::max(), | ||
.flt = std::numeric_limits<float>::max(), | ||
.dbl = std::numeric_limits<double>::max(), | ||
}; | ||
} | ||
|
||
custom_types_builtin::CustomTypesBuiltin custom_types_builtin::return_custom_types_builtin(custom_types_builtin::CustomTypesBuiltin type) { | ||
return type; | ||
} | ||
|
||
#include <custom_types_builtin_cpp_scaffolding.cpp> |
56 changes: 56 additions & 0 deletions
56
cpp-tests/scaffolding_tests/custom_types_builtin/lib_custom_types_builtin.hpp
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,56 @@ | ||
#include <string> | ||
#include <vector> | ||
#include <unordered_map> | ||
#include <cstdint> | ||
|
||
#define DEFINE_CUSTOM_TYPE(custom, underlying) \ | ||
typedef underlying custom; \ | ||
struct UniffiCustomTypeConverter##custom { \ | ||
static custom into_custom(underlying val) { return val; } \ | ||
static underlying from_custom(custom val) { return val; } \ | ||
}; | ||
|
||
namespace { | ||
namespace custom_types_builtin { | ||
typedef std::unordered_map<std::string, std::string> StringMap; | ||
|
||
DEFINE_CUSTOM_TYPE(MyString, std::string) | ||
DEFINE_CUSTOM_TYPE(CustomString, std::string) | ||
DEFINE_CUSTOM_TYPE(Array, std::vector<std::string>) | ||
DEFINE_CUSTOM_TYPE(Bytes, std::vector<uint8_t>) | ||
DEFINE_CUSTOM_TYPE(Table, StringMap) | ||
DEFINE_CUSTOM_TYPE(Boolean, bool) | ||
DEFINE_CUSTOM_TYPE(Int8, int8_t) | ||
DEFINE_CUSTOM_TYPE(Int16, int16_t) | ||
DEFINE_CUSTOM_TYPE(Int32, int32_t) | ||
DEFINE_CUSTOM_TYPE(Int64, int64_t) | ||
DEFINE_CUSTOM_TYPE(UInt8, uint8_t) | ||
DEFINE_CUSTOM_TYPE(UInt16, uint16_t) | ||
DEFINE_CUSTOM_TYPE(UInt32, uint32_t) | ||
DEFINE_CUSTOM_TYPE(UInt64, uint64_t) | ||
DEFINE_CUSTOM_TYPE(Float, float) | ||
DEFINE_CUSTOM_TYPE(Double, double) | ||
|
||
struct 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; | ||
}; | ||
|
||
CustomTypesBuiltin get_custom_types_builtin(); | ||
CustomTypesBuiltin return_custom_types_builtin(CustomTypesBuiltin type); | ||
} | ||
} |
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