Skip to content

Check in bindings, add CI for keeping them up to date #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ jobs:
run: cargo build -p temporal_capi
- name: Regen
run: cargo run -p diplomat-gen
- name: Check diff
run: git diff --exit-code
# Todo: eventually we should check in bindings and test them
- name: Makefile tests
run: cd temporal_capi/cpp_tests && make
Expand Down
2 changes: 0 additions & 2 deletions temporal_capi/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# To be checked in later once it stabilizes a bit
bindings/
*.out
215 changes: 215 additions & 0 deletions temporal_capi/bindings/cpp/diplomat_runtime.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#ifndef DIPLOMAT_RUNTIME_CPP_H
#define DIPLOMAT_RUNTIME_CPP_H

#include <optional>
#include <string>
#include <type_traits>
#include <variant>

#if __cplusplus >= 202002L
#include <span>
#else
#include <array>
#endif

namespace diplomat {

namespace capi {
extern "C" {

static_assert(sizeof(char) == sizeof(uint8_t), "your architecture's `char` is not 8 bits");
static_assert(sizeof(char16_t) == sizeof(uint16_t), "your architecture's `char16_t` is not 16 bits");
static_assert(sizeof(char32_t) == sizeof(uint32_t), "your architecture's `char32_t` is not 32 bits");

typedef struct DiplomatWrite {
void* context;
char* buf;
size_t len;
size_t cap;
bool grow_failed;
void (*flush)(struct DiplomatWrite*);
bool (*grow)(struct DiplomatWrite*, size_t);
} DiplomatWrite;

bool diplomat_is_str(const char* buf, size_t len);

#define MAKE_SLICES(name, c_ty) \
typedef struct Diplomat##name##View { \
const c_ty* data; \
size_t len; \
} Diplomat##name##View; \
typedef struct Diplomat##name##ViewMut { \
c_ty* data; \
size_t len; \
} Diplomat##name##ViewMut; \
typedef struct Diplomat##name##Array { \
const c_ty* data; \
size_t len; \
} Diplomat##name##Array;

#define MAKE_SLICES_AND_OPTIONS(name, c_ty) \
MAKE_SLICES(name, c_ty) \
typedef struct Option##name {union { c_ty ok; }; bool is_ok; } Option##name;

MAKE_SLICES_AND_OPTIONS(I8, int8_t)
MAKE_SLICES_AND_OPTIONS(U8, uint8_t)
MAKE_SLICES_AND_OPTIONS(I16, int16_t)
MAKE_SLICES_AND_OPTIONS(U16, uint16_t)
MAKE_SLICES_AND_OPTIONS(I32, int32_t)
MAKE_SLICES_AND_OPTIONS(U32, uint32_t)
MAKE_SLICES_AND_OPTIONS(I64, int64_t)
MAKE_SLICES_AND_OPTIONS(U64, uint64_t)
MAKE_SLICES_AND_OPTIONS(Isize, intptr_t)
MAKE_SLICES_AND_OPTIONS(Usize, size_t)
MAKE_SLICES_AND_OPTIONS(F32, float)
MAKE_SLICES_AND_OPTIONS(F64, double)
MAKE_SLICES_AND_OPTIONS(Bool, bool)
MAKE_SLICES_AND_OPTIONS(Char, char32_t)
MAKE_SLICES(String, char)
MAKE_SLICES(String16, char16_t)
MAKE_SLICES(Strings, DiplomatStringView)
MAKE_SLICES(Strings16, DiplomatString16View)

} // extern "C"
} // namespace capi

extern "C" inline void _flush(capi::DiplomatWrite* w) {
std::string* string = reinterpret_cast<std::string*>(w->context);
string->resize(w->len);
};

extern "C" inline bool _grow(capi::DiplomatWrite* w, uintptr_t requested) {
std::string* string = reinterpret_cast<std::string*>(w->context);
string->resize(requested);
w->cap = string->length();
w->buf = &(*string)[0];
return true;
};

inline capi::DiplomatWrite WriteFromString(std::string& string) {
capi::DiplomatWrite w;
w.context = &string;
w.buf = &string[0];
w.len = string.length();
w.cap = string.length();
// Will never become true, as _grow is infallible.
w.grow_failed = false;
w.flush = _flush;
w.grow = _grow;
return w;
};

