Make the Store API more type-safe

Most functions now take a StorePath argument rather than a Path (which
is just an alias for std::string). The StorePath constructor ensures
that the path is syntactically correct (i.e. it looks like
<store-dir>/<base32-hash>-<name>). Similarly, functions like
buildPaths() now take a StorePathWithOutputs, rather than abusing Path
by adding a '!<outputs>' suffix.

Note that the StorePath type is implemented in Rust. This involves
some hackery to allow Rust values to be used directly in C++, via a
helper type whose destructor calls the Rust type's drop()
function. The main issue is the dynamic nature of C++ move semantics:
after we have moved a Rust value, we should not call the drop function
on the original value. So when we move a value, we set the original
value to bitwise zero, and the destructor only calls drop() if the
value is not bitwise zero. This should be sufficient for most types.

Also lots of minor cleanups to the C++ API to make it more modern
(e.g. using std::optional and std::string_view in some places).
This commit is contained in:
Eelco Dolstra
2019-12-10 22:06:05 +01:00
parent ebd89999c2
commit bbe97dff8b
98 changed files with 2628 additions and 2870 deletions
+8 -7
View File
@@ -2,6 +2,7 @@
#include "args.hh"
#include "common-eval-args.hh"
#include "path.hh"
namespace nix {
@@ -28,8 +29,8 @@ private:
struct Buildable
{
Path drvPath; // may be empty
std::map<std::string, Path> outputs;
std::optional<StorePath> drvPath;
std::map<std::string, StorePath> outputs;
};
typedef std::vector<Buildable> Buildables;
@@ -131,7 +132,7 @@ public:
using StoreCommand::run;
virtual void run(ref<Store> store, Paths storePaths) = 0;
virtual void run(ref<Store> store, std::vector<StorePath> storePaths) = 0;
void run(ref<Store> store) override;
@@ -143,7 +144,7 @@ struct StorePathCommand : public InstallablesCommand
{
using StoreCommand::run;
virtual void run(ref<Store> store, const Path & storePath) = 0;
virtual void run(ref<Store> store, const StorePath & storePath) = 0;
void run(ref<Store> store) override;
};
@@ -174,13 +175,13 @@ std::shared_ptr<Installable> parseInstallable(
Buildables build(ref<Store> store, RealiseMode mode,
std::vector<std::shared_ptr<Installable>> installables);
PathSet toStorePaths(ref<Store> store, RealiseMode mode,
std::set<StorePath> toStorePaths(ref<Store> store, RealiseMode mode,
std::vector<std::shared_ptr<Installable>> installables);
Path toStorePath(ref<Store> store, RealiseMode mode,
StorePath toStorePath(ref<Store> store, RealiseMode mode,
std::shared_ptr<Installable> installable);
PathSet toDerivations(ref<Store> store,
std::set<StorePath> toDerivations(ref<Store> store,
std::vector<std::shared_ptr<Installable>> installables,
bool useDeriver = false);