From db03db0dca542546b3fabd3edb97e77751ca4971 Mon Sep 17 00:00:00 2001 From: Max Kellermann <max@musicpd.org> Date: Wed, 1 Jun 2022 21:39:56 +0200 Subject: [PATCH] util/SpanCast: add FromBytesStrict() --- src/util/SpanCast.hxx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/util/SpanCast.hxx b/src/util/SpanCast.hxx index ea2a870bb..a1bccac63 100644 --- a/src/util/SpanCast.hxx +++ b/src/util/SpanCast.hxx @@ -29,6 +29,7 @@ #pragma once +#include <cassert> #include <cstddef> #include <span> #include <string_view> @@ -52,6 +53,18 @@ FromBytesFloor(std::span<CopyConst<T, std::byte>> other) noexcept }; } +/** + * Like FromBytesFloor(), but assert that rounding is not necessary. + */ +template<typename T> +constexpr std::span<T> +FromBytesStrict(std::span<CopyConst<T, std::byte>> other) noexcept +{ + assert(other.size() % sizeof(T) == 0); + + return FromBytesFloor<T>(other); +} + constexpr std::span<const char> ToSpan(std::string_view sv) noexcept {