From 7e3f2cc6118ccc82570a57d33cd41730cb85b7d7 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Thu, 9 Jun 2016 14:10:06 +0200 Subject: [PATCH] use static if for better readybility --- source/cipher.d | 42 +++++++++++++++++++----------------------- source/salsa20.d | 2 +- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/source/cipher.d b/source/cipher.d index 96a73c4..d7195c3 100644 --- a/source/cipher.d +++ b/source/cipher.d @@ -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)); diff --git a/source/salsa20.d b/source/salsa20.d index 200adbf..b19d21d 100644 --- a/source/salsa20.d +++ b/source/salsa20.d @@ -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));