From a073db1e52cdfdc55efbd1816adae4f524be2385 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 21 Oct 2023 13:33:25 +0200 Subject: [PATCH] util/SpanCast: add ReferenceAsBytes() --- src/io/BufferedOutputStream.hxx | 2 +- src/output/plugins/snapcast/Client.cxx | 2 +- src/util/SpanCast.hxx | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/io/BufferedOutputStream.hxx b/src/io/BufferedOutputStream.hxx index 65548d963..68d0c0c4f 100644 --- a/src/io/BufferedOutputStream.hxx +++ b/src/io/BufferedOutputStream.hxx @@ -51,7 +51,7 @@ public: */ template void WriteT(const T &value) { - Write(std::as_bytes(std::span{&value, 1})); + Write(ReferenceAsBytes(value)); } /** diff --git a/src/output/plugins/snapcast/Client.cxx b/src/output/plugins/snapcast/Client.cxx index 805cc6784..1b9d72822 100644 --- a/src/output/plugins/snapcast/Client.cxx +++ b/src/output/plugins/snapcast/Client.cxx @@ -105,7 +105,7 @@ template static bool SendT(SocketDescriptor s, const T &buffer) noexcept { - return Send(s, std::as_bytes(std::span{&buffer, 1})); + return Send(s, ReferenceAsBytes(buffer)); } static bool diff --git a/src/util/SpanCast.hxx b/src/util/SpanCast.hxx index 90cc39aae..e6dc5e665 100644 --- a/src/util/SpanCast.hxx +++ b/src/util/SpanCast.hxx @@ -59,6 +59,16 @@ AsBytes(std::string_view sv) noexcept return std::as_bytes(ToSpan(sv)); } +/** + * Cast a reference to a fixed-size std::span. + */ +template +constexpr auto +ReferenceAsBytes(const T &value) noexcept +{ + return std::as_bytes(std::span{&value, 1}); +} + constexpr std::string_view ToStringView(std::span s) noexcept {