sumtype: put private matchImpl helpers together

This commit is contained in:
Paul Backus 2024-11-20 14:41:44 -05:00
parent edf6fb92e4
commit 4c41b433dc

View file

@ -1860,33 +1860,6 @@ private template Iota(size_t n)
assert(Iota!3 == AliasSeq!(0, 1, 2));
}
/* The number that the dim-th argument's tag is multiplied by when
* converting TagTuples to and from case indices ("caseIds").
*
* Named by analogy to the stride that the dim-th index into a
* multidimensional static array is multiplied by to calculate the
* offset of a specific element.
*/
private size_t stride(size_t dim, lengths...)()
{
import core.checkedint : mulu;
size_t result = 1;
bool overflow = false;
static foreach (i; 0 .. dim)
{
result = mulu(result, lengths[i], overflow);
}
/* The largest number matchImpl uses, numCases, is calculated with
* stride!(SumTypes.length), so as long as this overflow check
* passes, we don't need to check for overflow anywhere else.
*/
assert(!overflow, "Integer overflow");
return result;
}
private template matchImpl(Flag!"exhaustive" exhaustive, handlers...)
{
auto ref matchImpl(SumTypes...)(auto ref SumTypes args)
@ -2029,10 +2002,11 @@ private template matchImpl(Flag!"exhaustive" exhaustive, handlers...)
}
}
// Predicate for staticMap
private enum typeCount(SumType) = SumType.Types.length;
/* A TagTuple represents a single possible set of tags that `args`
* could have at runtime.
/* A TagTuple represents a single possible set of tags that the arguments to
* `matchImpl` could have at runtime.
*
* Because D does not allow a struct to be the controlling expression
* of a switch statement, we cannot dispatch on the TagTuple directly.
@ -2104,6 +2078,33 @@ private struct TagTuple(SumTypes...)
}
}
/* The number that the dim-th argument's tag is multiplied by when
* converting TagTuples to and from case indices ("caseIds").
*
* Named by analogy to the stride that the dim-th index into a
* multidimensional static array is multiplied by to calculate the
* offset of a specific element.
*/
private size_t stride(size_t dim, lengths...)()
{
import core.checkedint : mulu;
size_t result = 1;
bool overflow = false;
static foreach (i; 0 .. dim)
{
result = mulu(result, lengths[i], overflow);
}
/* The largest number matchImpl uses, numCases, is calculated with
* stride!(SumTypes.length), so as long as this overflow check
* passes, we don't need to check for overflow anywhere else.
*/
assert(!overflow, "Integer overflow");
return result;
}
// Matching
@safe unittest
{