1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 21:41:48 +02:00

More Rust FFI adventures

We can now convert Rust Errors to C++ exceptions. At the Rust->C++ FFI
boundary, Result<T, Error> will cause Error to be converted to and
thrown as a C++ exception.
This commit is contained in:
Eelco Dolstra 2019-09-11 01:15:20 +02:00
parent 8110b4ebb2
commit f738cd4d97
6 changed files with 133 additions and 18 deletions

View file

@ -27,7 +27,7 @@ void builtinUnpackChannel(const BasicDerivation & drv)
decompressor->finish();
});
unpack_tarfile(*source, out);
unpack_tarfile(*source, out).use()->unwrap();
auto entries = readDirectory(out);
if (entries.size() != 1)

12
src/libstore/rust.cc Normal file
View file

@ -0,0 +1,12 @@
#include "logging.hh"
#include "rust.hh"
namespace nix {
extern "C" std::exception_ptr * make_error(rust::StringSlice s)
{
// FIXME: leak
return new std::exception_ptr(std::make_exception_ptr(Error(std::string(s.ptr, s.size))));
}
}

View file

@ -4,7 +4,8 @@ namespace rust {
// Depending on the internal representation of Rust slices is slightly
// evil...
template<typename T> struct Slice
template<typename T>
struct Slice
{
T * ptr;
size_t size;
@ -37,8 +38,64 @@ struct Source
}
};
/* 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);
}
};
}
extern "C" {
void unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
rust::CBox2<rust::Result<std::tuple<>>> unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
}