util/SpanCast: add FromBytesStrict()

This commit is contained in:
Max Kellermann 2022-06-01 21:39:56 +02:00
parent bd96f6e572
commit db03db0dca
1 changed files with 13 additions and 0 deletions

View File

@ -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
{