aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/serve-protocol-impl.hh
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-03-04 04:35:39 +0100
committereldritch horrors <pennae@lix.systems>2024-03-04 04:37:05 +0100
commite12e9f2452681a3036884e9e778154b35246d39e (patch)
tree57aaea8bf74c53e28fba9598cea034e9b08ce9f5 /src/libstore/serve-protocol-impl.hh
parentaeb803de9ad3cd449f7dc85588430ed5b21503eb (diff)
Merge pull request #9137 from obsidiansystems/serve-protocol
Introduce separate Serve protocol serialisers (cherry picked from commit d070d8b7460f412a657745698dba291c66792402) Change-Id: Ibcc8639e8997bcd07f7a5318330a147bcadc411b
Diffstat (limited to 'src/libstore/serve-protocol-impl.hh')
-rw-r--r--src/libstore/serve-protocol-impl.hh59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/libstore/serve-protocol-impl.hh b/src/libstore/serve-protocol-impl.hh
new file mode 100644
index 000000000..a3ce81026
--- /dev/null
+++ b/src/libstore/serve-protocol-impl.hh
@@ -0,0 +1,59 @@
+#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 "serve-protocol.hh"
+#include "length-prefixed-protocol-helper.hh"
+
+namespace nix {
+
+/* protocol-agnostic templates */
+
+#define SERVE_USE_LENGTH_PREFIX_SERIALISER(TEMPLATE, T) \
+ TEMPLATE T ServeProto::Serialise< T >::read(const Store & store, ServeProto::ReadConn conn) \
+ { \
+ return LengthPrefixedProtoHelper<ServeProto, T >::read(store, conn); \
+ } \
+ TEMPLATE void ServeProto::Serialise< T >::write(const Store & store, ServeProto::WriteConn conn, const T & t) \
+ { \
+ LengthPrefixedProtoHelper<ServeProto, T >::write(store, conn, t); \
+ }
+
+SERVE_USE_LENGTH_PREFIX_SERIALISER(template<typename T>, std::vector<T>)
+SERVE_USE_LENGTH_PREFIX_SERIALISER(template<typename T>, std::set<T>)
+SERVE_USE_LENGTH_PREFIX_SERIALISER(template<typename... Ts>, std::tuple<Ts...>)
+
+#define COMMA_ ,
+SERVE_USE_LENGTH_PREFIX_SERIALISER(
+ template<typename K COMMA_ typename V>,
+ std::map<K COMMA_ V>)
+#undef COMMA_
+
+/**
+ * Use `CommonProto` where possible.
+ */
+template<typename T>
+struct ServeProto::Serialise
+{
+ static T read(const Store & store, ServeProto::ReadConn conn)
+ {
+ return CommonProto::Serialise<T>::read(store,
+ CommonProto::ReadConn { .from = conn.from });
+ }
+ static void write(const Store & store, ServeProto::WriteConn conn, const T & t)
+ {
+ CommonProto::Serialise<T>::write(store,
+ CommonProto::WriteConn { .to = conn.to },
+ t);
+ }
+};
+
+/* protocol-specific templates */
+
+}