template<class T> struct Ok {
T inner;
Ok(T&& i): inner(std::move(i)) {}
// We don't want to expose an lvalue-capable constructor in general
// however there is no problem doing this for trivially copyable types
template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
Ok(T i): inner(i) {}
Ok() = default;
Ok(Ok&&) noexcept = default;
Ok(const Ok &) = default;
Ok& operator=(const Ok&) = default;
Ok& operator=(Ok&&) noexcept = default;
};

template<class T> struct Err {
T inner;
Err(T&& i): inner(std::move(i)) {}
// We don't want to expose an lvalue-capable constructor in general
// however there is no problem doing this for trivially copyable types
template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
Err(T i): inner(i) {}
Err() = default;
Err(Err&&) noexcept = default;
Err(const Err &) = default;
Err& operator=(const Err&) = default;
Err& operator=(Err&&) noexcept = default;
};

template<class T, class E>
class result {
private:
std::variant<Ok<T>, Err<E>> val;
public:
result(Ok<T>&& v): val(std::move(v)) {}
result(Err<E>&& v): val(std::move(v)) {}
result() = default;
result(const result &) = default;
result& operator=(const result&) = default;
result& operator=(result&&) noexcept = default;
result(result &&) noexcept = default;
~result() = default;
bool is_ok() const {
return std::holds_alternative<Ok<T>>(this->val);
};
bool is_err() const {
return std::holds_alternative<Err<E>>(this->val);
};

std::optional<T> ok() && {
if (!this->is_ok()) {
return std::nullopt;
}
return std::make_optional(std::move(std::get<Ok<T>>(std::move(this->val)).inner));
};
std::optional<E> err() && {
if (!this->is_err()) {
return std::nullopt;
}
return std::make_optional(std::move(std::get<Err<E>>(std::move(this->val)).inner));
}

void set_ok(T&& t) {
this->val = Ok<T>(std::move(t));
}

void set_err(E&& e) {
this->val = Err<E>(std::move(e));
}

template<typename T2>
result<T2, E> replace_ok(T2&& t) {
if (this->is_err()) {
return result<T2, E>(Err<E>(std::get<Err<E>>(std::move(this->val))));
} else {
return result<T2, E>(Ok<T2>(std::move(t)));
}
}
};

class Utf8Error {};

// Use custom std::span on C++17, otherwise use std::span
#if __cplusplus >= 202002L

template<class T> using span = std::span<T>;

#else // __cplusplus < 202002L

// C++-17-compatible std::span
template<class T>
class span {

public:
constexpr span(T* data, size_t size)
: data_(data), size_(size) {}
template<size_t N>
constexpr span(std::array<typename std::remove_const<T>::type, N>& arr)
: data_(const_cast<T*>(arr.data())), size_(N) {}
constexpr T* data() const noexcept {
return this->data_;
}
constexpr size_t size() const noexcept {
return this->size_;
}
private:
T* data_;
size_t size_;
};

#endif // __cplusplus >= 202002L

} // namespace diplomat

#endif
Empty file.
84 changes: 84 additions & 0 deletions temporal_capi/bindings/cpp/temporal_rs/AnyCalendarKind.d.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#ifndef temporal_rs_AnyCalendarKind_D_HPP
#define temporal_rs_AnyCalendarKind_D_HPP

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <memory>
#include <optional>
#include "../diplomat_runtime.hpp"

namespace temporal_rs {
class AnyCalendarKind;
}


namespace temporal_rs {
namespace capi {
enum AnyCalendarKind {
AnyCalendarKind_Buddhist = 0,
AnyCalendarKind_Chinese = 1,
AnyCalendarKind_Coptic = 2,
AnyCalendarKind_Dangi = 3,
AnyCalendarKind_Ethiopian = 4,
AnyCalendarKind_EthiopianAmeteAlem = 5,
AnyCalendarKind_Gregorian = 6,
AnyCalendarKind_Hebrew = 7,
AnyCalendarKind_Indian = 8,
AnyCalendarKind_IslamicCivil = 9,
AnyCalendarKind_IslamicObservational = 10,
AnyCalendarKind_IslamicTabular = 11,
AnyCalendarKind_IslamicUmmAlQura = 12,
AnyCalendarKind_Iso = 13,
AnyCalendarKind_Japanese = 14,
AnyCalendarKind_JapaneseExtended = 15,
AnyCalendarKind_Persian = 16,
AnyCalendarKind_Roc = 17,
};

typedef struct AnyCalendarKind_option {union { AnyCalendarKind ok; }; bool is_ok; } AnyCalendarKind_option;
} // namespace capi
} // namespace

