From f045cf43e4fa8e840e3cc62461a86df65bab14c9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 24 May 2022 13:50:04 +0200 Subject: [PATCH] output/snapcast/Client: work around clang 14 std::span cast bug Closes https://github.com/MusicPlayerDaemon/MPD/issues/1538 --- src/output/plugins/snapcast/Client.cxx | 3 ++- src/util/SpanCast.hxx | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/output/plugins/snapcast/Client.cxx b/src/output/plugins/snapcast/Client.cxx index 882baa571..bd92fbf67 100644 --- a/src/output/plugins/snapcast/Client.cxx +++ b/src/output/plugins/snapcast/Client.cxx @@ -25,6 +25,7 @@ #include "event/Loop.hxx" #include "net/SocketError.hxx" #include "net/UniqueSocketDescriptor.hxx" +#include "util/SpanCast.hxx" #include "Log.hxx" #include @@ -126,7 +127,7 @@ SendT(SocketDescriptor s, const T &buffer) noexcept static bool Send(SocketDescriptor s, std::string_view buffer) noexcept { - return Send(s, std::as_bytes(std::span{buffer})); + return Send(s, AsBytes(buffer)); } static bool diff --git a/src/util/SpanCast.hxx b/src/util/SpanCast.hxx index a2cb624ce..11254c48c 100644 --- a/src/util/SpanCast.hxx +++ b/src/util/SpanCast.hxx @@ -31,6 +31,7 @@ #include #include +#include /** * Cast a std::span to a std::span, rounding down to the @@ -47,3 +48,21 @@ FromBytesFloor(std::span other) noexcept other.size() / sizeof(T), }; } + +constexpr std::span +ToSpan(std::string_view sv) noexcept +{ +#if defined(__clang__) && __clang_major__ < 15 + /* workaround for old clang/libc++ versions which can't cast + std::string_view to std::span */ + return {sv.data(), sv.size()}; +#else + return std::span{sv}; +#endif +} + +inline std::span +AsBytes(std::string_view sv) noexcept +{ + return std::as_bytes(ToSpan(sv)); +}