aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libstore/rust.hh97
-rw-r--r--src/libutil/rust-ffi.cc (renamed from src/libstore/rust.cc)0
-rw-r--r--src/libutil/rust-ffi.hh97
3 files changed, 98 insertions, 96 deletions
diff --git a/src/libstore/rust.hh b/src/libstore/rust.hh
index 4c7720a44..dccc98b4b 100644
--- a/src/libstore/rust.hh
+++ b/src/libstore/rust.hh
@@ -1,100 +1,5 @@
#include "serialise.hh"
-
-namespace rust {
-
-// Depending on the internal representation of Rust slices is slightly
-// evil...
-template<typename T>
-struct Slice
-{
- T * ptr;
- size_t size;
-
- Slice(T * ptr, size_t size) : ptr(ptr), size(size)
- {
- assert(ptr);
- }
-};
-
-struct StringSlice : Slice<char>
-{
- StringSlice(const std::string & s): Slice((char *) s.data(), s.size()) {}
-};
-
-struct Source
-{
- size_t (*fun)(void * source_this, rust::Slice<uint8_t> data);
- nix::Source * _this;
-
- Source(nix::Source & _this)
- : fun(sourceWrapper), _this(&_this)
- {}
-
- // FIXME: how to propagate exceptions?
- static size_t sourceWrapper(void * _this, rust::Slice<uint8_t> data)
- {
- auto n = ((nix::Source *) _this)->read(data.ptr, data.size);
- return n;
- }
-};
-
-/* C++ representation of Rust's Result<T, CppException>. */
-template<typename T>
-struct Result
-{
- unsigned int tag;
-
- union {
- T data;
- std::exception_ptr * exc;
- };
-
- /* Rethrow the wrapped exception or return the wrapped value. */
- T unwrap()
- {
- if (tag == 0)
- return data;
- else if (tag == 1)
- std::rethrow_exception(*exc);
- else
- abort();
- }
-};
-
-template<typename T>
-struct CBox
-{
- T * ptr;
-
- T * operator ->()
- {
- return ptr;
- }
-
- CBox(T * ptr) : ptr(ptr) { }
- CBox(const CBox &) = delete;
- CBox(CBox &&) = delete;
-
- ~CBox()
- {
- free(ptr);
- }
-};
-
-// Grrr, this is only needed because 'extern "C"' functions don't
-// support non-POD return types (and CBox has a destructor so it's not
-// POD).
-template<typename T>
-struct CBox2
-{
- T * ptr;
- CBox<T> use()
- {
- return CBox(ptr);
- }
-};
-
-}
+#include "rust-ffi.hh"
extern "C" {
rust::CBox2<rust::Result<std::tuple<>>> unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
diff --git a/src/libstore/rust.cc b/src/libutil/rust-ffi.cc
index a616d83a6..a616d83a6 100644
--- a/src/libstore/rust.cc
+++ b/src/libutil/rust-ffi.cc
diff --git a/src/libutil/rust-ffi.hh b/src/libutil/rust-ffi.hh
new file mode 100644
index 000000000..a488b96d6
--- /dev/null
+++ b/src/libutil/rust-ffi.hh
@@ -0,0 +1,97 @@
+#include "serialise.hh"
+
+namespace rust {
+
+// Depending on the internal representation of Rust slices is slightly
+// evil...
+template<typename T>
+struct Slice
+{
+ T * ptr;
+ size_t size;
+
+ Slice(T * ptr, size_t size) : ptr(ptr), size(size)
+ {
+ assert(ptr);
+ }
+};
+
+struct StringSlice : Slice<char>
+{
+ StringSlice(const std::string & s): Slice((char *) s.data(), s.size()) {}
+};
+
+struct Source
+{
+ size_t (*fun)(void * source_this, rust::Slice<uint8_t> data);
+ nix::Source * _this;
+
+ Source(nix::Source & _this)
+ : fun(sourceWrapper), _this(&_this)
+ {}
+
+ // FIXME: how to propagate exceptions?
+ static size_t sourceWrapper(void * _this, rust::Slice<uint8_t> data)
+ {
+ auto n = ((nix::Source *) _this)->read(data.ptr, data.size);
+ return n;
+ }
+};
+
+/* C++ representation of Rust's Result<T, CppException>. */
+template<typename T>
+struct Result
+{
+ unsigned int tag;
+
+ union {
+ T data;
+ std::exception_ptr * exc;
+ };
+
+ /* Rethrow the wrapped exception or return the wrapped value. */
+ T unwrap()
+ {
+ if (tag == 0)
+ return data;
+ else if (tag == 1)
+ std::rethrow_exception(*exc);
+ else
+ abort();
+ }
+};
+
+template<typename T>
+struct CBox
+{
+ T * ptr;
+
+ T * operator ->()
+ {
+ return ptr;
+ }
+
+ CBox(T * ptr) : ptr(ptr) { }
+ CBox(const CBox &) = delete;
+ CBox(CBox &&) = delete;
+
+ ~CBox()
+ {
+ free(ptr);
+ }
+};
+
+// Grrr, this is only needed because 'extern "C"' functions don't
+// support non-POD return types (and CBox has a destructor so it's not
+// POD).
+template<typename T>
+struct CBox2
+{
+ T * ptr;
+ CBox<T> use()
+ {
+ return CBox(ptr);
+ }
+};
+
+}