Skip to content

Commit 2d735c1

Browse files
authored
Check in bindings, add CI for keeping them up to date (#220)
1 parent 5b93f5e commit 2d735c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+5642
-2
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ jobs:
119119
run: cargo build -p temporal_capi
120120
- name: Regen
121121
run: cargo run -p diplomat-gen
122+
- name: Check diff
123+
run: git diff --exit-code
122124
# Todo: eventually we should check in bindings and test them
123125
- name: Makefile tests
124126
run: cd temporal_capi/cpp_tests && make

temporal_capi/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
# To be checked in later once it stabilizes a bit
2-
bindings/
31
*.out
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#ifndef DIPLOMAT_RUNTIME_CPP_H
2+
#define DIPLOMAT_RUNTIME_CPP_H
3+
4+
#include <optional>
5+
#include <string>
6+
#include <type_traits>
7+
#include <variant>
8+
9+
#if __cplusplus >= 202002L
10+
#include <span>
11+
#else
12+
#include <array>
13+
#endif
14+
15+
namespace diplomat {
16+
17+
namespace capi {
18+
extern "C" {
19+
20+
static_assert(sizeof(char) == sizeof(uint8_t), "your architecture's `char` is not 8 bits");
21+
static_assert(sizeof(char16_t) == sizeof(uint16_t), "your architecture's `char16_t` is not 16 bits");
22+
static_assert(sizeof(char32_t) == sizeof(uint32_t), "your architecture's `char32_t` is not 32 bits");
23+
24+
typedef struct DiplomatWrite {
25+
void* context;
26+
char* buf;
27+
size_t len;
28+
size_t cap;
29+
bool grow_failed;
30+
void (*flush)(struct DiplomatWrite*);
31+
bool (*grow)(struct DiplomatWrite*, size_t);
32+
} DiplomatWrite;
33+
34+
bool diplomat_is_str(const char* buf, size_t len);
35+
36+
#define MAKE_SLICES(name, c_ty) \
37+
typedef struct Diplomat##name##View { \
38+
const c_ty* data; \
39+
size_t len; \
40+
} Diplomat##name##View; \
41+
typedef struct Diplomat##name##ViewMut { \
42+
c_ty* data; \
43+
size_t len; \
44+
} Diplomat##name##ViewMut; \
45+
typedef struct Diplomat##name##Array { \
46+
const c_ty* data; \
47+
size_t len; \
48+
} Diplomat##name##Array;
49+
50+
#define MAKE_SLICES_AND_OPTIONS(name, c_ty) \
51+
MAKE_SLICES(name, c_ty) \
52+
typedef struct Option##name {union { c_ty ok; }; bool is_ok; } Option##name;
53+
54+
MAKE_SLICES_AND_OPTIONS(I8, int8_t)
55+
MAKE_SLICES_AND_OPTIONS(U8, uint8_t)
56+
MAKE_SLICES_AND_OPTIONS(I16, int16_t)
57+
MAKE_SLICES_AND_OPTIONS(U16, uint16_t)
58+
MAKE_SLICES_AND_OPTIONS(I32, int32_t)
59+
MAKE_SLICES_AND_OPTIONS(U32, uint32_t)
60+
MAKE_SLICES_AND_OPTIONS(I64, int64_t)
61+
MAKE_SLICES_AND_OPTIONS(U64, uint64_t)
62+
MAKE_SLICES_AND_OPTIONS(Isize, intptr_t)
63+
MAKE_SLICES_AND_OPTIONS(Usize, size_t)
64+
MAKE_SLICES_AND_OPTIONS(F32, float)
65+
MAKE_SLICES_AND_OPTIONS(F64, double)
66+
MAKE_SLICES_AND_OPTIONS(Bool, bool)
67+
MAKE_SLICES_AND_OPTIONS(Char, char32_t)
68+
MAKE_SLICES(String, char)
69+
MAKE_SLICES(String16, char16_t)
70+
MAKE_SLICES(Strings, DiplomatStringView)
71+
MAKE_SLICES(Strings16, DiplomatString16View)
72+
73+
} // extern "C"
74+
} // namespace capi
75+
76+
extern "C" inline void _flush(capi::DiplomatWrite* w) {
77+
std::string* string = reinterpret_cast<std::string*>(w->context);
78+
string->resize(w->len);
79+
};
80+
81+
extern "C" inline bool _grow(capi::DiplomatWrite* w, uintptr_t requested) {
82+
std::string* string = reinterpret_cast<std::string*>(w->context);
83+
string->resize(requested);
84+
w->cap = string->length();
85+
w->buf = &(*string)[0];
86+
return true;
87+
};
88+
89+
inline capi::DiplomatWrite WriteFromString(std::string& string) {
90+
capi::DiplomatWrite w;
91+
w.context = &string;
92+
w.buf = &string[0];
93+
w.len = string.length();
94+
w.cap = string.length();
95+
// Will never become true, as _grow is infallible.
96+
w.grow_failed = false;
97+
w.flush = _flush;
98+
w.grow = _grow;
99+
return w;
100+
};
101+
102+
template<class T> struct Ok {
103+
T inner;
104+
Ok(T&& i): inner(std::move(i)) {}
105+
// We don't want to expose an lvalue-capable constructor in general
106+
// however there is no problem doing this for trivially copyable types
107+
template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
108+
Ok(T i): inner(i) {}
109+
Ok() = default;
110+
Ok(Ok&&) noexcept = default;
111+
Ok(const Ok &) = default;
112+
Ok& operator=(const Ok&) = default;
113+
Ok& operator=(Ok&&) noexcept = default;
114+
};
115+
116+
template<class T> struct Err {
117+
T inner;
118+
Err(T&& i): inner(std::move(i)) {}
119+
// We don't want to expose an lvalue-capable constructor in general
120+
// however there is no problem doing this for trivially copyable types
121+
template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
122+
Err(T i): inner(i) {}
123+
Err() = default;
124+
Err(Err&&) noexcept = default;
125+
Err(const Err &) = default;
126+
Err& operator=(const Err&) = default;
127+
Err& operator=(Err&&) noexcept = default;
128+
};
129+
130+
template<class T, class E>
131+
class result {
132+
private:
133+
std::variant<Ok<T>, Err<E>> val;
134+
public:
135+
result(Ok<T>&& v): val(std::move(v)) {}
136+
result(Err<E>&& v): val(std::move(v)) {}
137+
result() = default;
138+
result(const result &) = default;
139+
result& operator=(const result&) = default;
140+
result& operator=(result&&) noexcept = default;
141+
result(result &&) noexcept = default;
142+
~result() = default;
143+
bool is_ok() const {
144+
return std::holds_alternative<Ok<T>>(this->val);
145+
};
146+
bool is_err() const {
147+
return std::holds_alternative<Err<E>>(this->val);
148+
};
149+
150+
std::optional<T> ok() && {
151+
if (!this->is_ok()) {
152+
return std::nullopt;
153+
}
154+
return std::make_optional(std::move(std::get<Ok<T>>(std::move(this->val)).inner));
155+
};
156+
std::optional<E> err() && {
157+
if (!this->is_err()) {
158+
return std::nullopt;
159+
}
160+
return std::make_optional(std::move(std::get<Err<E>>(std::move(this->val)).inner));
161+
}
162+
163+
void set_ok(T&& t) {
164+
this->val = Ok<T>(std::move(t));
165+
}
166+
167+
void set_err(E&& e) {
168+
this->val = Err<E>(std::move(e));
169+
}
170+
171+
template<typename T2>
172+
result<T2, E> replace_ok(T2&& t) {
173+
if (this->is_err()) {
174+
return result<T2, E>(Err<E>(std::get<Err<E>>(std::move(this->val))));
175+
} else {
176+
return result<T2, E>(Ok<T2>(std::move(t)));
177+
}
178+
}
179+
};
180+
181+
class Utf8Error {};
182+
183+
// Use custom std::span on C++17, otherwise use std::span
184+
#if __cplusplus >= 202002L
185+
186+
template<class T> using span = std::span<T>;
187+
188+
#else // __cplusplus < 202002L
189+
190+
// C++-17-compatible std::span
191+
template<class T>
192+
class span {
193+
194+
public:
195+
constexpr span(T* data, size_t size)
196+
: data_(data), size_(size) {}
197+
template<size_t N>
198+
constexpr span(std::array<typename std::remove_const<T>::type, N>& arr)
199+
: data_(const_cast<T*>(arr.data())), size_(N) {}
200+
constexpr T* data() const noexcept {
201+
return this->data_;
202+
}
203+
constexpr size_t size() const noexcept {
204+
return this->size_;
205+
}
206+
private:
207+
T* data_;
208+
size_t size_;
209+
};
210+
211+
#endif // __cplusplus >= 202002L
212+
213+
} // namespace diplomat
214+
215+
#endif

