diff --git a/src/util/ConstBuffer.hxx b/src/util/ConstBuffer.hxx index 9e780d78b..b5e9082b5 100644 --- a/src/util/ConstBuffer.hxx +++ b/src/util/ConstBuffer.hxx @@ -124,6 +124,16 @@ struct ConstBuffer { return ConstBuffer(nullptr, 0); } + /** + * Cast a ConstBuffer to a ConstBuffer, rounding down + * to the next multiple of T's size. + */ + static constexpr ConstBuffer FromVoidFloor(ConstBuffer other) { + static_assert(sizeof(T) > 0, "Empty base type"); + return ConstBuffer(pointer_type(other.data), + other.size / sizeof(T)); + } + /** * Cast a ConstBuffer to a ConstBuffer. A "void" * buffer records its size in bytes, and when casting to "T", @@ -134,12 +144,10 @@ struct ConstBuffer { constexpr #endif static ConstBuffer FromVoid(ConstBuffer other) { - static_assert(sizeof(T) > 0, "Empty base type"); #ifndef NDEBUG assert(other.size % sizeof(T) == 0); #endif - return ConstBuffer(pointer_type(other.data), - other.size / sizeof(T)); + return FromVoidFloor(other); } constexpr ConstBuffer ToVoid() const { diff --git a/src/util/WritableBuffer.hxx b/src/util/WritableBuffer.hxx index 3155d5503..805f3d9cd 100644 --- a/src/util/WritableBuffer.hxx +++ b/src/util/WritableBuffer.hxx @@ -118,6 +118,16 @@ struct WritableBuffer { return { nullptr, 0 }; } + /** + * Cast a WritableBuffer to a WritableBuffer, + * rounding down to the next multiple of T's size. + */ + static constexpr WritableBuffer FromVoidFloor(WritableBuffer other) { + static_assert(sizeof(T) > 0, "Empty base type"); + return WritableBuffer(pointer_type(other.data), + other.size / sizeof(T)); + } + /** * Cast a WritableBuffer to a WritableBuffer. A "void" * buffer records its size in bytes, and when casting to "T", @@ -128,12 +138,10 @@ struct WritableBuffer { constexpr #endif static WritableBuffer FromVoid(WritableBuffer other) { - static_assert(sizeof(T) > 0, "Empty base type"); #ifndef NDEBUG assert(other.size % sizeof(T) == 0); #endif - return WritableBuffer(pointer_type(other.data), - other.size / sizeof(T)); + return FromVoidFloor(other); } constexpr WritableBuffer ToVoid() const {