mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 12:06:01 +01:00
treewide: Replace a lot of std::function with recursive lambdas via 'deducing this'
This is much simpler and has less overhead than a std::function, which does type erasure.
This commit is contained in:
parent
64c55961eb
commit
b9ecd329ae
13 changed files with 50 additions and 81 deletions
|
|
@ -2160,9 +2160,7 @@ void EvalState::forceValueDeep(Value & v)
|
|||
{
|
||||
std::set<const Value *> seen;
|
||||
|
||||
std::function<void(Value & v)> recurse;
|
||||
|
||||
recurse = [&](Value & v) {
|
||||
auto recurse = [&](this auto & recurse, Value & v) {
|
||||
if (!seen.insert(&v).second)
|
||||
return;
|
||||
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
|
|||
|
||||
std::istringstream tomlStream(std::string{toml});
|
||||
|
||||
auto visit = [&](auto & self, Value & v, toml::value t) -> void {
|
||||
auto visit = [&](this auto & visit, Value & v, toml::value t) -> void {
|
||||
switch (t.type()) {
|
||||
case toml::value_t::table: {
|
||||
auto table = toml::get<toml::table>(t);
|
||||
|
|
@ -100,7 +100,7 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
|
|||
|
||||
for (auto & elem : table) {
|
||||
forceNoNullByte(elem.first);
|
||||
self(self, attrs.alloc(elem.first), elem.second);
|
||||
visit(attrs.alloc(elem.first), elem.second);
|
||||
}
|
||||
|
||||
v.mkAttrs(attrs);
|
||||
|
|
@ -110,7 +110,7 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
|
|||
|
||||
auto list = state.buildList(array.size());
|
||||
for (const auto & [n, v] : enumerate(list))
|
||||
self(self, *(v = state.allocValue()), array[n]);
|
||||
visit(*(v = state.allocValue()), array[n]);
|
||||
v.mkList(list);
|
||||
} break;
|
||||
case toml::value_t::boolean:
|
||||
|
|
@ -155,7 +155,6 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
|
|||
|
||||
try {
|
||||
visit(
|
||||
visit,
|
||||
val,
|
||||
toml::parse(
|
||||
tomlStream,
|
||||
|
|
|
|||
|
|
@ -469,41 +469,31 @@ lockFlake(const Settings & settings, EvalState & state, const FlakeRef & topRef,
|
|||
|
||||
std::vector<FlakeRef> parents;
|
||||
|
||||
std::function<void(
|
||||
const FlakeInputs & flakeInputs,
|
||||
ref<Node> node,
|
||||
const InputAttrPath & inputAttrPathPrefix,
|
||||
std::shared_ptr<const Node> oldNode,
|
||||
const InputAttrPath & followsPrefix,
|
||||
const SourcePath & sourcePath,
|
||||
bool trustLock)>
|
||||
computeLocks;
|
||||
|
||||
computeLocks = [&](
|
||||
/* The inputs of this node, either from flake.nix or
|
||||
flake.lock. */
|
||||
const FlakeInputs & flakeInputs,
|
||||
/* The node whose locks are to be updated.*/
|
||||
ref<Node> node,
|
||||
/* The path to this node in the lock file graph. */
|
||||
const InputAttrPath & inputAttrPathPrefix,
|
||||
/* The old node, if any, from which locks can be
|
||||
copied. */
|
||||
std::shared_ptr<const Node> oldNode,
|
||||
/* The prefix relative to which 'follows' should be
|
||||
interpreted. When a node is initially locked, it's
|
||||
relative to the node's flake; when it's already locked,
|
||||
it's relative to the root of the lock file. */
|
||||
const InputAttrPath & followsPrefix,
|
||||
/* The source path of this node's flake. */
|
||||
const SourcePath & sourcePath,
|
||||
bool trustLock) {
|
||||
auto computeLocks = [&](this auto & computeLocks,
|
||||
/* The inputs of this node, either from flake.nix or
|
||||
flake.lock. */
|
||||
const FlakeInputs & flakeInputs,
|
||||
/* The node whose locks are to be updated.*/
|
||||
ref<Node> node,
|
||||
/* The path to this node in the lock file graph. */
|
||||
const InputAttrPath & inputAttrPathPrefix,
|
||||
/* The old node, if any, from which locks can be
|
||||
copied. */
|
||||
std::shared_ptr<const Node> oldNode,
|
||||
/* The prefix relative to which 'follows' should be
|
||||
interpreted. When a node is initially locked, it's
|
||||
relative to the node's flake; when it's already locked,
|
||||
it's relative to the root of the lock file. */
|
||||
const InputAttrPath & followsPrefix,
|
||||
/* The source path of this node's flake. */
|
||||
const SourcePath & sourcePath,
|
||||
bool trustLock) -> void {
|
||||
debug("computing lock file node '%s'", printInputAttrPath(inputAttrPathPrefix));
|
||||
|
||||
/* Get the overrides (i.e. attributes of the form
|
||||
'inputs.nixops.inputs.nixpkgs.url = ...'). */
|
||||
std::function<void(const FlakeInput & input, const InputAttrPath & prefix)> addOverrides;
|
||||
addOverrides = [&](const FlakeInput & input, const InputAttrPath & prefix) {
|
||||
auto addOverrides =
|
||||
[&](this auto & addOverrides, const FlakeInput & input, const InputAttrPath & prefix) -> void {
|
||||
for (auto & [idOverride, inputOverride] : input.overrides) {
|
||||
auto inputAttrPath(prefix);
|
||||
inputAttrPath.push_back(idOverride);
|
||||
|
|
|
|||
|
|
@ -149,9 +149,7 @@ LockFile::LockFile(const fetchers::Settings & fetchSettings, std::string_view co
|
|||
|
||||
std::map<std::string, ref<Node>> nodeMap;
|
||||
|
||||
std::function<void(Node & node, const nlohmann::json & jsonNode)> getInputs;
|
||||
|
||||
getInputs = [&](Node & node, const nlohmann::json & jsonNode) {
|
||||
auto getInputs = [&](this auto & getInputs, Node & node, const nlohmann::json & jsonNode) -> void {
|
||||
if (jsonNode.find("inputs") == jsonNode.end())
|
||||
return;
|
||||
for (auto & i : jsonNode["inputs"].items()) {
|
||||
|
|
@ -276,9 +274,7 @@ std::optional<FlakeRef> LockFile::isUnlocked(const fetchers::Settings & fetchSet
|
|||
{
|
||||
std::set<ref<const Node>> nodes;
|
||||
|
||||
std::function<void(ref<const Node> node)> visit;
|
||||
|
||||
visit = [&](ref<const Node> node) {
|
||||
auto visit = [&](this auto & visit, ref<const Node> node) -> void {
|
||||
if (!nodes.insert(node).second)
|
||||
return;
|
||||
for (auto & i : node->inputs)
|
||||
|
|
@ -332,9 +328,7 @@ std::map<InputAttrPath, Node::Edge> LockFile::getAllInputs() const
|
|||
std::set<ref<Node>> done;
|
||||
std::map<InputAttrPath, Node::Edge> res;
|
||||
|
||||
std::function<void(const InputAttrPath & prefix, ref<Node> node)> recurse;
|
||||
|
||||
recurse = [&](const InputAttrPath & prefix, ref<Node> node) {
|
||||
auto recurse = [&](this auto & recurse, const InputAttrPath & prefix, ref<Node> node) -> void {
|
||||
if (!done.insert(node).second)
|
||||
return;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,15 +6,14 @@ namespace nix {
|
|||
template<typename V>
|
||||
typename DerivedPathMap<V>::ChildNode & DerivedPathMap<V>::ensureSlot(const SingleDerivedPath & k)
|
||||
{
|
||||
std::function<ChildNode &(const SingleDerivedPath &)> initIter;
|
||||
initIter = [&](const auto & k) -> auto & {
|
||||
auto initIter = [&](this auto & initIter, const auto & k) -> ChildNode & {
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[&](const SingleDerivedPath::Opaque & bo) -> auto & {
|
||||
[&](const SingleDerivedPath::Opaque & bo) -> ChildNode & {
|
||||
// will not overwrite if already there
|
||||
return map[bo.path];
|
||||
},
|
||||
[&](const SingleDerivedPath::Built & bfd) -> auto & {
|
||||
[&](const SingleDerivedPath::Built & bfd) -> ChildNode & {
|
||||
auto & n = initIter(*bfd.drvPath);
|
||||
return n.childMap[bfd.output];
|
||||
},
|
||||
|
|
@ -27,15 +26,14 @@ typename DerivedPathMap<V>::ChildNode & DerivedPathMap<V>::ensureSlot(const Sing
|
|||
template<typename V>
|
||||
typename DerivedPathMap<V>::ChildNode * DerivedPathMap<V>::findSlot(const SingleDerivedPath & k)
|
||||
{
|
||||
std::function<ChildNode *(const SingleDerivedPath &)> initIter;
|
||||
initIter = [&](const auto & k) {
|
||||
auto initIter = [&](this auto & initIter, const auto & k) -> ChildNode * {
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[&](const SingleDerivedPath::Opaque & bo) {
|
||||
[&](const SingleDerivedPath::Opaque & bo) -> ChildNode * {
|
||||
auto it = map.find(bo.path);
|
||||
return it != map.end() ? &it->second : nullptr;
|
||||
},
|
||||
[&](const SingleDerivedPath::Built & bfd) {
|
||||
[&](const SingleDerivedPath::Built & bfd) -> ChildNode * {
|
||||
auto * n = initIter(*bfd.drvPath);
|
||||
if (!n)
|
||||
return (ChildNode *) nullptr;
|
||||
|
|
|
|||
|
|
@ -146,9 +146,7 @@ struct NarAccessor : public SourceAccessor
|
|||
{
|
||||
using json = nlohmann::json;
|
||||
|
||||
std::function<void(NarMember &, const json &)> recurse;
|
||||
|
||||
recurse = [&](NarMember & member, const json & v) {
|
||||
auto recurse = [&](this auto & recurse, NarMember & member, const json & v) -> void {
|
||||
std::string type = v["type"];
|
||||
|
||||
if (type == "directory") {
|
||||
|
|
|
|||
|
|
@ -284,8 +284,7 @@ TEST_F(GitTest, both_roundrip)
|
|||
|
||||
MemorySink sinkFiles2{*files2};
|
||||
|
||||
std::function<void(const CanonPath, const Hash &, BlobMode)> mkSinkHook;
|
||||
mkSinkHook = [&](auto prefix, auto & hash, auto blobMode) {
|
||||
auto mkSinkHook = [&](this auto & mkSinkHook, auto prefix, auto & hash, auto blobMode) -> void {
|
||||
StringSource in{cas[hash]};
|
||||
parse(
|
||||
sinkFiles2,
|
||||
|
|
|
|||
|
|
@ -14,9 +14,7 @@ std::vector<T> topoSort(
|
|||
std::vector<T> sorted;
|
||||
decltype(items) visited, parents;
|
||||
|
||||
std::function<void(const T & path, const T * parent)> dfsVisit;
|
||||
|
||||
dfsVisit = [&](const T & path, const T * parent) {
|
||||
auto dfsVisit = [&](this auto & dfsVisit, const T & path, const T * parent) -> void {
|
||||
if (parents.count(path))
|
||||
throw makeCycleError(path, *parent);
|
||||
|
||||
|
|
|
|||
|
|
@ -85,9 +85,8 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
|
|||
if (pathExists(*writeTo))
|
||||
throw Error("path '%s' already exists", writeTo->string());
|
||||
|
||||
std::function<void(Value & v, const PosIdx pos, const std::filesystem::path & path)> recurse;
|
||||
|
||||
recurse = [&](Value & v, const PosIdx pos, const std::filesystem::path & path) {
|
||||
auto recurse =
|
||||
[&](this auto & recurse, Value & v, const PosIdx pos, const std::filesystem::path & path) -> void {
|
||||
state->forceValue(v, pos);
|
||||
if (v.type() == nString)
|
||||
// FIXME: disallow strings with contexts?
|
||||
|
|
|
|||
|
|
@ -38,8 +38,7 @@ struct CmdFlakePrefetchInputs : FlakeCommand
|
|||
|
||||
std::atomic<size_t> nrFailed{0};
|
||||
|
||||
std::function<void(const Node & node)> visit;
|
||||
visit = [&](const Node & node) {
|
||||
auto visit = [&](this auto & visit, const Node & node) -> void {
|
||||
if (!state_.lock()->done.insert(&node).second)
|
||||
return;
|
||||
|
||||
|
|
|
|||
|
|
@ -269,9 +269,7 @@ struct CmdFlakeMetadata : FlakeCommand, MixJSON
|
|||
|
||||
std::set<ref<Node>> visited;
|
||||
|
||||
std::function<void(const Node & node, const std::string & prefix)> recurse;
|
||||
|
||||
recurse = [&](const Node & node, const std::string & prefix) {
|
||||
auto recurse = [&](this auto & recurse, const Node & node, const std::string & prefix) -> void {
|
||||
for (const auto & [i, input] : enumerate(node.inputs)) {
|
||||
bool last = i + 1 == node.inputs.size();
|
||||
|
||||
|
|
|
|||
|
|
@ -90,10 +90,10 @@ struct CmdSearch : InstallableValueCommand, MixJSON
|
|||
|
||||
uint64_t results = 0;
|
||||
|
||||
std::function<void(eval_cache::AttrCursor & cursor, const std::vector<Symbol> & attrPath, bool initialRecurse)>
|
||||
visit;
|
||||
|
||||
visit = [&](eval_cache::AttrCursor & cursor, const std::vector<Symbol> & attrPath, bool initialRecurse) {
|
||||
auto visit = [&](this auto & visit,
|
||||
eval_cache::AttrCursor & cursor,
|
||||
const std::vector<Symbol> & attrPath,
|
||||
bool initialRecurse) -> void {
|
||||
auto attrPathS = state->symbols.resolve(attrPath);
|
||||
|
||||
Activity act(*logger, lvlInfo, actUnknown, fmt("evaluating '%s'", concatStringsSep(".", attrPathS)));
|
||||
|
|
|
|||
|
|
@ -160,16 +160,15 @@ struct CmdWhyDepends : SourceExprCommand, MixOperateOnOptions
|
|||
}
|
||||
}
|
||||
|
||||
struct BailOut
|
||||
{};
|
||||
|
||||
/* Print the subgraph of nodes that have 'dependency' in their
|
||||
closure (i.e., that have a non-infinite distance to
|
||||
'dependency'). Print every edge on a path between `package`
|
||||
and `dependency`. */
|
||||
std::function<void(Node &, const std::string &, const std::string &)> printNode;
|
||||
|
||||
struct BailOut
|
||||
{};
|
||||
|
||||
printNode = [&](Node & node, const std::string & firstPad, const std::string & tailPad) {
|
||||
auto printNode =
|
||||
[&](this auto & printNode, Node & node, const std::string & firstPad, const std::string & tailPad) -> void {
|
||||
assert(node.dist != inf);
|
||||
if (precise) {
|
||||
logger->cout(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue