On Monday, 3 August 2015 at 09:27:03 UTC, Dicebot wrote:
On Monday, 3 August 2015 at 09:21:50 UTC, Atila Neves wrote:
The summary is you can now write this:
struct UdpPacket {
static struct Header {
ushort srcPort;
ushort dstPort;
ushort length;
ushort checksum;
}
enum headerSize = unalignedSizeof!Header;
alias header this;
Header header;
@LengthInBytes("length - headerSize") ubyte[] data;
}
This deserialization will be identical to casting like this,
right? (Not trying to diminish your work, just making sure I
get semantics :))
align(1)
struct UdpPacket
{
align(1)
static struct Header {
ushort srcPort;
ushort dstPort;
ushort length;
ushort checksum;
}
Header header;
ubyte[0] data;
}
// ...
auto packet = cast(UdpPacket*) raw_data.ptr;
In this case, yes. In the real-life case I was simplifying, it
wasn't a ubyte[] array, it was an array of structs with
non-trivial serialisation that also depended on a previous
deserialised variable. It was more like this:
struct Outer {
static struct Header { ... }
Header header;
@LengthInBytes("length - headerSize") Inner[] array;
}
struct Inner {
static struct Header { ... }
Header header;
@ArrayLength("length") Unit[] units; //actual length of the
array instead of in bytes
}
struct Unit { ... }
So maybe not as useless after all ;)
Atila