buffer to struct type conversion...TArrayStream?

Charles Hixson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 19 10:47:05 PDT 2015


I've read a chunk of data into a buffer and want to convert it into a 
struct.  The reading routine is in a class that doesn't know about the 
struct, but the size should be exactly the same.  (I.e., I want to use 
the converse procedure to write it.)

Is there a better way to do this than using TArrayStream?

The idea here is to have a file of fixed length records (rather like 
Fortran binary records, except that there's a header record which is a 
different size and which specifies various things about the file).  The 
class (well, struct) that handles the fixed length records only knows 
what the record size is, and a couple of other quite general things.  
The class which uses it holds each record in a fixed length struct with 
no indirections.  So I thought I could just cast the buffer to the 
struct...but this doesn't work.  Every straightforward way I've tried of 
doing it yields:
Error: cannot cast from Node_ to ubyte[]
or something reasonably analogous.  The current version of the 
(non-working) code is:
         ubyte    buf[];
         auto    len    =    btFile.read(nodeId, buf);
         assert    (len == n.self.sizeof);


         n.self    =    to!(Node.Node_)(buf);
         //    TODO    write the code
which yields the error:
Error: template instance std.conv.to!(Node_).to!(ubyte[]) error 
instantiating

Node_ is (approximately, I've renamed aliased values to their base value):
         struct    Node_
         {  ulong    idvalue;
             ulong    keyvalue;
             int        eLen;
             Entry   e[23];
         }
and Entry is (approximately):
struct    Entry
{  ulong    key;
     ulong    d;
     ulong    d2;
}



More information about the Digitalmars-d-learn mailing list