aboutsummaryrefslogtreecommitdiff
path: root/src/libstore
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2022-03-08 21:53:26 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2023-06-19 11:45:59 -0400
commit469d06f9bc9b2543f48d8e633e47f9344fba35ab (patch)
tree27a5d20348ceec93bbbfdcbc0a16db63802712c6 /src/libstore
parentf5e620bf2be7ed552962ab5b6637771d5a4d64d3 (diff)
Split out worker protocol template definitions from declarations
This is generally a fine practice: Putting implementations in headers makes them harder to read and slows compilation. Unfortunately it is necessary for templates, but we can ameliorate that by putting them in a separate header. Only files which need to instantiate those templates will need to include the header with the implementation; the rest can just include the declaration. This is now documenting in the contributing guide. Also, it just happens that these polymorphic serializers are the protocol agnostic ones. (Worker and serve protocol have the same logic for these container types.) This means by doing this general template cleanup, we are also getting a head start on better indicating which code is protocol-specific and which code is shared between protocols.
Diffstat (limited to 'src/libstore')
-rw-r--r--src/libstore/build/derivation-goal.cc1
-rw-r--r--src/libstore/build/local-derivation-goal.cc1
-rw-r--r--src/libstore/daemon.cc1
-rw-r--r--src/libstore/derivations.cc1
-rw-r--r--src/libstore/export-import.cc1
-rw-r--r--src/libstore/legacy-ssh-store.cc1
-rw-r--r--src/libstore/path-info.cc1
-rw-r--r--src/libstore/remote-store.cc1
-rw-r--r--src/libstore/worker-protocol-impl.hh78
-rw-r--r--src/libstore/worker-protocol.cc1
-rw-r--r--src/libstore/worker-protocol.hh63
11 files changed, 87 insertions, 63 deletions
diff --git a/src/libstore/build/derivation-goal.cc b/src/libstore/build/derivation-goal.cc
index df7d21e54..e02000c8b 100644
--- a/src/libstore/build/derivation-goal.cc
+++ b/src/libstore/build/derivation-goal.cc
@@ -9,6 +9,7 @@
#include "archive.hh"
#include "compression.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include "topo-sort.hh"
#include "callback.hh"
#include "local-store.hh" // TODO remove, along with remaining downcasts
diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc
index 7f87cdf55..6e06f6061 100644
--- a/src/libstore/build/local-derivation-goal.cc
+++ b/src/libstore/build/local-derivation-goal.cc
@@ -11,6 +11,7 @@
#include "compression.hh"
#include "daemon.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include "topo-sort.hh"
#include "callback.hh"
#include "json-utils.hh"
diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc
index b6dd83684..54b089b30 100644
--- a/src/libstore/daemon.cc
+++ b/src/libstore/daemon.cc
@@ -1,6 +1,7 @@
#include "daemon.hh"
#include "monitor-fd.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include "build-result.hh"
#include "store-api.hh"
#include "store-cast.hh"
diff --git a/src/libstore/derivations.cc b/src/libstore/derivations.cc
index ccb165d68..4a1f1e99f 100644
--- a/src/libstore/derivations.cc
+++ b/src/libstore/derivations.cc
@@ -5,6 +5,7 @@
#include "util.hh"
#include "split.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include "fs-accessor.hh"
#include <boost/container/small_vector.hpp>
#include <nlohmann/json.hpp>
diff --git a/src/libstore/export-import.cc b/src/libstore/export-import.cc
index 5ea263a86..72ad77124 100644
--- a/src/libstore/export-import.cc
+++ b/src/libstore/export-import.cc
@@ -2,6 +2,7 @@
#include "store-api.hh"
#include "archive.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include <algorithm>
diff --git a/src/libstore/legacy-ssh-store.cc b/src/libstore/legacy-ssh-store.cc
index 2b7bebe9d..bff024f96 100644
--- a/src/libstore/legacy-ssh-store.cc
+++ b/src/libstore/legacy-ssh-store.cc
@@ -7,6 +7,7 @@
#include "store-api.hh"
#include "path-with-outputs.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include "ssh.hh"
#include "derivations.hh"
#include "callback.hh"
diff --git a/src/libstore/path-info.cc b/src/libstore/path-info.cc
index 97b72faa3..6b93976fa 100644
--- a/src/libstore/path-info.cc
+++ b/src/libstore/path-info.cc
@@ -1,5 +1,6 @@
#include "path-info.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include "store-api.hh"
namespace nix {
diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc
index c3dfb5979..f02342d32 100644
--- a/src/libstore/remote-store.cc
+++ b/src/libstore/remote-store.cc
@@ -6,6 +6,7 @@
#include "build-result.hh"
#include "remote-store.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include "archive.hh"
#include "globals.hh"
#include "derivations.hh"
diff --git a/src/libstore/worker-protocol-impl.hh b/src/libstore/worker-protocol-impl.hh
new file mode 100644
index 000000000..509a8a5d0
--- /dev/null
+++ b/src/libstore/worker-protocol-impl.hh
@@ -0,0 +1,78 @@
+#pragma once
+/**
+ * @file
+ *
+ * Template implementations (as opposed to mere declarations).
+ *
+ * This file is an exmample of the "impl.hh" pattern. See the
+ * contributing guide.
+ */
+
+#include "worker-protocol.hh"
+
+namespace nix {
+
+template<typename T>
+std::vector<T> WorkerProto<std::vector<T>>::read(const Store & store, Source & from)
+{
+ std::vector<T> resSet;
+ auto size = readNum<size_t>(from);
+ while (size--) {
+ resSet.push_back(WorkerProto<T>::read(store, from));
+ }
+ return resSet;
+}
+
+template<typename T>
+void WorkerProto<std::vector<T>>::write(const Store & store, Sink & out, const std::vector<T> & resSet)
+{
+ out << resSet.size();
+ for (auto & key : resSet) {
+ WorkerProto<T>::write(store, out, key);
+ }
+}
+
+template<typename T>
+std::set<T> WorkerProto<std::set<T>>::read(const Store & store, Source & from)
+{
+ std::set<T> resSet;
+ auto size = readNum<size_t>(from);
+ while (size--) {
+ resSet.insert(WorkerProto<T>::read(store, from));
+ }
+ return resSet;
+}
+
+template<typename T>
+void WorkerProto<std::set<T>>::write(const Store & store, Sink & out, const std::set<T> & resSet)
+{
+ out << resSet.size();
+ for (auto & key : resSet) {
+ WorkerProto<T>::write(store, out, key);
+ }
+}
+
+template<typename K, typename V>
+std::map<K, V> WorkerProto<std::map<K, V>>::read(const Store & store, Source & from)
+{
+ std::map<K, V> resMap;
+ auto size = readNum<size_t>(from);
+ while (size--) {
+ auto k = WorkerProto<K>::read(store, from);
+ auto v = WorkerProto<V>::read(store, from);
+ resMap.insert_or_assign(std::move(k), std::move(v));
+ }
+ return resMap;
+}
+
+template<typename K, typename V>
+void WorkerProto<std::map<K, V>>::write(const Store & store, Sink & out, const std::map<K, V> & resMap)
+{
+ out << resMap.size();
+ for (auto & i : resMap) {
+ WorkerProto<K>::write(store, out, i.first);
+ WorkerProto<V>::write(store, out, i.second);
+ }
+}
+
+}
diff --git a/src/libstore/worker-protocol.cc b/src/libstore/worker-protocol.cc
index 51bb12026..49708b642 100644
--- a/src/libstore/worker-protocol.cc
+++ b/src/libstore/worker-protocol.cc
@@ -4,6 +4,7 @@
#include "store-api.hh"
#include "build-result.hh"
#include "worker-protocol.hh"
+#include "worker-protocol-impl.hh"
#include "archive.hh"
#include "derivations.hh"
diff --git a/src/libstore/worker-protocol.hh b/src/libstore/worker-protocol.hh
index f06332d17..18a4e11b2 100644
--- a/src/libstore/worker-protocol.hh
+++ b/src/libstore/worker-protocol.hh
@@ -173,67 +173,4 @@ MAKE_WORKER_PROTO(std::optional<StorePath>);
template<>
MAKE_WORKER_PROTO(std::optional<ContentAddress>);
-template<typename T>
-std::vector<T> WorkerProto<std::vector<T>>::read(const Store & store, Source & from)
-{
- std::vector<T> resSet;
- auto size = readNum<size_t>(from);
- while (size--) {
- resSet.push_back(WorkerProto<T>::read(store, from));
- }
- return resSet;
-}
-
-template<typename T>
-void WorkerProto<std::vector<T>>::write(const Store & store, Sink & out, const std::vector<T> & resSet)
-{
- out << resSet.size();
- for (auto & key : resSet) {
- WorkerProto<T>::write(store, out, key);
- }
-}
-
-template<typename T>
-std::set<T> WorkerProto<std::set<T>>::read(const Store & store, Source & from)
-{
- std::set<T> resSet;
- auto size = readNum<size_t>(from);
- while (size--) {
- resSet.insert(WorkerProto<T>::read(store, from));
- }
- return resSet;
-}
-
-template<typename T>
-void WorkerProto<std::set<T>>::write(const Store & store, Sink & out, const std::set<T> & resSet)
-{
- out << resSet.size();
- for (auto & key : resSet) {
- WorkerProto<T>::write(store, out, key);
- }
-}
-
-template<typename K, typename V>
-std::map<K, V> WorkerProto<std::map<K, V>>::read(const Store & store, Source & from)
-{
- std::map<K, V> resMap;
- auto size = readNum<size_t>(from);
- while (size--) {
- auto k = WorkerProto<K>::read(store, from);
- auto v = WorkerProto<V>::read(store, from);
- resMap.insert_or_assign(std::move(k), std::move(v));
- }
- return resMap;
-}
-
-template<typename K, typename V>
-void WorkerProto<std::map<K, V>>::write(const Store & store, Sink & out, const std::map<K, V> & resMap)
-{
- out << resMap.size();
- for (auto & i : resMap) {
- WorkerProto<K>::write(store, out, i.first);
- WorkerProto<V>::write(store, out, i.second);
- }
-}
-
}