Skip to content

Commit

Permalink
Move RustStream implementation to a separate template file
Browse files Browse the repository at this point in the history
Signed-off-by: Martynas Gurskas <[email protected]>
  • Loading branch information
Lipt0nas committed Mar 1, 2024
1 parent 078b323 commit 7709f4e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 97 deletions.
50 changes: 1 addition & 49 deletions bindgen/src/bindings/cpp/templates/cpp_scaffolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,55 +48,7 @@ struct RustCallStatus {

typedef int ForeignCallback(uint64_t handle, uint32_t method, uint8_t *args_data, int32_t args_len, RustBuffer *buf_ptr);

struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
RustStreamBuffer(RustBuffer *buf) {
this->setg(buf->data, buf->data, buf->data + buf->len);
this->setp(buf->data, buf->data + buf->capacity);
}
~RustStreamBuffer() = default;

private:
RustStreamBuffer() = delete;
RustStreamBuffer(const RustStreamBuffer &) = delete;
RustStreamBuffer(RustStreamBuffer &&) = delete;

RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
};

struct RustStream: std::basic_iostream<uint8_t> {
RustStream(RustBuffer *buf):
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator>>(T &val) {
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));

if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

return *this;
}

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator<<(T val) {
if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

write(reinterpret_cast<uint8_t *>(&val), sizeof(T));

return *this;
}
private:
RustStreamBuffer streambuf;
};

{%- include "rust_buf_stream.cpp" %}
{%- include "scaffolding/object_map.cpp" %}

{%- for typ in ci.iter_types() %}
Expand Down
49 changes: 49 additions & 0 deletions bindgen/src/bindings/cpp/templates/rust_buf_stream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
RustStreamBuffer(RustBuffer *buf) {
this->setg(buf->data, buf->data, buf->data + buf->len);
this->setp(buf->data, buf->data + buf->capacity);
}
~RustStreamBuffer() = default;

private:
RustStreamBuffer() = delete;
RustStreamBuffer(const RustStreamBuffer &) = delete;
RustStreamBuffer(RustStreamBuffer &&) = delete;

RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
};

struct RustStream: std::basic_iostream<uint8_t> {
RustStream(RustBuffer *buf):
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator>>(T &val) {
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));

if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

return *this;
}

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator<<(T val) {
if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

write(reinterpret_cast<uint8_t *>(&val), sizeof(T));

return *this;
}
private:
RustStreamBuffer streambuf;
};

49 changes: 1 addition & 48 deletions bindgen/src/bindings/cpp/templates/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,54 +79,7 @@ typedef {{ type_name }} {{ name }};
{%- endfor %}

namespace uniffi {
struct RustStreamBuffer: std::basic_streambuf<uint8_t> {
RustStreamBuffer(RustBuffer *buf) {
this->setg(buf->data, buf->data, buf->data + buf->len);
this->setp(buf->data, buf->data + buf->capacity);
}
~RustStreamBuffer() = default;

private:
RustStreamBuffer() = delete;
RustStreamBuffer(const RustStreamBuffer &) = delete;
RustStreamBuffer(RustStreamBuffer &&) = delete;

RustStreamBuffer &operator=(const RustStreamBuffer &) = delete;
RustStreamBuffer &operator=(RustStreamBuffer &&) = delete;
};

struct RustStream: std::basic_iostream<uint8_t> {
RustStream(RustBuffer *buf):
streambuf(RustStreamBuffer(buf)), std::basic_iostream<uint8_t>(&streambuf) { }

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator>>(T &val) {
read(reinterpret_cast<uint8_t *>(&val), sizeof(T));

if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

return *this;
}

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
RustStream &operator<<(T val) {
if (std::endian::native != std::endian::big) {
auto bytes = reinterpret_cast<char *>(&val);

std::reverse(bytes, bytes + sizeof(T));
}

write(reinterpret_cast<uint8_t *>(&val), sizeof(T));

return *this;
}
private:
RustStreamBuffer streambuf;
};
{%- include "rust_buf_stream.cpp" %}

RustBuffer rustbuffer_alloc(int32_t);
RustBuffer rustbuffer_from_bytes(const ForeignBytes &);
Expand Down

0 comments on commit 7709f4e

Please sign in to comment.