Skip to content

Commit c8481e3

Browse files
committed
add example where we use a type from a third party crate
should also be an important test case
1 parent 9e82f6b commit c8481e3

File tree

7 files changed

+263
-2
lines changed

7 files changed

+263
-2
lines changed

Cargo.lock

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

example/buffi_example/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ crate-type = ["staticlib"]
1111
buffi_macro = { path = "../../buffi_macro" }
1212
bincode = "1.3.3"
1313
serde = { version = "1.0.214", features = ["derive"] }
14-
tokio = { version = "1.41.1", features = ["rt-multi-thread"] }
14+
tokio = { version = "1.41.1", features = ["rt-multi-thread"] }
15+
cgmath = { version = "0.18.0", features = ["serde"] }

example/buffi_example/src/include/BUFFI_NAMESPACE.hpp

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,39 @@ namespace BUFFI_NAMESPACE {
102102
static Result_i64_SerializableError bincodeDeserialize(std::vector<uint8_t>);
103103
};
104104

105+
struct Result_void_SerializableError {
106+
107+
struct Ok {
108+
std::tuple<std::tuple<>> value;
109+
110+
friend bool operator==(const Ok&, const Ok&);
111+
std::vector<uint8_t> bincodeSerialize() const;
112+
static Ok bincodeDeserialize(std::vector<uint8_t>);
113+
};
114+
115+
struct Err {
116+
std::tuple<BUFFI_NAMESPACE::SerializableError> value;
117+
118+
friend bool operator==(const Err&, const Err&);
119+
std::vector<uint8_t> bincodeSerialize() const;
120+
static Err bincodeDeserialize(std::vector<uint8_t>);
121+
};
122+
123+
std::variant<Ok, Err> value;
124+
125+
friend bool operator==(const Result_void_SerializableError&, const Result_void_SerializableError&);
126+
std::vector<uint8_t> bincodeSerialize() const;
127+
static Result_void_SerializableError bincodeDeserialize(std::vector<uint8_t>);
128+
};
129+
130+
struct Point1_f64 {
131+
double x;
132+
133+
friend bool operator==(const Point1_f64&, const Point1_f64&);
134+
std::vector<uint8_t> bincodeSerialize() const;
135+
static Point1_f64 bincodeDeserialize(std::vector<uint8_t>);
136+
};
137+
105138
} // end of namespace BUFFI_NAMESPACE
106139

107140

@@ -150,6 +183,48 @@ BUFFI_NAMESPACE::CustomType serde::Deserializable<BUFFI_NAMESPACE::CustomType>::
150183
return obj;
151184
}
152185

