From 5eec6418de35daaa7b14b5412e39d85ce80a37cb Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Tue, 19 Mar 2024 22:22:18 +0100 Subject: libutil: begin porting serialization to generators generators are a better basis for serializers than streaming into sinks as we do currently for many reasons, such as being usable as sources if one wishes to (without requiring an intermediate sink to serialize full data sets into memory, or boost coroutines to turn sinks into sources), composing more naturally (as one can just yield a sub-generator instead of being forced to wrap entire substreams into clunky functions or even more clunky custom types to implement operator<< on), allowing wrappers to transform data with clear ownership semantics (removing the need for explicit memory allocations and Source wrappers), and many other things Change-Id: I361d89ff556354f6930d9204f55117565f2f7f20 --- src/libutil/serialise.hh | 85 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 18 deletions(-) (limited to 'src/libutil/serialise.hh') diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh index 491b1987d..2651ec979 100644 --- a/src/libutil/serialise.hh +++ b/src/libutil/serialise.hh @@ -350,33 +350,82 @@ inline Sink & operator<<(Sink & sink, Generator && g) return sink; } +struct SerializingTransform; +using WireFormatGenerator = Generator; + +struct SerializingTransform +{ + std::array buf; + + Bytes operator()(uint64_t n) + { + buf[0] = n & 0xff; + buf[1] = (n >> 8) & 0xff; + buf[2] = (n >> 16) & 0xff; + buf[3] = (n >> 24) & 0xff; + buf[4] = (n >> 32) & 0xff; + buf[5] = (n >> 40) & 0xff; + buf[6] = (n >> 48) & 0xff; + buf[7] = (unsigned char) (n >> 56) & 0xff; + return {reinterpret_cast(buf.begin()), 8}; + } + + static Bytes padding(size_t unpadded) + { + return Bytes("\0\0\0\0\0\0\0", unpadded % 8 ? 8 - unpadded % 8 : 0); + } + + // opt in to generator chaining. without this co_yielding + // another generator of any type will cause a type error. + auto operator()(Generator && g) + { + return std::move(g); + } + + // only choose this for *exactly* char spans, do not allow implicit + // conversions. this would cause ambiguities with strings literals, + // and resolving those with more string-like overloads needs a lot. + template + requires std::same_as> || std::same_as> + Bytes operator()(Span s) + { + return s; + } + WireFormatGenerator operator()(std::string_view s); + WireFormatGenerator operator()(const Strings & s); + WireFormatGenerator operator()(const StringSet & s); + WireFormatGenerator operator()(const Error & s); +}; + void writePadding(size_t len, Sink & sink); -void writeString(std::string_view s, Sink & sink); -inline Sink & operator << (Sink & sink, uint64_t n) +inline Sink & operator<<(Sink & sink, uint64_t u) { - unsigned char buf[8]; - buf[0] = n & 0xff; - buf[1] = (n >> 8) & 0xff; - buf[2] = (n >> 16) & 0xff; - buf[3] = (n >> 24) & 0xff; - buf[4] = (n >> 32) & 0xff; - buf[5] = (n >> 40) & 0xff; - buf[6] = (n >> 48) & 0xff; - buf[7] = (unsigned char) (n >> 56) & 0xff; - sink({(char *) buf, sizeof(buf)}); - return sink; + return sink << [&]() -> WireFormatGenerator { co_yield u; }(); +} + +inline Sink & operator<<(Sink & sink, std::string_view s) +{ + return sink << [&]() -> WireFormatGenerator { co_yield s; }(); +} + +inline Sink & operator<<(Sink & sink, const Strings & s) +{ + return sink << [&]() -> WireFormatGenerator { co_yield s; }(); } -Sink & operator << (Sink & in, const Error & ex); -Sink & operator << (Sink & sink, std::string_view s); -Sink & operator << (Sink & sink, const Strings & s); -Sink & operator << (Sink & sink, const StringSet & s); +inline Sink & operator<<(Sink & sink, const StringSet & s) +{ + return sink << [&]() -> WireFormatGenerator { co_yield s; }(); +} +inline Sink & operator<<(Sink & sink, const Error & ex) +{ + return sink << [&]() -> WireFormatGenerator { co_yield ex; }(); +} MakeError(SerialisationError, Error); - template T readNum(Source & source) { -- cgit v1.2.3