Skip to content

Commit

Permalink
util/SpanCast: add FromBytesStrict()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jun 1, 2022
1 parent bd96f6e commit db03db0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/util/SpanCast.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#pragma once

#include <cassert>
#include <cstddef>
#include <span>
#include <string_view>
Expand All @@ -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
{
Expand Down

0 comments on commit db03db0

Please sign in to comment.