aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/rust-ffi.hh
blob: 8b61b6d005f239a03e4b3d6ecd6882ba5d1428cc (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#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()
    {
        // Precaution, in case this is used improperly
        evacuate();
    }

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

    // Not all Rust types are Clone / Copy, but our base Value class needs to
    // have a copy constructor so that types which do implement Copy/Clone
    // can be copied/cloned.
    Value(const Value & other)
        : raw(other.raw)
    {
    }
    void operator =(const Value & other)
    {
        if (!isEvacuated())
            drop(this);
    }

    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];
    }

protected:

    // Must not be called directly.
    Vec<T, drop>();

    Vec<T, drop>(Vec<T, drop> && other) = default;

    // Delete until we know how to do this properly.
    Vec<T, drop>(const Vec<T, drop> & other) = delete;
};

/* 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() = delete;

    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() = delete;

    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();
    }
};

}