186+
namespace BUFFI_NAMESPACE {
187+
188+
inline bool operator==(const Point1_f64 &lhs, const Point1_f64 &rhs) {
189+
if (!(lhs.x == rhs.x)) { return false; }
190+
return true;
191+
}
192+
193+
inline std::vector<uint8_t> Point1_f64::bincodeSerialize() const {
194+
auto serializer = serde::BincodeSerializer();
195+
serde::Serializable<Point1_f64>::serialize(*this, serializer);
196+
return std::move(serializer).bytes();
197+
}
198+
199+
inline Point1_f64 Point1_f64::bincodeDeserialize(std::vector<uint8_t> input) {
200+
auto deserializer = serde::BincodeDeserializer(input);
201+
auto value = serde::Deserializable<Point1_f64>::deserialize(deserializer);
202+
if (deserializer.get_buffer_offset() < input.size()) {
203+
throw serde::deserialization_error("Some input bytes were not read");
204+
}
205+
return value;
206+
}
207+
208+
} // end of namespace BUFFI_NAMESPACE
209+
210+
template <>
211+
template <typename Serializer>
212+
void serde::Serializable<BUFFI_NAMESPACE::Point1_f64>::serialize(const BUFFI_NAMESPACE::Point1_f64 &obj, Serializer &serializer) {
213+
serializer.increase_container_depth();
214+
serde::Serializable<decltype(obj.x)>::serialize(obj.x, serializer);
215+
serializer.decrease_container_depth();
216+
}
217+
218+
template <>
219+
template <typename Deserializer>
220+
BUFFI_NAMESPACE::Point1_f64 serde::Deserializable<BUFFI_NAMESPACE::Point1_f64>::deserialize(Deserializer &deserializer) {
221+
deserializer.increase_container_depth();
222+
BUFFI_NAMESPACE::Point1_f64 obj;
223+
obj.x = serde::Deserializable<decltype(obj.x)>::deserialize(deserializer);
224+
deserializer.decrease_container_depth();
225+
return obj;
226+
}
227+
153228
namespace BUFFI_NAMESPACE {
154229

155230
inline bool operator==(const Result_CustomType_SerializableError &lhs, const Result_CustomType_SerializableError &rhs) {
@@ -504,6 +579,124 @@ BUFFI_NAMESPACE::Result_i64_SerializableError::Err serde::Deserializable<BUFFI_N
504579
return obj;
505580
}
506581

582+
namespace BUFFI_NAMESPACE {
583+
584+
inline bool operator==(const Result_void_SerializableError &lhs, const Result_void_SerializableError &rhs) {
585+
if (!(lhs.value == rhs.value)) { return false; }
586+
return true;
587+
}
588+
589+
inline std::vector<uint8_t> Result_void_SerializableError::bincodeSerialize() const {
590+
auto serializer = serde::BincodeSerializer();
591+
serde::Serializable<Result_void_SerializableError>::serialize(*this, serializer);
592+
return std::move(serializer).bytes();
593+
}
594+
595+
inline Result_void_SerializableError Result_void_SerializableError::bincodeDeserialize(std::vector<uint8_t> input) {
596+
auto deserializer = serde::BincodeDeserializer(input);
597+
auto value = serde::Deserializable<Result_void_SerializableError>::deserialize(deserializer);
598+
if (deserializer.get_buffer_offset() < input.size()) {
599+
throw serde::deserialization_error("Some input bytes were not read");
600+
}
601+
return value;
602+
}
603+
604+
} // end of namespace BUFFI_NAMESPACE
605+
606+
template <>
607+
template <typename Serializer>
608+
void serde::Serializable<BUFFI_NAMESPACE::Result_void_SerializableError>::serialize(const BUFFI_NAMESPACE::Result_void_SerializableError &obj, Serializer &serializer) {
609+
serializer.increase_container_depth();
610+
serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
611+
serializer.decrease_container_depth();
612+
}
613+
614+
template <>
615+
template <typename Deserializer>
616+
BUFFI_NAMESPACE::Result_void_SerializableError serde::Deserializable<BUFFI_NAMESPACE::Result_void_SerializableError>::deserialize(Deserializer &deserializer) {
617+
deserializer.increase_container_depth();
618+
BUFFI_NAMESPACE::Result_void_SerializableError obj;
619+
obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
620+
deserializer.decrease_container_depth();
621+
return obj;
622+
}
623+
624+
namespace BUFFI_NAMESPACE {
625+
626+
inline bool operator==(const Result_void_SerializableError::Ok &lhs, const Result_void_SerializableError::Ok &rhs) {
627+
if (!(lhs.value == rhs.value)) { return false; }
628+
return true;
629+
}
630+
631+
inline std::vector<uint8_t> Result_void_SerializableError::Ok::bincodeSerialize() const {
632+
auto serializer = serde::BincodeSerializer();
633+
serde::Serializable<Result_void_SerializableError::Ok>::serialize(*this, serializer);
634+
return std::move(serializer).bytes();
635+
}
636+
637+
inline Result_void_SerializableError::Ok Result_void_SerializableError::Ok::bincodeDeserialize(std::vector<uint8_t> input) {
638+
auto deserializer = serde::BincodeDeserializer(input);
639+
auto value = serde::Deserializable<Result_void_SerializableError::Ok>::deserialize(deserializer);
640+
if (deserializer.get_buffer_offset() < input.size()) {
641+
throw serde::deserialization_error("Some input bytes were not read");
642+
}
643+
return value;
644+
}
645+
646+
} // end of namespace BUFFI_NAMESPACE
647+
648+
template <>
649+
template <typename Serializer>
650+
void serde::Serializable<BUFFI_NAMESPACE::Result_void_SerializableError::Ok>::serialize(const BUFFI_NAMESPACE::Result_void_SerializableError::Ok &obj, Serializer &serializer) {
651+
serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
652+
}
653+
654+
template <>
655+
template <typename Deserializer>
656+
BUFFI_NAMESPACE::Result_void_SerializableError::Ok serde::Deserializable<BUFFI_NAMESPACE::Result_void_SerializableError::Ok>::deserialize(Deserializer &deserializer) {
657+
BUFFI_NAMESPACE::Result_void_SerializableError::Ok obj;
658+
obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
659+
return obj;
660+
}
661+
662+
namespace BUFFI_NAMESPACE {
663+
664+
inline bool operator==(const Result_void_SerializableError::Err &lhs, const Result_void_SerializableError::Err &rhs) {
665+
if (!(lhs.value == rhs.value)) { return false; }
666+
return true;
667+
}
668+
669+
inline std::vector<uint8_t> Result_void_SerializableError::Err::bincodeSerialize() const {
670+
auto serializer = serde::BincodeSerializer();
671+
serde::Serializable<Result_void_SerializableError::Err>::serialize(*this, serializer);
672+
return std::move(serializer).bytes();
673+
}
674+
675+
inline Result_void_SerializableError::Err Result_void_SerializableError::Err::bincodeDeserialize(std::vector<uint8_t> input) {
676+
auto deserializer = serde::BincodeDeserializer(input);
677+
auto value = serde::Deserializable<Result_void_SerializableError::Err>::deserialize(deserializer);
678+
if (deserializer.get_buffer_offset() < input.size()) {
679+
throw serde::deserialization_error("Some input bytes were not read");
680+
}
681+
return value;
682+
}
683+
684+
} // end of namespace BUFFI_NAMESPACE
685+
686+
template <>
687+
template <typename Serializer>
688+
void serde::Serializable<BUFFI_NAMESPACE::Result_void_SerializableError::Err>::serialize(const BUFFI_NAMESPACE::Result_void_SerializableError::Err &obj, Serializer &serializer) {
689+
serde::Serializable<decltype(obj.value)>::serialize(obj.value, serializer);
690+
}
691+
692+
template <>
693+
template <typename Deserializer>
694+
BUFFI_NAMESPACE::Result_void_SerializableError::Err serde::Deserializable<BUFFI_NAMESPACE::Result_void_SerializableError::Err>::deserialize(Deserializer &deserializer) {
695+
BUFFI_NAMESPACE::Result_void_SerializableError::Err obj;
696+
obj.value = serde::Deserializable<decltype(obj.value)>::deserialize(deserializer);
697+
return obj;
698+
}
699+
507700
namespace BUFFI_NAMESPACE {
508701

509702
inline bool operator==(const SerializableError &lhs, const SerializableError &rhs) {

example/buffi_example/src/include/buffi_example_api_functions.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ extern "C" TestClient* get_test_client();
1010
extern "C" size_t buffi_async_function(TestClient* this_ptr, const std::uint8_t* content, size_t content_size, std::uint8_t** out_ptr);
1111
extern "C" size_t buffi_client_function(TestClient* this_ptr, const std::uint8_t* input, size_t input_size, std::uint8_t** out_ptr);
1212
extern "C" size_t buffi_free_standing_function(const std::uint8_t* input, size_t input_size, std::uint8_t** out_ptr);
13+
extern "C" size_t buffi_use_foreign_type_and_return_nothing(TestClient* this_ptr, const std::uint8_t* point, size_t point_size, std::uint8_t** out_ptr);
1314
extern "C" void buffi_free_byte_buffer(std::uint8_t* ptr, size_t size);

example/buffi_example/src/include/buffi_example_testclient.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ class TestClientHolder {
6262
}
6363
}
6464

65+
// Here we use a type from a third party crate and return `()`
66+
inline void use_foreign_type_and_return_nothing(const Point1_f64& point) {
67+
auto serializer_point = serde::BincodeSerializer();
68+
serde::Serializable<Point1_f64>::serialize(point, serializer_point);
69+
std::vector<uint8_t> point_serialized = std::move(serializer_point).bytes();
70+
uint8_t* out_ptr = nullptr;
71+
72+
size_t res_size = buffi_use_foreign_type_and_return_nothing(this->inner, point_serialized.data(), point_serialized.size(), &out_ptr);
73+
74+
std::vector<uint8_t> serialized_result(out_ptr, out_ptr + res_size);
75+
Result_void_SerializableError out = Result_void_SerializableError::bincodeDeserialize(serialized_result);
76+
buffi_free_byte_buffer(out_ptr, res_size);
77+
78+
if (out.value.index() == 0) { // Ok
79+
return;
80+
} else { // Err
81+
auto err = std::get<1>(out.value);
82+
auto error = std::get<0>(err.value);
83+
throw error;
84+
}
85+
}
86+
6587
};
6688

6789
} // end of namespace BUFFI_NAMESPACE

example/buffi_example/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(unexpected_cfgs)]
22

3+
use cgmath::Point1;
34
use serde::Serialize;
45
use std::sync::Arc;
56
use tokio::runtime::Runtime;
@@ -47,6 +48,12 @@ impl TestClient {
4748
itself: None,
4849
})
4950
}
51+
52+
/// Here we use a type from a third party crate and return `()`
53+
pub fn use_foreign_type_and_return_nothing(&self, point: Point1<f64>) -> Result<(), String> {
54+
println!("{:?}", point);
55+
Ok(())
56+
}
5057
}
5158

5259
/// This function frees a byte buffer allocated on the Rust side

example/generate_bindings/api_config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ namespace = "BUFFI_NAMESPACE"
22
api_lib_name = "buffi_example"
33
parent_crate = "buffi_example"
44
rustdoc_crates = [
5-
"buffi_example"
5+
"buffi_example",
6+
"cgmath"
67
]

0 commit comments

Comments
 (0)