-
-
Save crazymonkyyy/14e36a99dd307c5d6e37bdb3ca2524a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std; | |
import core.vararg; | |
struct Foo{ | |
bool b; | |
int[4] bar; | |
float f; | |
string s; | |
} | |
auto ref hardcast(T, S)(auto ref S s) => *(cast(T*)(&s)); | |
struct anytuple(alias F,Extra){ | |
ubyte[] data; | |
TypeInfo[] what; | |
Extra[] extra; | |
void opOpAssign(string op : "~",T...)(T args){ | |
foreach(I,A;args){ | |
what~=typeid(T[I]); | |
data~=A.hardcast!(ubyte[A.sizeof]); | |
extra~=F!T; | |
}} | |
auto get(T)(){ | |
assert(what[0] == typeid(T)); | |
T o; | |
assert(data.length >= T.sizeof); | |
o.hardcast!(ubyte[T.sizeof]) = data[0..T.sizeof]; | |
return o; | |
} | |
void pop(){ | |
data=data[what[0].tsize..$]; | |
what=what[1..$]; | |
extra=extra[1..$]; | |
} | |
auto meta()=>extra[0]; | |
} | |
unittest{ | |
anytuple!(isBasicType,bool) foo; | |
foo~=Foo(true, [1, 2, 3, 4], 13.37); | |
foo~=69; | |
foo.get!Foo.writeln; | |
foo.meta.writeln; | |
foo.pop; | |
foo.get!int.writeln; | |
foo.meta.writeln; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment