From 2b6c456bf648e5a7b408b550649d409378e39539 Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Thu, 27 Dec 2012 06:16:16 -0600 Subject: 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 --- src/libsyntax/ext/auto_encode.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/libsyntax/ext/auto_encode.rs') diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index 8eb37386364..680fa169afe 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -25,7 +25,7 @@ would generate two implementations like: impl node_id: Encodable { 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 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, -- cgit 1.4.1-3-g733a5