1 /** 2 A module of utility functions 3 */ 4 module grain.utility; 5 6 import std.typecons : isTuple, tuple; 7 8 /// non-tuple to tuple. tuple to tuple 9 auto toTuple(T)(T t) { 10 static if (isTuple!T) { 11 return t; 12 } else { 13 return tuple(t); 14 } 15 } 16 17 /// single element tuple to element. the other tuple to tuple 18 auto fromTuple(T)(T t) { 19 static if (t.length == 0) { 20 return t[0]; 21 } else { 22 return t; 23 } 24 } 25 26 /// unsafe cast of array (e.g., int[] -> size_t[]) 27 Dst[N] castArray(Dst, Src, size_t N)(Src[N] src) { 28 Dst[N] dst; 29 static foreach (i; 0 .. N) { 30 dst[i] = cast(Dst) src[i]; 31 } 32 return dst; 33 } 34 35 version (LDC) { 36 public import ldc.attributes : optStrategy; 37 } else { 38 /// usage @optStrategy("none") 39 struct optStrategy { 40 string s; 41 } 42 }