1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-30 03:23:16 +02:00

Extend Rust FFI

Do idiomatic C++ copy and move constructors for a few things, so
wrapping structs' defaults can work.
This commit is contained in:
John Ericson 2020-03-22 23:43:07 -04:00 committed by John Ericson
parent eb1911e277
commit e433d4af4c
8 changed files with 63 additions and 21 deletions

View file

@ -44,6 +44,12 @@ struct Hash
string. */
Hash(const std::string & s, HashType type = htUnknown);
Hash(const Hash &) = default;
Hash(Hash &&) = default;
Hash & operator = (const Hash &) = default;
void init();
/* Check whether a hash is set. */

View file

@ -30,7 +30,10 @@ protected:
// Must not be called directly.
Value()
{ }
{
// Precaution, in case this is used improperly
evacuate();
}
Value(Value && other)
: raw(other.raw)
@ -38,6 +41,19 @@ protected:
other.evacuate();
}
// Not all Rust types are Clone / Copy, but our base Value class needs to
// have a copy constructor so that ones 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())
@ -76,6 +92,16 @@ struct Vec : Value<3 * sizeof(void *), drop>
{
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. */
@ -144,7 +170,7 @@ struct Result
std::exception_ptr * exc;
};
Result() : tag(Uninit) { }; // FIXME: remove
Result() = delete;
Result(const Result &) = delete;
@ -171,7 +197,7 @@ struct Result
}
/* Rethrow the wrapped exception or return the wrapped value. */
T unwrap()
T unwrap() &&
{
if (tag == Ok) {
tag = Uninit;