From ed96e603e116123c1e44f5108b67b472b2c96538 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 5 Aug 2020 19:44:08 +0000 Subject: [PATCH 1/4] Proxy -> Phantom to match Rust Sorry, Haskell. --- src/libstore/remote-store.cc | 4 ++-- src/libstore/worker-protocol.hh | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc index 377c81ff4..2c0857466 100644 --- a/src/libstore/remote-store.cc +++ b/src/libstore/remote-store.cc @@ -58,7 +58,7 @@ void writeStorePathCAMap(const Store & store, Sink & out, const StorePathCAMap & } -StorePath read(const Store & store, Source & from, Proxy _) +StorePath read(const Store & store, Source & from, Phantom _) { return store.parseStorePath(readString(from)); } @@ -461,7 +461,7 @@ std::map> RemoteStore::queryDerivationOutp auto conn(getConnection()); conn->to << wopQueryDerivationOutputMap << printStorePath(path); conn.processStderr(); - return read(*this, conn->from, Proxy>> {}); + return read(*this, conn->from, Phantom>> {}); } diff --git a/src/libstore/worker-protocol.hh b/src/libstore/worker-protocol.hh index 117d3e1a4..384b81f08 100644 --- a/src/libstore/worker-protocol.hh +++ b/src/libstore/worker-protocol.hh @@ -72,16 +72,16 @@ void writeStorePaths(const Store & store, Sink & out, const StorePathSet & paths /* To guide overloading */ template -struct Proxy {}; +struct Phantom {}; template -std::map read(const Store & store, Source & from, Proxy> _) +std::map read(const Store & store, Source & from, Phantom> _) { std::map resMap; auto size = (size_t)readInt(from); while (size--) { auto thisKey = readString(from); - resMap.insert_or_assign(std::move(thisKey), read(store, from, Proxy {})); + resMap.insert_or_assign(std::move(thisKey), read(store, from, Phantom {})); } return resMap; } @@ -97,14 +97,14 @@ void write(const Store & store, Sink & out, const std::map & resMap) } template -std::optional read(const Store & store, Source & from, Proxy> _) +std::optional read(const Store & store, Source & from, Phantom> _) { auto tag = readNum(from); switch (tag) { case 0: return std::nullopt; case 1: - return read(store, from, Proxy {}); + return read(store, from, Phantom {}); default: throw Error("got an invalid tag bit for std::optional: %#04x", tag); } @@ -118,7 +118,7 @@ void write(const Store & store, Sink & out, const std::optional & optVal) write(store, out, *optVal); } -StorePath read(const Store & store, Source & from, Proxy _); +StorePath read(const Store & store, Source & from, Phantom _); void write(const Store & store, Sink & out, const StorePath & storePath); From 6c66331d5c34d97092a9b5e2832fed73e0da3c87 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Wed, 5 Aug 2020 20:37:48 +0000 Subject: [PATCH 2/4] WIP: Put the worker protocol `read` and `write` in a namespace to disambig --- src/libstore/remote-store.cc | 6 +++++- src/libstore/worker-protocol.hh | 15 +++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc index 2c0857466..96e0aabba 100644 --- a/src/libstore/remote-store.cc +++ b/src/libstore/remote-store.cc @@ -58,6 +58,8 @@ void writeStorePathCAMap(const Store & store, Sink & out, const StorePathCAMap & } +namespace worker_proto { + StorePath read(const Store & store, Source & from, Phantom _) { return store.parseStorePath(readString(from)); @@ -68,6 +70,8 @@ void write(const Store & store, Sink & out, const StorePath & storePath) out << store.printStorePath(storePath); } +} + /* TODO: Separate these store impls into different files, give them better names */ RemoteStore::RemoteStore(const Params & params) @@ -461,7 +465,7 @@ std::map> RemoteStore::queryDerivationOutp auto conn(getConnection()); conn->to << wopQueryDerivationOutputMap << printStorePath(path); conn.processStderr(); - return read(*this, conn->from, Phantom>> {}); + return worker_proto::read(*this, conn->from, Phantom>> {}); } diff --git a/src/libstore/worker-protocol.hh b/src/libstore/worker-protocol.hh index 384b81f08..fdd700668 100644 --- a/src/libstore/worker-protocol.hh +++ b/src/libstore/worker-protocol.hh @@ -74,6 +74,10 @@ void writeStorePaths(const Store & store, Sink & out, const StorePathSet & paths template struct Phantom {}; + +namespace worker_proto { +/* FIXME maybe move more stuff inside here */ + template std::map read(const Store & store, Source & from, Phantom> _) { @@ -81,7 +85,7 @@ std::map read(const Store & store, Source & from, Phantom {})); + resMap.insert_or_assign(std::move(thisKey), nix::worker_proto::read(store, from, Phantom {})); } return resMap; } @@ -92,7 +96,7 @@ void write(const Store & store, Sink & out, const std::map & resMap) out << resMap.size(); for (auto & i : resMap) { out << i.first; - write(store, out, i.second); + nix::worker_proto::write(store, out, i.second); } } @@ -104,7 +108,7 @@ std::optional read(const Store & store, Source & from, Phantom {}); + return nix::worker_proto::read(store, from, Phantom {}); default: throw Error("got an invalid tag bit for std::optional: %#04x", tag); } @@ -115,13 +119,16 @@ void write(const Store & store, Sink & out, const std::optional & optVal) { out << (optVal ? 1 : 0); if (optVal) - write(store, out, *optVal); + nix::worker_proto::write(store, out, *optVal); } StorePath read(const Store & store, Source & from, Phantom _); void write(const Store & store, Sink & out, const StorePath & storePath); +} + + StorePathCAMap readStorePathCAMap(const Store & store, Source & from); void writeStorePathCAMap(const Store & store, Sink & out, const StorePathCAMap & paths); From 0739d428e07b59b7d44bdbf7354dafaae09dba7c Mon Sep 17 00:00:00 2001 From: Carlo Nucera Date: Wed, 5 Aug 2020 17:49:45 -0400 Subject: [PATCH 3/4] Solve template deduction problem We had to predeclare our template functions --- src/libstore/daemon.cc | 2 +- src/libstore/worker-protocol.hh | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index e18f793c8..a420dcab0 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -327,7 +327,7 @@ static void performOp(TunnelLogger * logger, ref store, logger->startWork(); auto outputs = store->queryDerivationOutputMap(path); logger->stopWork(); - write(*store, to, outputs); + nix::worker_proto::write(*store, to, outputs); break; } diff --git a/src/libstore/worker-protocol.hh b/src/libstore/worker-protocol.hh index fdd700668..c50995d7c 100644 --- a/src/libstore/worker-protocol.hh +++ b/src/libstore/worker-protocol.hh @@ -78,6 +78,19 @@ struct Phantom {}; namespace worker_proto { /* FIXME maybe move more stuff inside here */ +StorePath read(const Store & store, Source & from, Phantom _); +void write(const Store & store, Sink & out, const StorePath & storePath); + +template +std::map read(const Store & store, Source & from, Phantom> _); +template +void write(const Store & store, Sink & out, const std::map & resMap); +template +std::optional read(const Store & store, Source & from, Phantom> _); +template +void write(const Store & store, Sink & out, const std::optional & optVal); + + template std::map read(const Store & store, Source & from, Phantom> _) { @@ -122,9 +135,6 @@ void write(const Store & store, Sink & out, const std::optional & optVal) nix::worker_proto::write(store, out, *optVal); } -StorePath read(const Store & store, Source & from, Phantom _); - -void write(const Store & store, Sink & out, const StorePath & storePath); } From 8b175f58d1a25dd904fb0ffdf5b2b9501983dba0 Mon Sep 17 00:00:00 2001 From: Carlo Nucera Date: Wed, 5 Aug 2020 17:57:07 -0400 Subject: [PATCH 4/4] Simplify the namespace --- src/libstore/daemon.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index a420dcab0..9503915eb 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -327,7 +327,7 @@ static void performOp(TunnelLogger * logger, ref store, logger->startWork(); auto outputs = store->queryDerivationOutputMap(path); logger->stopWork(); - nix::worker_proto::write(*store, to, outputs); + worker_proto::write(*store, to, outputs); break; }