output/snapcast/Client: work around clang 14 std::span cast bug

Closes https://github.com/MusicPlayerDaemon/MPD/issues/1538
This commit is contained in:
Max Kellermann 2022-05-24 13:50:04 +02:00
parent 843dad19e9
commit f045cf43e4
2 changed files with 21 additions and 1 deletions

View File

@ -25,6 +25,7 @@
#include "event/Loop.hxx" #include "event/Loop.hxx"
#include "net/SocketError.hxx" #include "net/SocketError.hxx"
#include "net/UniqueSocketDescriptor.hxx" #include "net/UniqueSocketDescriptor.hxx"
#include "util/SpanCast.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <cassert> #include <cassert>
@ -126,7 +127,7 @@ SendT(SocketDescriptor s, const T &buffer) noexcept
static bool static bool
Send(SocketDescriptor s, std::string_view buffer) noexcept Send(SocketDescriptor s, std::string_view buffer) noexcept
{ {
return Send(s, std::as_bytes(std::span{buffer})); return Send(s, AsBytes(buffer));
} }
static bool static bool

View File

@ -31,6 +31,7 @@
#include <cstddef> #include <cstddef>
#include <span> #include <span>
#include <string_view>
/** /**
* Cast a std::span<std::byte> to a std::span<T>, rounding down to the * Cast a std::span<std::byte> to a std::span<T>, rounding down to the
@ -47,3 +48,21 @@ FromBytesFloor(std::span<std::byte> other) noexcept
other.size() / sizeof(T), other.size() / sizeof(T),
}; };
} }
constexpr std::span<const char>
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<const std::byte>
AsBytes(std::string_view sv) noexcept
{
return std::as_bytes(ToSpan(sv));
}