diff options
author | eldritch horrors <pennae@lix.systems> | 2024-05-18 02:35:34 +0200 |
---|---|---|
committer | eldritch horrors <pennae@lix.systems> | 2024-07-16 00:57:42 +0000 |
commit | a5d1f698417dff4e470eb94c38ad6c4e5ebf38ff (patch) | |
tree | 116b887a80fd96f9008bc55c872a24ef1955ca2f /src/libstore/length-prefixed-protocol-helper.hh | |
parent | 5271424d14241ec5cbe7ab3bda85ddeb486cff76 (diff) |
libstore: generatorize protocol serializers
this is cursed. deeply and profoundly cursed. under NO CIRCUMSTANCES
must protocol serializer helpers be applied to temporaries! doing so
will inevitably cause dangling references and cause the entire thing
to crash. we need to do this even so to get rid of boost coroutines,
and likewise to encapsulate the serializers we suffer today at least
a little bit to allow a gradual migration to an actual IPC protocol.
(this isn't a problem that's unique to generators. c++ coroutines in
general cannot safely take references to arbitrary temporaries since
c++ does not have a lifetime system that can make this safe. -sigh-)
Change-Id: I2921ba451e04d86798752d140885d3c5cc08e146
Diffstat (limited to 'src/libstore/length-prefixed-protocol-helper.hh')
-rw-r--r-- | src/libstore/length-prefixed-protocol-helper.hh | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/src/libstore/length-prefixed-protocol-helper.hh b/src/libstore/length-prefixed-protocol-helper.hh index 4061b0cd6..1475d2690 100644 --- a/src/libstore/length-prefixed-protocol-helper.hh +++ b/src/libstore/length-prefixed-protocol-helper.hh @@ -7,6 +7,7 @@ */ #include "types.hh" +#include "serialise.hh" namespace nix { @@ -45,7 +46,7 @@ struct LengthPrefixedProtoHelper; struct LengthPrefixedProtoHelper< Inner, T > \ { \ static T read(const Store & store, typename Inner::ReadConn conn); \ - static void write(const Store & store, typename Inner::WriteConn conn, const T & str); \ + [[nodiscard]] static WireFormatGenerator write(const Store & store, typename Inner::WriteConn conn, const T & str); \ private: \ template<typename U> using S = typename Inner::template Serialise<U>; \ } @@ -78,13 +79,13 @@ LengthPrefixedProtoHelper<Inner, std::vector<T>>::read( } template<class Inner, typename T> -void +WireFormatGenerator LengthPrefixedProtoHelper<Inner, std::vector<T>>::write( const Store & store, typename Inner::WriteConn conn, const std::vector<T> & resSet) { - conn.to << resSet.size(); + co_yield resSet.size(); for (auto & key : resSet) { - S<T>::write(store, conn, key); + co_yield S<T>::write(store, conn, key); } } @@ -102,13 +103,13 @@ LengthPrefixedProtoHelper<Inner, std::set<T>>::read( } template<class Inner, typename T> -void +WireFormatGenerator LengthPrefixedProtoHelper<Inner, std::set<T>>::write( const Store & store, typename Inner::WriteConn conn, const std::set<T> & resSet) { - conn.to << resSet.size(); + co_yield resSet.size(); for (auto & key : resSet) { - S<T>::write(store, conn, key); + co_yield S<T>::write(store, conn, key); } } @@ -128,14 +129,14 @@ LengthPrefixedProtoHelper<Inner, std::map<K, V>>::read( } template<class Inner, typename K, typename V> -void +WireFormatGenerator LengthPrefixedProtoHelper<Inner, std::map<K, V>>::write( const Store & store, typename Inner::WriteConn conn, const std::map<K, V> & resMap) { - conn.to << resMap.size(); + co_yield resMap.size(); for (auto & i : resMap) { - S<K>::write(store, conn, i.first); - S<V>::write(store, conn, i.second); + co_yield S<K>::write(store, conn, i.first); + co_yield S<V>::write(store, conn, i.second); } } @@ -150,13 +151,24 @@ LengthPrefixedProtoHelper<Inner, std::tuple<Ts...>>::read( } template<class Inner, typename... Ts> -void +WireFormatGenerator LengthPrefixedProtoHelper<Inner, std::tuple<Ts...>>::write( const Store & store, typename Inner::WriteConn conn, const std::tuple<Ts...> & res) { - std::apply([&]<typename... Us>(const Us &... args) { - (S<Us>::write(store, conn, args), ...); - }, res); + auto fullArgs = std::apply( + [&](auto &... rest) { + return std::tuple<const Store &, typename Inner::WriteConn &, const Ts &...>( + std::cref(store), conn, rest... + ); + }, + res + ); + return std::apply( + []<typename... Us>(auto & store, auto conn, const Us &... args) -> WireFormatGenerator { + (co_yield S<Us>::write(store, conn, args), ...); + }, + fullArgs + ); } } |