temporal_capi/bindings/cpp/temporal_rs/.gitkeep

Whitespace-only changes.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#ifndef temporal_rs_AnyCalendarKind_D_HPP
2+
#define temporal_rs_AnyCalendarKind_D_HPP
3+
4+
#include <stdio.h>
5+
#include <stdint.h>
6+
#include <stddef.h>
7+
#include <stdbool.h>
8+
#include <memory>
9+
#include <optional>
10+
#include "../diplomat_runtime.hpp"
11+
12+
namespace temporal_rs {
13+
class AnyCalendarKind;
14+
}
15+
16+
17+
namespace temporal_rs {
18+
namespace capi {
19+
enum AnyCalendarKind {
20+
AnyCalendarKind_Buddhist = 0,
21+
AnyCalendarKind_Chinese = 1,
22+
AnyCalendarKind_Coptic = 2,
23+
AnyCalendarKind_Dangi = 3,
24+
AnyCalendarKind_Ethiopian = 4,
25+
AnyCalendarKind_EthiopianAmeteAlem = 5,
26+
AnyCalendarKind_Gregorian = 6,
27+
AnyCalendarKind_Hebrew = 7,
28+
AnyCalendarKind_Indian = 8,
29+
AnyCalendarKind_IslamicCivil = 9,
30+
AnyCalendarKind_IslamicObservational = 10,
31+
AnyCalendarKind_IslamicTabular = 11,
32+
AnyCalendarKind_IslamicUmmAlQura = 12,
33+
AnyCalendarKind_Iso = 13,
34+
AnyCalendarKind_Japanese = 14,
35+
AnyCalendarKind_JapaneseExtended = 15,
36+
AnyCalendarKind_Persian = 16,
37+
AnyCalendarKind_Roc = 17,
38+
};
39+
40+
typedef struct AnyCalendarKind_option {union { AnyCalendarKind ok; }; bool is_ok; } AnyCalendarKind_option;
41+
} // namespace capi
42+
} // namespace
43+
44+
namespace temporal_rs {
45+
class AnyCalendarKind {
46+
public:
47+
enum Value {
48+
Buddhist = 0,
49+
Chinese = 1,
50+
Coptic = 2,
51+
Dangi = 3,
52+
Ethiopian = 4,
53+
EthiopianAmeteAlem = 5,
54+
Gregorian = 6,
55+
Hebrew = 7,
56+
Indian = 8,
57+
IslamicCivil = 9,
58+
IslamicObservational = 10,
59+
IslamicTabular = 11,
60+
IslamicUmmAlQura = 12,
61+
Iso = 13,
62+
Japanese = 14,
63+
JapaneseExtended = 15,
64+
Persian = 16,
65+
Roc = 17,
66+
};
67+
68+
AnyCalendarKind() = default;
69+
// Implicit conversions between enum and ::Value
70+
constexpr AnyCalendarKind(Value v) : value(v) {}
71+
constexpr operator Value() const { return value; }
72+
// Prevent usage as boolean value
73+
explicit operator bool() const = delete;
74+
75+
inline static std::optional<temporal_rs::AnyCalendarKind> get_for_bcp47_string(std::string_view s);
76+
77+
inline temporal_rs::capi::AnyCalendarKind AsFFI() const;
78+
inline static temporal_rs::AnyCalendarKind FromFFI(temporal_rs::capi::AnyCalendarKind c_enum);
79+
private:
80+
Value value;
81+
};
82+
83+
} // namespace
84+
#endif // temporal_rs_AnyCalendarKind_D_HPP
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef temporal_rs_AnyCalendarKind_HPP
2+
#define temporal_rs_AnyCalendarKind_HPP
3+
4+
#include "AnyCalendarKind.d.hpp"
5+
6+
#include <stdio.h>
7+
#include <stdint.h>
8+
#include <stddef.h>
9+
#include <stdbool.h>
10+
#include <memory>
11+
#include <optional>
12+
#include "../diplomat_runtime.hpp"
13+
14+
15+
namespace temporal_rs {
16+
namespace capi {
17+
extern "C" {
18+
19+
typedef struct temporal_rs_AnyCalendarKind_get_for_bcp47_string_result {union {temporal_rs::capi::AnyCalendarKind ok; }; bool is_ok;} temporal_rs_AnyCalendarKind_get_for_bcp47_string_result;
20+
temporal_rs_AnyCalendarKind_get_for_bcp47_string_result temporal_rs_AnyCalendarKind_get_for_bcp47_string(diplomat::capi::DiplomatStringView s);
21+
22+
23+
} // extern "C"
24+
} // namespace capi
25+
} // namespace
26+
27+
inline temporal_rs::capi::AnyCalendarKind temporal_rs::AnyCalendarKind::AsFFI() const {
28+
return static_cast<temporal_rs::capi::AnyCalendarKind>(value);
29+
}
30+
31+
inline temporal_rs::AnyCalendarKind temporal_rs::AnyCalendarKind::FromFFI(temporal_rs::capi::AnyCalendarKind c_enum) {
32+
switch (c_enum) {
33+
case temporal_rs::capi::AnyCalendarKind_Buddhist:
34+
case temporal_rs::capi::AnyCalendarKind_Chinese:
35+
case temporal_rs::capi::AnyCalendarKind_Coptic:
36+
case temporal_rs::capi::AnyCalendarKind_Dangi:
37+
case temporal_rs::capi::AnyCalendarKind_Ethiopian:
38+
case temporal_rs::capi::AnyCalendarKind_EthiopianAmeteAlem:
39+
case temporal_rs::capi::AnyCalendarKind_Gregorian:
40+
case temporal_rs::capi::AnyCalendarKind_Hebrew:
41+
case temporal_rs::capi::AnyCalendarKind_Indian:
42+
case temporal_rs::capi::AnyCalendarKind_IslamicCivil:
43+
case temporal_rs::capi::AnyCalendarKind_IslamicObservational:
44+
case temporal_rs::capi::AnyCalendarKind_IslamicTabular:
45+
case temporal_rs::capi::AnyCalendarKind_IslamicUmmAlQura:
46+
case temporal_rs::capi::AnyCalendarKind_Iso:
47+
case temporal_rs::capi::AnyCalendarKind_Japanese:
48+
case temporal_rs::capi::AnyCalendarKind_JapaneseExtended:
49+
case temporal_rs::capi::AnyCalendarKind_Persian:
50+
case temporal_rs::capi::AnyCalendarKind_Roc:
51+
return static_cast<temporal_rs::AnyCalendarKind::Value>(c_enum);
52+
default:
53+
abort();
54+
}
55+
}
56+
57+
inline std::optional<temporal_rs::AnyCalendarKind> temporal_rs::AnyCalendarKind::get_for_bcp47_string(std::string_view s) {
58+
auto result = temporal_rs::capi::temporal_rs_AnyCalendarKind_get_for_bcp47_string({s.data(), s.size()});
59+
return result.is_ok ? std::optional<temporal_rs::AnyCalendarKind>(temporal_rs::AnyCalendarKind::FromFFI(result.ok)) : std::nullopt;
60+
}
61+
#endif // temporal_rs_AnyCalendarKind_HPP

0 commit comments

Comments
 (0)