buffer to struct type conversion...TArrayStream?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 19 12:05:50 PDT 2015


On Thursday, 19 March 2015 at 18:42:03 UTC, Marc Schütz wrote:
> 3) Using std.bitmap.peek(), which also supports conversion 
> between big- and little-endian:
>    import std.bitmap;
>    n.self = buf.peek!(Node.Node_, Endian.bigEndian);
>
> (The examples are untested, it's possible you'll need to make 
> some adjustments to make them work.)
>
> You should probably use option 3). It is safe, because it 
> checks that the buffer has the right size, and it also allows 
> you to specify the endian-ness (many file formats have a 
> standardized endian-ness).
>
> While you're at it, you can also try std.bitmanip.read(). It 
> can be applied to any range, so you can probably do something 
> like this (also untested):
>
>     auto input = btFile.byChunk(4096).joiner;
>     while(!input.empty) {
>         auto node = input.read!(Node.Node_, Endian.bigEndian);
>         // use `node`
>     }

Urgh... it seems `peek()` and `read()` only work with numerical 
types :-( Is this intentional? It would be quite useful for 
arbitrary types, IMO, even if care must be taken with regard to 
pointers.


More information about the Digitalmars-d-learn mailing list