"newtype" BuildableReq

This makes for better types errors and allows us to give it methods.
This commit is contained in:
John Ericson 2021-04-05 09:24:42 -04:00
parent 4fe41c6db3
commit 9dfb97c987
8 changed files with 24 additions and 16 deletions

View file

@ -73,7 +73,7 @@ DerivationGoal::DerivationGoal(const StorePath & drvPath,
state = &DerivationGoal::getDerivation; state = &DerivationGoal::getDerivation;
name = fmt( name = fmt(
"building of '%s' from .drv file", "building of '%s' from .drv file",
to_string(worker.store, BuildableReqFromDrv { drvPath, wantedOutputs })); BuildableReqFromDrv { drvPath, wantedOutputs }.to_string(worker.store));
trace("created"); trace("created");
mcExpectedBuilds = std::make_unique<MaintainCount<uint64_t>>(worker.expectedBuilds); mcExpectedBuilds = std::make_unique<MaintainCount<uint64_t>>(worker.expectedBuilds);
@ -94,7 +94,7 @@ DerivationGoal::DerivationGoal(const StorePath & drvPath, const BasicDerivation
state = &DerivationGoal::haveDerivation; state = &DerivationGoal::haveDerivation;
name = fmt( name = fmt(
"building of '%s' from in-memory derivation", "building of '%s' from in-memory derivation",
to_string(worker.store, BuildableReqFromDrv { drvPath, drv.outputNames() })); BuildableReqFromDrv { drvPath, drv.outputNames() }.to_string(worker.store));
trace("created"); trace("created");
mcExpectedBuilds = std::make_unique<MaintainCount<uint64_t>>(worker.expectedBuilds); mcExpectedBuilds = std::make_unique<MaintainCount<uint64_t>>(worker.expectedBuilds);

View file

@ -19,7 +19,7 @@ void Store::buildPaths(const std::vector<BuildableReq> & reqs, BuildMode buildMo
[&](BuildableOpaque bo) { [&](BuildableOpaque bo) {
goals.insert(worker.makePathSubstitutionGoal(bo.path, buildMode == bmRepair ? Repair : NoRepair)); goals.insert(worker.makePathSubstitutionGoal(bo.path, buildMode == bmRepair ? Repair : NoRepair));
}, },
}, br); }, br.raw());
} }
worker.run(goals); worker.run(goals);

View file

@ -1200,7 +1200,7 @@ static StorePath pathPartOfReq(const BuildableReq & req)
[&](BuildableReqFromDrv bfd) { [&](BuildableReqFromDrv bfd) {
return bfd.drvPath; return bfd.drvPath;
}, },
}, req); }, req.raw());
} }
@ -1340,7 +1340,7 @@ struct RestrictedStore : public virtual RestrictedStoreConfig, public virtual Lo
for (auto & req : paths) { for (auto & req : paths) {
if (!goal.isAllowed(req)) if (!goal.isAllowed(req))
throw InvalidPath("cannot build '%s' in recursive Nix because path is unknown", to_string(*next, req)); throw InvalidPath("cannot build '%s' in recursive Nix because path is unknown", req.to_string(*next));
} }
next->buildPaths(paths, buildMode); next->buildPaths(paths, buildMode);

View file

@ -41,11 +41,11 @@ std::string BuildableReqFromDrv::to_string(const Store & store) const {
+ (outputs.empty() ? std::string { "*" } : concatStringsSep(",", outputs)); + (outputs.empty() ? std::string { "*" } : concatStringsSep(",", outputs));
} }
std::string to_string(const Store & store, const BuildableReq & req) std::string BuildableReq::to_string(const Store & store) const
{ {
return std::visit( return std::visit(
[&](const auto & req) { return req.to_string(store); }, [&](const auto & req) { return req.to_string(store); },
req); this->raw());
} }
@ -66,7 +66,7 @@ BuildableReqFromDrv BuildableReqFromDrv::parse(const Store & store, std::string_
return {drvPath, outputs}; return {drvPath, outputs};
} }
BuildableReq parseBuildableReq(const Store & store, std::string_view s) BuildableReq BuildableReq::parse(const Store & store, std::string_view s)
{ {
size_t n = s.find("!"); size_t n = s.find("!");
return n == s.npos return n == s.npos

View file

@ -28,14 +28,22 @@ struct BuildableReqFromDrv {
static BuildableReqFromDrv parse(const Store & store, std::string_view); static BuildableReqFromDrv parse(const Store & store, std::string_view);
}; };
using BuildableReq = std::variant< using _BuildableReqRaw = std::variant<
BuildableOpaque, BuildableOpaque,
BuildableReqFromDrv BuildableReqFromDrv
>; >;
std::string to_string(const Store & store, const BuildableReq &); struct BuildableReq : _BuildableReqRaw {
using Raw = _BuildableReqRaw;
using Raw::Raw;
BuildableReq parseBuildableReq(const Store & store, std::string_view); inline const Raw & raw() const {
return static_cast<const Raw &>(*this);
}
std::string to_string(const Store & store) const;
static BuildableReq parse(const Store & store, std::string_view);
};
struct BuildableFromDrv { struct BuildableFromDrv {
StorePath drvPath; StorePath drvPath;

View file

@ -187,7 +187,7 @@ void Store::queryMissing(const std::vector<BuildableReq> & targets,
{ {
auto state(state_.lock()); auto state(state_.lock());
if (!state->done.insert(to_string(*this, req)).second) return; if (!state->done.insert(req.to_string(*this)).second) return;
} }
std::visit(overloaded { std::visit(overloaded {
@ -250,7 +250,7 @@ void Store::queryMissing(const std::vector<BuildableReq> & targets,
for (auto & ref : info->second.references) for (auto & ref : info->second.references)
pool.enqueue(std::bind(doPath, BuildableOpaque { ref })); pool.enqueue(std::bind(doPath, BuildableOpaque { ref }));
}, },
}, req); }, req.raw());
}; };
for (auto & path : targets) for (auto & path : targets)

View file

@ -41,7 +41,7 @@ std::variant<StorePathWithOutputs, StorePath> StorePathWithOutputs::tryFromBuild
[&](BuildableReqFromDrv bfd) -> std::variant<StorePathWithOutputs, StorePath> { [&](BuildableReqFromDrv bfd) -> std::variant<StorePathWithOutputs, StorePath> {
return StorePathWithOutputs { bfd.drvPath, bfd.outputs }; return StorePathWithOutputs { bfd.drvPath, bfd.outputs };
}, },
}, p); }, p.raw());
} }

View file

@ -55,12 +55,12 @@ void write(const Store & store, Sink & out, const ContentAddress & ca)
BuildableReq read(const Store & store, Source & from, Phantom<BuildableReq> _) BuildableReq read(const Store & store, Source & from, Phantom<BuildableReq> _)
{ {
auto s = readString(from); auto s = readString(from);
return parseBuildableReq(store, s); return BuildableReq::parse(store, s);
} }
void write(const Store & store, Sink & out, const BuildableReq & req) void write(const Store & store, Sink & out, const BuildableReq & req)
{ {
out << to_string(store, req); out << req.to_string(store);
} }