use static if for better readybility

This commit is contained in:
Johannes Loher 2016-06-09 14:10:06 +02:00
parent f865e49519
commit 7e3f2cc611
2 changed files with 20 additions and 24 deletions

View file

@ -14,8 +14,25 @@ enum Cipher
chacha20
}
mixin(cipherFunctionString!(InputRange, isOnlyInputRange));
mixin(cipherFunctionString!(ForwardRange, isForwardRange));
template cipherFunction(R)
if(isInputRange!R && is(ElementType!R : ubyte))
{
static if(isForwardRange!R)
alias ReturnType = ForwardRange;
else
alias ReturnType = InputRange;
ReturnType!(ElementType!R) cipherFunction(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher)
{
final switch (cipher)
{
case Cipher.salsa20:
return range.salsa20Cipher(key, nonce).inputRangeObject;
case Cipher.chacha20:
return range.chacha20Cipher(key, nonce).inputRangeObject;
}
}
}
unittest
{
@ -31,24 +48,3 @@ unittest
static assert(isForwardRange!(typeof(b)));
static assert(isInputRange!(typeof(d)) && !(isForwardRange!(typeof(d))));
}
private:
template isOnlyInputRange(R)
{
enum isOnlyInputRange = isInputRange!R && !(isForwardRange!R);
}
enum cipherFunctionString(alias ReturnType, alias Constraint) = q{
%s!(ElementType!R) cipherFunction(R)(R range, ubyte[32] key, ubyte[8] nonce, Cipher cipher)
if (is(ElementType!R : ubyte) && %s!R)
{
final switch (cipher)
{
case Cipher.salsa20:
return range.salsa20Cipher(key, nonce).inputRangeObject;
case Cipher.chacha20:
return range.chacha20Cipher(key, nonce).inputRangeObject;
}
}
}.format(__traits(identifier, ReturnType), __traits(identifier, Constraint));

View file

@ -91,7 +91,7 @@ unittest
enum string rowRound(alias _x00, alias _x01, alias _x02, alias _x03,
alias _x04, alias _x05, alias _x06, alias _x07,
alias _x08, alias _x09, alias _x10, alias _x11,
alias _x12, alias _x13, alias _x14, alias _x15,) = q{
alias _x12, alias _x13, alias _x14, alias _x15) = q{
mixin(quarterRound!(%1$s, %2$s, %3$s, %4$s));
mixin(quarterRound!(%6$s, %7$s, %8$s, %5$s));
mixin(quarterRound!(%11$s, %12$s, %9$s, %10$s));