Skip to content

Commit 647ee65

Browse files
committed
---
yaml --- r: 14511 b: refs/heads/try c: 8cffc6f h: refs/heads/master i: 14509: 5b933d0 14507: 47a43fa 14503: b08163e 14495: fe15f64 v: v3
1 parent 5d832e7 commit 647ee65

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 324ecb58a7cf6230662d5a20b4abab17d1631957
5+
refs/heads/try: 8cffc6f84c1553fb7075d465f918d2ac335b15b1
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libstd/serialization.rs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Support code for serialization.
77
import list::list;
88
import ebml::writer;
99

10+
// Set to true to generate more debugging in EBML serialization.
11+
// Totally lame approach.
12+
const debug: bool = true;
13+
1014
iface serializer {
1115
// Primitive types:
1216
fn emit_nil();
@@ -86,7 +90,9 @@ enum ebml_serializer_tag {
8690
es_str,
8791
es_f64, es_f32, es_float,
8892
es_enum, es_enum_vid, es_enum_body,
89-
es_vec, es_vec_len, es_vec_elt
93+
es_vec, es_vec_len, es_vec_elt,
94+
95+
es_label // Used only when debugging
9096
}
9197

9298
impl of serializer for ebml::writer {
@@ -98,6 +104,16 @@ impl of serializer for ebml::writer {
98104
self.wr_tagged_u32(t as uint, v as u32);
99105
}
100106

107+
fn _emit_label(label: str) {
108+
// There are various strings that we have access to, such as
109+
// the name of a record field, which do not actually appear in
110+
// the serialized EBML (normally). This is just for
111+
// efficiency. When debugging, though, we can emit such
112+
// labels and then they will be checked by deserializer to
113+
// try and check failures more quickly.
114+
if debug { self.wr_tagged_str(es_label as uint, label) }
115+
}
116+
101117
fn emit_uint(v: uint) { self.wr_tagged_u64(es_uint as uint, v as u64); }
102118
fn emit_u64(v: u64) { self.wr_tagged_u64(es_u64 as uint, v); }
103119
fn emit_u32(v: u32) { self.wr_tagged_u32(es_u32 as uint, v); }
@@ -118,7 +134,8 @@ impl of serializer for ebml::writer {
118134

119135
fn emit_str(v: str) { self.wr_tagged_str(es_str as uint, v) }
120136

121-
fn emit_enum(_name: str, f: fn()) {
137+
fn emit_enum(name: str, f: fn()) {
138+
self._emit_label(name);
122139
self.wr_tag(es_enum as uint, f)
123140
}
124141
fn emit_enum_variant(_v_name: str, v_id: uint, _cnt: uint, f: fn()) {
@@ -138,14 +155,13 @@ impl of serializer for ebml::writer {
138155
self.wr_tag(es_vec_elt as uint, f)
139156
}
140157

141-
fn emit_vec_elt(_idx: uint, f: fn()) {
142-
self.wr_tag(es_vec_elt as uint, f)
143-
}
144-
145158
fn emit_box(f: fn()) { f() }
146159
fn emit_uniq(f: fn()) { f() }
147160
fn emit_rec(f: fn()) { f() }
148-
fn emit_rec_field(_f_name: str, _f_idx: uint, f: fn()) { f() }
161+
fn emit_rec_field(f_name: str, _f_idx: uint, f: fn()) {
162+
self._emit_label(f_name);
163+
f()
164+
}
149165
fn emit_tup(_sz: uint, f: fn()) { f() }
150166
fn emit_tup_elt(_idx: uint, f: fn()) { f() }
151167
}
@@ -158,7 +174,22 @@ fn mk_ebml_deserializer(d: ebml::doc) -> ebml_deserializer {
158174
}
159175

160176
impl of deserializer for ebml_deserializer {
177+
fn _check_label(lbl: str) {
178+
if self.pos < self.parent.end {
179+
let {tag: r_tag, doc: r_doc} =
180+
ebml::doc_at(self.parent.data, self.pos);
181+
if r_tag == (es_label as uint) {
182+
self.pos = r_doc.end;
183+
let str = ebml::doc_as_str(r_doc);
184+
if lbl != str {
185+
fail #fmt["Expected label %s but found %s", lbl, str];
186+
}
187+
}
188+
}
189+
}
190+
161191
fn next_doc(exp_tag: ebml_serializer_tag) -> ebml::doc {
192+
#debug[". next_doc(exp_tag=%?)", exp_tag];
162193
if self.pos >= self.parent.end {
163194
fail "no more documents in current node!";
164195
}
@@ -231,53 +262,68 @@ impl of deserializer for ebml_deserializer {
231262
fn read_str() -> str { ebml::doc_as_str(self.next_doc(es_str)) }
232263

233264
// Compound types:
234-
fn read_enum<T:copy>(_name: str, f: fn() -> T) -> T {
265+
fn read_enum<T:copy>(name: str, f: fn() -> T) -> T {
266+
#debug["read_enum(%s)", name];
267+
self._check_label(name);
235268
self.push_doc(self.next_doc(es_enum), f)
236269
}
237270

238271
fn read_enum_variant<T:copy>(f: fn(uint) -> T) -> T {
272+
#debug["read_enum_variant()"];
239273
let idx = self._next_uint(es_enum_vid);
274+
#debug[" idx=%u", idx];
240275
self.push_doc(self.next_doc(es_enum_body)) {||
241276
f(idx)
242277
}
243278
}
244279

245-
fn read_enum_variant_arg<T:copy>(_idx: uint, f: fn() -> T) -> T {
280+
fn read_enum_variant_arg<T:copy>(idx: uint, f: fn() -> T) -> T {
281+
#debug["read_enum_variant_arg(idx=%u)", idx];
246282
f()
247283
}
248284

249285
fn read_vec<T:copy>(f: fn(uint) -> T) -> T {
286+
#debug["read_vec()"];
250287
self.push_doc(self.next_doc(es_vec)) {||
251288
let len = self._next_uint(es_vec_len);
289+
#debug[" len=%u", len];
252290
f(len)
253291
}
254292
}
255293

256-
fn read_vec_elt<T:copy>(_idx: uint, f: fn() -> T) -> T {
294+
fn read_vec_elt<T:copy>(idx: uint, f: fn() -> T) -> T {
295+
#debug["read_vec_elt(idx=%u)", idx];
257296
self.push_doc(self.next_doc(es_vec_elt), f)
258297
}
259298

260299
fn read_box<T:copy>(f: fn() -> T) -> T {
300+
#debug["read_box()"];
261301
f()
262302
}
263303

264304
fn read_uniq<T:copy>(f: fn() -> T) -> T {
305+
#debug["read_uniq()"];
265306
f()
266307
}
267308

268309
fn read_rec<T:copy>(f: fn() -> T) -> T {
310+
#debug["read_rec()"];
269311
f()
270312
}
271313

272-
fn read_rec_field<T:copy>(_f_name: str, _f_idx: uint, f: fn() -> T) -> T {
314+
fn read_rec_field<T:copy>(f_name: str, f_idx: uint, f: fn() -> T) -> T {
315+
#debug["read_rec_field(%s, idx=%u)", f_name, f_idx];
316+
self._check_label(f_name);
273317
f()
274318
}
275319

276-
fn read_tup<T:copy>(_sz: uint, f: fn() -> T) -> T {
320+
fn read_tup<T:copy>(sz: uint, f: fn() -> T) -> T {
321+
#debug["read_tup(sz=%u)", sz];
277322
f()
278323
}
279324

280-
fn read_tup_elt<T:copy>(_idx: uint, f: fn() -> T) -> T {
325+
fn read_tup_elt<T:copy>(idx: uint, f: fn() -> T) -> T {
326+
#debug["read_tup_elt(idx=%u)", idx];
281327
f()
282328
}
283329
}

0 commit comments

Comments
 (0)