Enhance auto_encode to take number of struct fields
emit_struct and read_struct takes an additional len:uint parameter which tells us how many fields the struct we are working on has. This is required to implement an Encoder for the msgpack [1] serialization format. To serialize a struct with msgpack you have to use arrays and the size of the array has to be know before each of the elements are written out. JSON as an example doesn't have this problem as it uses '[' and ']' delimiters for arrays. [1]: www.msgpack.org
This commit is contained in:
parent
09bb07bed9
commit
2b6c456bf6
5 changed files with 12 additions and 10 deletions
|
@ -25,7 +25,7 @@ would generate two implementations like:
|
|||
|
||||
impl<S: Encoder> node_id: Encodable<S> {
|
||||
fn encode(s: &S) {
|
||||
do s.emit_struct("Node") {
|
||||
do s.emit_struct("Node", 1) {
|
||||
s.emit_field("id", 0, || s.emit_uint(self))
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ would generate two implementations like:
|
|||
|
||||
impl<D: Decoder> node_id: Decodable {
|
||||
static fn decode(d: &D) -> Node {
|
||||
do d.read_struct("Node") {
|
||||
do d.read_struct("Node", 1) {
|
||||
Node {
|
||||
id: d.read_field(~"x", 0, || decode(d))
|
||||
}
|
||||
|
@ -686,6 +686,7 @@ fn mk_struct_ser_impl(
|
|||
),
|
||||
~[
|
||||
cx.lit_str(span, @cx.str_of(ident)),
|
||||
cx.lit_uint(span, vec::len(fields)),
|
||||
cx.lambda_stmts(span, fields),
|
||||
]
|
||||
);
|
||||
|
@ -712,6 +713,7 @@ fn mk_struct_deser_impl(
|
|||
),
|
||||
~[
|
||||
cx.lit_str(span, @cx.str_of(ident)),
|
||||
cx.lit_uint(span, vec::len(fields)),
|
||||
cx.lambda_expr(
|
||||
cx.expr(
|
||||
span,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue