aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/rust-ffi.hh
blob: 469a5fba321c19f5402b4a79b8378614a0fb1d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once

#include "serialise.hh"

#include <string_view>
#include <cstring>
#include <array>

namespace rust {

typedef void (*DropFun)(void *);

/* A Rust value of N bytes. It can be moved but not copied. When it
   goes out of scope, the C++ destructor will run the drop
   function. */
template<std::size_t N, DropFun drop>
struct Value
{
protected:

    std::array<char, N> raw;

    ~Value()
    {
        if (!isEvacuated()) {
            drop(this);
            evacuate();
        }
    }

    // Must not be called directly.
    Value()
    { }

    Value(Value && other)
        : raw(other.raw)
    {
        other.evacuate();
    }

    void operator =(Value && other)
    {
        if (!isEvacuated())
            drop(this);
        raw = other.raw;
        other.evacuate();
    }

private:

    /* FIXME: optimize these (ideally in such a way that the compiler
       can elide most calls to evacuate() / isEvacuated(). */
    inline void evacuate()
    {
        for (auto & i : raw) i = 0;
    }

    inline bool isEvacuated()
    {
        for (auto & i : raw)
            if (i != 0) return false;
        return true;
    }
};

/* A Rust vector. */
template<typename T, DropFun drop>
struct Vec : Value<3 * sizeof(void *), drop>
{
    inline size_t size() const
    {
        return ((const size_t *) &this->raw)[2];
    }

    const T * data() const
    {
        return ((const T * *) &this->raw)[0];
    }
};

/* A Rust slice. */
template<typename T>
struct Slice
{
    const T * ptr;
    size_t size;

    Slice(const T * ptr, size_t size) : ptr(ptr), size(size)
    {
        assert(ptr);
    }
};

struct StringSlice : Slice<char>
{
    StringSlice(const std::string & s): Slice(s.data(), s.size()) {}
    explicit StringSlice(std::string_view s): Slice(s.data(), s.size()) {}
    StringSlice(const char * s): Slice(s, strlen(s)) {}

    operator std::string_view() const
    {
        return std::string_view(ptr, size);
    }
};

/* A Rust string. */
struct String;

extern "C" {
    void ffi_String_new(StringSlice s, String * out);
    void ffi_String_drop(void * s);
}

struct String : Vec<char, ffi_String_drop>
{
    String(std::string_view s)
    {
        ffi_String_new(StringSlice(s), this);
    }

    String(const char * s)
        : String({s, std::strlen(s)})
    {
    }

    operator std::string_view() const
    {
        return std::string_view(data(), size());
    }
};

std::ostream & operator << (std::ostream & str, const String & s);

/* C++ representation of Rust's Result<T, CppException>. */
template<typename T>
struct Result
{
    enum { Ok = 0, Err = 1, Uninit = 2 } tag;

    union {
        T data;
        std::exception_ptr * exc;
    };

    Result() : tag(Uninit) { }; // FIXME: remove

    Result(const Result &) = delete;

    Result(Result && other)
        : tag(other.tag)
    {
        other.tag = Uninit;
        if (tag == Ok)
            data = std::move(other.data);
        else if (tag == Err)
            exc = other.exc;
    }

    ~Result()
    {
        if (tag == Ok)
            data.~T();
        else if (tag == Err)
            free(exc);
        else if (tag == Uninit)
            ;
        else
            abort();
    }

    /* Rethrow the wrapped exception or return the wrapped value. */
    T unwrap()
    {
        if (tag == Ok) {
            tag = Uninit;
            return std::move(data);
        }
        else if (tag == Err)
            std::rethrow_exception(*exc);
        else
            abort();
    }
};

}