I've just made some experiments for a replacement to ADT.struct and
ADT.Struct:
> program struct =
>> ADT.Serialize.SimpleStruct("struct",
>> ADT.Serialize.Int32, "integer",
>> ADT.Serialize.SimpleStruct("substruct",
>> ADT.Serialize.String, "data"
>> ), "sub");
> object o = struct(14);
> indices(o);
(1) Result: ({ /* 5 elements */
"encode",
"decode",
"_integer",
"integer",
"sub"
})
> o->integer;
(2) Result: 14
> o->sub;
(3) Result: substruct()
> o->sub->data = "foobar";
(4) Result: "foobar"
A decent encoding API is easy:
> o->encode();
(5) Result: "\0\0\0\16foobar"
Now to the hard part; a streaming decoder API...
As far as I know, there are two main approaches:
* Pull -- the user sends a data source (eg a file object) to
the object, and it's up to the object to actually
read the data.
* Push -- the user reads the data in segments (typically with
non-blocking I/O), and pushes it into the object.
The Pull API is the easiest to implement, but it has the following
problems:
* Does not support non-blocking I/O.
* Error recovery is complicated (eg if the source is a pipe/socket).
* Extra over-head for the common case where you already have read
the data.
The Push API on the other hand has the following problems:
* How to find where to restart the decoding when more data arrives.
* How to signal that more data is needed/too much data was received.
Suggestions?