namespace temporal_rs {
class AnyCalendarKind {
public:
enum Value {
Buddhist = 0,
Chinese = 1,
Coptic = 2,
Dangi = 3,
Ethiopian = 4,
EthiopianAmeteAlem = 5,
Gregorian = 6,
Hebrew = 7,
Indian = 8,
IslamicCivil = 9,
IslamicObservational = 10,
IslamicTabular = 11,
IslamicUmmAlQura = 12,
Iso = 13,
Japanese = 14,
JapaneseExtended = 15,
Persian = 16,
Roc = 17,
};

AnyCalendarKind() = default;
// Implicit conversions between enum and ::Value
constexpr AnyCalendarKind(Value v) : value(v) {}
constexpr operator Value() const { return value; }
// Prevent usage as boolean value
explicit operator bool() const = delete;

inline static std::optional<temporal_rs::AnyCalendarKind> get_for_bcp47_string(std::string_view s);

inline temporal_rs::capi::AnyCalendarKind AsFFI() const;
inline static temporal_rs::AnyCalendarKind FromFFI(temporal_rs::capi::AnyCalendarKind c_enum);
private:
Value value;
};

} // namespace
#endif // temporal_rs_AnyCalendarKind_D_HPP
61 changes: 61 additions & 0 deletions temporal_capi/bindings/cpp/temporal_rs/AnyCalendarKind.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef temporal_rs_AnyCalendarKind_HPP
#define temporal_rs_AnyCalendarKind_HPP

#include "AnyCalendarKind.d.hpp"

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <memory>
#include <optional>
#include "../diplomat_runtime.hpp"


namespace temporal_rs {
namespace capi {
extern "C" {

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;
temporal_rs_AnyCalendarKind_get_for_bcp47_string_result temporal_rs_AnyCalendarKind_get_for_bcp47_string(diplomat::capi::DiplomatStringView s);


} // extern "C"
} // namespace capi
} // namespace

inline temporal_rs::capi::AnyCalendarKind temporal_rs::AnyCalendarKind::AsFFI() const {
return static_cast<temporal_rs::capi::AnyCalendarKind>(value);
}

inline temporal_rs::AnyCalendarKind temporal_rs::AnyCalendarKind::FromFFI(temporal_rs::capi::AnyCalendarKind c_enum) {
switch (c_enum) {
case temporal_rs::capi::AnyCalendarKind_Buddhist:
case temporal_rs::capi::AnyCalendarKind_Chinese:
case temporal_rs::capi::AnyCalendarKind_Coptic:
case temporal_rs::capi::AnyCalendarKind_Dangi:
case temporal_rs::capi::AnyCalendarKind_Ethiopian:
case temporal_rs::capi::AnyCalendarKind_EthiopianAmeteAlem:
case temporal_rs::capi::AnyCalendarKind_Gregorian:
case temporal_rs::capi::AnyCalendarKind_Hebrew:
case temporal_rs::capi::AnyCalendarKind_Indian:
case temporal_rs::capi::AnyCalendarKind_IslamicCivil:
case temporal_rs::capi::AnyCalendarKind_IslamicObservational:
case temporal_rs::capi::AnyCalendarKind_IslamicTabular:
case temporal_rs::capi::AnyCalendarKind_IslamicUmmAlQura:
case temporal_rs::capi::AnyCalendarKind_Iso:
case temporal_rs::capi::AnyCalendarKind_Japanese:
case temporal_rs::capi::AnyCalendarKind_JapaneseExtended:
case temporal_rs::capi::AnyCalendarKind_Persian:
case temporal_rs::capi::AnyCalendarKind_Roc:
return static_cast<temporal_rs::AnyCalendarKind::Value>(c_enum);
default:
abort();
}
}

inline std::optional<temporal_rs::AnyCalendarKind> temporal_rs::AnyCalendarKind::get_for_bcp47_string(std::string_view s) {
auto result = temporal_rs::capi::temporal_rs_AnyCalendarKind_get_for_bcp47_string({s.data(), s.size()});
return result.is_ok ? std::optional<temporal_rs::AnyCalendarKind>(temporal_rs::AnyCalendarKind::FromFFI(result.ok)) : std::nullopt;
}
#endif // temporal_rs_AnyCalendarKind_HPP
Loading