(index<- ) ./libextra/serialize.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Support code for encoding and decoding types.
12
13 /*
14 Core encoding and decoding interfaces.
15 */
16
17 #[allow(missing_doc)];
18 #[forbid(non_camel_case_types)];
19
20
21 use std::at_vec;
22 use std::hashmap::{HashMap, HashSet};
23 use std::trie::{TrieMap, TrieSet};
24 use std::vec;
25 use ringbuf::RingBuf;
26 use container::Deque;
27 use dlist::DList;
28 use treemap::{TreeMap, TreeSet};
29
30 pub trait Encoder {
31 // Primitive types:
32 fn emit_nil(&mut self);
33 fn emit_uint(&mut self, v: uint);
34 fn emit_u64(&mut self, v: u64);
35 fn emit_u32(&mut self, v: u32);
36 fn emit_u16(&mut self, v: u16);
37 fn emit_u8(&mut self, v: u8);
38 fn emit_int(&mut self, v: int);
39 fn emit_i64(&mut self, v: i64);
40 fn emit_i32(&mut self, v: i32);
41 fn emit_i16(&mut self, v: i16);
42 fn emit_i8(&mut self, v: i8);
43 fn emit_bool(&mut self, v: bool);
44 fn emit_float(&mut self, v: float);
45 fn emit_f64(&mut self, v: f64);
46 fn emit_f32(&mut self, v: f32);
47 fn emit_char(&mut self, v: char);
48 fn emit_str(&mut self, v: &str);
49
50 // Compound types:
51 fn emit_enum(&mut self, name: &str, f: &fn(&mut Self));
52
53 fn emit_enum_variant(&mut self,
54 v_name: &str,
55 v_id: uint,
56 len: uint,
57 f: &fn(&mut Self));
58 fn emit_enum_variant_arg(&mut self, a_idx: uint, f: &fn(&mut Self));
59
60 fn emit_enum_struct_variant(&mut self,
61 v_name: &str,
62 v_id: uint,
63 len: uint,
64 f: &fn(&mut Self));
65 fn emit_enum_struct_variant_field(&mut self,
66 f_name: &str,
67 f_idx: uint,
68 f: &fn(&mut Self));
69
70 fn emit_struct(&mut self, name: &str, len: uint, f: &fn(&mut Self));
71 fn emit_struct_field(&mut self,
72 f_name: &str,
73 f_idx: uint,
74 f: &fn(&mut Self));
75
76 fn emit_tuple(&mut self, len: uint, f: &fn(&mut Self));
77 fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut Self));
78
79 fn emit_tuple_struct(&mut self, name: &str, len: uint, f: &fn(&mut Self));
80 fn emit_tuple_struct_arg(&mut self, f_idx: uint, f: &fn(&mut Self));
81
82 // Specialized types:
83 fn emit_option(&mut self, f: &fn(&mut Self));
84 fn emit_option_none(&mut self);
85 fn emit_option_some(&mut self, f: &fn(&mut Self));
86
87 fn emit_seq(&mut self, len: uint, f: &fn(this: &mut Self));
88 fn emit_seq_elt(&mut self, idx: uint, f: &fn(this: &mut Self));
89
90 fn emit_map(&mut self, len: uint, f: &fn(&mut Self));
91 fn emit_map_elt_key(&mut self, idx: uint, f: &fn(&mut Self));
92 fn emit_map_elt_val(&mut self, idx: uint, f: &fn(&mut Self));
93 }
94
95 pub trait Decoder {
96 // Primitive types:
97 fn read_nil(&mut self) -> ();
98 fn read_uint(&mut self) -> uint;
99 fn read_u64(&mut self) -> u64;
100 fn read_u32(&mut self) -> u32;
101 fn read_u16(&mut self) -> u16;
102 fn read_u8(&mut self) -> u8;
103 fn read_int(&mut self) -> int;
104 fn read_i64(&mut self) -> i64;
105 fn read_i32(&mut self) -> i32;
106 fn read_i16(&mut self) -> i16;
107 fn read_i8(&mut self) -> i8;
108 fn read_bool(&mut self) -> bool;
109 fn read_f64(&mut self) -> f64;
110 fn read_f32(&mut self) -> f32;
111 fn read_float(&mut self) -> float;
112 fn read_char(&mut self) -> char;
113 fn read_str(&mut self) -> ~str;
114
115 // Compound types:
116 fn read_enum<T>(&mut self, name: &str, f: &fn(&mut Self) -> T) -> T;
117
118 fn read_enum_variant<T>(&mut self,
119 names: &[&str],
120 f: &fn(&mut Self, uint) -> T)
121 -> T;
122 fn read_enum_variant_arg<T>(&mut self,
123 a_idx: uint,
124 f: &fn(&mut Self) -> T)
125 -> T;
126
127 fn read_enum_struct_variant<T>(&mut self,
128 names: &[&str],
129 f: &fn(&mut Self, uint) -> T)
130 -> T;
131 fn read_enum_struct_variant_field<T>(&mut self,
132 &f_name: &str,
133 f_idx: uint,
134 f: &fn(&mut Self) -> T)
135 -> T;
136
137 fn read_struct<T>(&mut self,
138 s_name: &str,
139 len: uint,
140 f: &fn(&mut Self) -> T)
141 -> T;
142 fn read_struct_field<T>(&mut self,
143 f_name: &str,
144 f_idx: uint,
145 f: &fn(&mut Self) -> T)
146 -> T;
147
148 fn read_tuple<T>(&mut self, f: &fn(&mut Self, uint) -> T) -> T;
149 fn read_tuple_arg<T>(&mut self, a_idx: uint, f: &fn(&mut Self) -> T) -> T;
150
151 fn read_tuple_struct<T>(&mut self,
152 s_name: &str,
153 f: &fn(&mut Self, uint) -> T)
154 -> T;
155 fn read_tuple_struct_arg<T>(&mut self,
156 a_idx: uint,
157 f: &fn(&mut Self) -> T)
158 -> T;
159
160 // Specialized types:
161 fn read_option<T>(&mut self, f: &fn(&mut Self, bool) -> T) -> T;
162
163 fn read_seq<T>(&mut self, f: &fn(&mut Self, uint) -> T) -> T;
164 fn read_seq_elt<T>(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T;
165
166 fn read_map<T>(&mut self, f: &fn(&mut Self, uint) -> T) -> T;
167 fn read_map_elt_key<T>(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T;
168 fn read_map_elt_val<T>(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T;
169 }
170
171 pub trait Encodable<S:Encoder> {
172 fn encode(&self, s: &mut S);
173 }
174
175 pub trait Decodable<D:Decoder> {
176 fn decode(d: &mut D) -> Self;
177 }
178
179 impl<S:Encoder> Encodable<S> for uint {
180 fn encode(&self, s: &mut S) {
181 s.emit_uint(*self)
182 }
183 }
184
185 impl<D:Decoder> Decodable<D> for uint {
186 fn decode(d: &mut D) -> uint {
187 d.read_uint()
188 }
189 }
190
191 impl<S:Encoder> Encodable<S> for u8 {
192 fn encode(&self, s: &mut S) {
193 s.emit_u8(*self)
194 }
195 }
196
197 impl<D:Decoder> Decodable<D> for u8 {
198 fn decode(d: &mut D) -> u8 {
199 d.read_u8()
200 }
201 }
202
203 impl<S:Encoder> Encodable<S> for u16 {
204 fn encode(&self, s: &mut S) {
205 s.emit_u16(*self)
206 }
207 }
208
209 impl<D:Decoder> Decodable<D> for u16 {
210 fn decode(d: &mut D) -> u16 {
211 d.read_u16()
212 }
213 }
214
215 impl<S:Encoder> Encodable<S> for u32 {
216 fn encode(&self, s: &mut S) {
217 s.emit_u32(*self)
218 }
219 }
220
221 impl<D:Decoder> Decodable<D> for u32 {
222 fn decode(d: &mut D) -> u32 {
223 d.read_u32()
224 }
225 }
226
227 impl<S:Encoder> Encodable<S> for u64 {
228 fn encode(&self, s: &mut S) {
229 s.emit_u64(*self)
230 }
231 }
232
233 impl<D:Decoder> Decodable<D> for u64 {
234 fn decode(d: &mut D) -> u64 {
235 d.read_u64()
236 }
237 }
238
239 impl<S:Encoder> Encodable<S> for int {
240 fn encode(&self, s: &mut S) {
241 s.emit_int(*self)
242 }
243 }
244
245 impl<D:Decoder> Decodable<D> for int {
246 fn decode(d: &mut D) -> int {
247 d.read_int()
248 }
249 }
250
251 impl<S:Encoder> Encodable<S> for i8 {
252 fn encode(&self, s: &mut S) {
253 s.emit_i8(*self)
254 }
255 }
256
257 impl<D:Decoder> Decodable<D> for i8 {
258 fn decode(d: &mut D) -> i8 {
259 d.read_i8()
260 }
261 }
262
263 impl<S:Encoder> Encodable<S> for i16 {
264 fn encode(&self, s: &mut S) {
265 s.emit_i16(*self)
266 }
267 }
268
269 impl<D:Decoder> Decodable<D> for i16 {
270 fn decode(d: &mut D) -> i16 {
271 d.read_i16()
272 }
273 }
274
275 impl<S:Encoder> Encodable<S> for i32 {
276 fn encode(&self, s: &mut S) {
277 s.emit_i32(*self)
278 }
279 }
280
281 impl<D:Decoder> Decodable<D> for i32 {
282 fn decode(d: &mut D) -> i32 {
283 d.read_i32()
284 }
285 }
286
287 impl<S:Encoder> Encodable<S> for i64 {
288 fn encode(&self, s: &mut S) {
289 s.emit_i64(*self)
290 }
291 }
292
293 impl<D:Decoder> Decodable<D> for i64 {
294 fn decode(d: &mut D) -> i64 {
295 d.read_i64()
296 }
297 }
298
299 impl<'self, S:Encoder> Encodable<S> for &'self str {
300 fn encode(&self, s: &mut S) {
301 s.emit_str(*self)
302 }
303 }
304
305 impl<S:Encoder> Encodable<S> for ~str {
306 fn encode(&self, s: &mut S) {
307 s.emit_str(*self)
308 }
309 }
310
311 impl<D:Decoder> Decodable<D> for ~str {
312 fn decode(d: &mut D) -> ~str {
313 d.read_str()
314 }
315 }
316
317 impl<S:Encoder> Encodable<S> for @str {
318 fn encode(&self, s: &mut S) {
319 s.emit_str(*self)
320 }
321 }
322
323 impl<D:Decoder> Decodable<D> for @str {
324 fn decode(d: &mut D) -> @str {
325 d.read_str().to_managed()
326 }
327 }
328
329 impl<S:Encoder> Encodable<S> for float {
330 fn encode(&self, s: &mut S) {
331 s.emit_float(*self)
332 }
333 }
334
335 impl<D:Decoder> Decodable<D> for float {
336 fn decode(d: &mut D) -> float {
337 d.read_float()
338 }
339 }
340
341 impl<S:Encoder> Encodable<S> for f32 {
342 fn encode(&self, s: &mut S) {
343 s.emit_f32(*self)
344 }
345 }
346
347 impl<D:Decoder> Decodable<D> for f32 {
348 fn decode(d: &mut D) -> f32 {
349 d.read_f32()
350 }
351 }
352
353 impl<S:Encoder> Encodable<S> for f64 {
354 fn encode(&self, s: &mut S) {
355 s.emit_f64(*self)
356 }
357 }
358
359 impl<D:Decoder> Decodable<D> for f64 {
360 fn decode(d: &mut D) -> f64 {
361 d.read_f64()
362 }
363 }
364
365 impl<S:Encoder> Encodable<S> for bool {
366 fn encode(&self, s: &mut S) {
367 s.emit_bool(*self)
368 }
369 }
370
371 impl<D:Decoder> Decodable<D> for bool {
372 fn decode(d: &mut D) -> bool {
373 d.read_bool()
374 }
375 }
376
377 impl<S:Encoder> Encodable<S> for char {
378 fn encode(&self, s: &mut S) {
379 s.emit_char(*self)
380 }
381 }
382
383 impl<D:Decoder> Decodable<D> for char {
384 fn decode(d: &mut D) -> char {
385 d.read_char()
386 }
387 }
388
389 impl<S:Encoder> Encodable<S> for () {
390 fn encode(&self, s: &mut S) {
391 s.emit_nil()
392 }
393 }
394
395 impl<D:Decoder> Decodable<D> for () {
396 fn decode(d: &mut D) -> () {
397 d.read_nil()
398 }
399 }
400
401 impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self T {
402 fn encode(&self, s: &mut S) {
403 (**self).encode(s)
404 }
405 }
406
407 impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~T {
408 fn encode(&self, s: &mut S) {
409 (**self).encode(s)
410 }
411 }
412
413 impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~T {
414 fn decode(d: &mut D) -> ~T {
415 ~Decodable::decode(d)
416 }
417 }
418
419 impl<S:Encoder,T:Encodable<S>> Encodable<S> for @T {
420 fn encode(&self, s: &mut S) {
421 (**self).encode(s)
422 }
423 }
424
425 impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @T {
426 fn decode(d: &mut D) -> @T {
427 @Decodable::decode(d)
428 }
429 }
430
431 impl<S:Encoder,T:Encodable<S>> Encodable<S> for @mut T {
432 fn encode(&self, s: &mut S) {
433 (**self).encode(s)
434 }
435 }
436
437 impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @mut T {
438 fn decode(d: &mut D) -> @mut T {
439 @mut Decodable::decode(d)
440 }
441 }
442
443 impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self [T] {
444 fn encode(&self, s: &mut S) {
445 do s.emit_seq(self.len()) |s| {
446 for (i, e) in self.iter().enumerate() {
447 s.emit_seq_elt(i, |s| e.encode(s))
448 }
449 }
450 }
451 }
452
453 impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~[T] {
454 fn encode(&self, s: &mut S) {
455 do s.emit_seq(self.len()) |s| {
456 for (i, e) in self.iter().enumerate() {
457 s.emit_seq_elt(i, |s| e.encode(s))
458 }
459 }
460 }
461 }
462
463 impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~[T] {
464 fn decode(d: &mut D) -> ~[T] {
465 do d.read_seq |d, len| {
466 do vec::from_fn(len) |i| {
467 d.read_seq_elt(i, |d| Decodable::decode(d))
468 }
469 }
470 }
471 }
472
473 impl<S:Encoder,T:Encodable<S>> Encodable<S> for @[T] {
474 fn encode(&self, s: &mut S) {
475 do s.emit_seq(self.len()) |s| {
476 for (i, e) in self.iter().enumerate() {
477 s.emit_seq_elt(i, |s| e.encode(s))
478 }
479 }
480 }
481 }
482
483 impl<D:Decoder,T:Decodable<D>> Decodable<D> for @[T] {
484 fn decode(d: &mut D) -> @[T] {
485 do d.read_seq |d, len| {
486 do at_vec::from_fn(len) |i| {
487 d.read_seq_elt(i, |d| Decodable::decode(d))
488 }
489 }
490 }
491 }
492
493 impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> {
494 fn encode(&self, s: &mut S) {
495 do s.emit_option |s| {
496 match *self {
497 None => s.emit_option_none(),
498 Some(ref v) => s.emit_option_some(|s| v.encode(s)),
499 }
500 }
501 }
502 }
503
504 impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> {
505 fn decode(d: &mut D) -> Option<T> {
506 do d.read_option |d, b| {
507 if b {
508 Some(Decodable::decode(d))
509 } else {
510 None
511 }
512 }
513 }
514 }
515
516 impl<S:Encoder,T0:Encodable<S>,T1:Encodable<S>> Encodable<S> for (T0, T1) {
517 fn encode(&self, s: &mut S) {
518 match *self {
519 (ref t0, ref t1) => {
520 do s.emit_seq(2) |s| {
521 s.emit_seq_elt(0, |s| t0.encode(s));
522 s.emit_seq_elt(1, |s| t1.encode(s));
523 }
524 }
525 }
526 }
527 }
528
529 impl<D:Decoder,T0:Decodable<D>,T1:Decodable<D>> Decodable<D> for (T0, T1) {
530 fn decode(d: &mut D) -> (T0, T1) {
531 do d.read_seq |d, len| {
532 assert_eq!(len, 2);
533 (
534 d.read_seq_elt(0, |d| Decodable::decode(d)),
535 d.read_seq_elt(1, |d| Decodable::decode(d))
536 )
537 }
538 }
539 }
540
541 impl<
542 S: Encoder,
543 T0: Encodable<S>,
544 T1: Encodable<S>,
545 T2: Encodable<S>
546 > Encodable<S> for (T0, T1, T2) {
547 fn encode(&self, s: &mut S) {
548 match *self {
549 (ref t0, ref t1, ref t2) => {
550 do s.emit_seq(3) |s| {
551 s.emit_seq_elt(0, |s| t0.encode(s));
552 s.emit_seq_elt(1, |s| t1.encode(s));
553 s.emit_seq_elt(2, |s| t2.encode(s));
554 }
555 }
556 }
557 }
558 }
559
560 impl<
561 D: Decoder,
562 T0: Decodable<D>,
563 T1: Decodable<D>,
564 T2: Decodable<D>
565 > Decodable<D> for (T0, T1, T2) {
566 fn decode(d: &mut D) -> (T0, T1, T2) {
567 do d.read_seq |d, len| {
568 assert_eq!(len, 3);
569 (
570 d.read_seq_elt(0, |d| Decodable::decode(d)),
571 d.read_seq_elt(1, |d| Decodable::decode(d)),
572 d.read_seq_elt(2, |d| Decodable::decode(d))
573 )
574 }
575 }
576 }
577
578 impl<
579 S: Encoder,
580 T0: Encodable<S>,
581 T1: Encodable<S>,
582 T2: Encodable<S>,
583 T3: Encodable<S>
584 > Encodable<S> for (T0, T1, T2, T3) {
585 fn encode(&self, s: &mut S) {
586 match *self {
587 (ref t0, ref t1, ref t2, ref t3) => {
588 do s.emit_seq(4) |s| {
589 s.emit_seq_elt(0, |s| t0.encode(s));
590 s.emit_seq_elt(1, |s| t1.encode(s));
591 s.emit_seq_elt(2, |s| t2.encode(s));
592 s.emit_seq_elt(3, |s| t3.encode(s));
593 }
594 }
595 }
596 }
597 }
598
599 impl<
600 D: Decoder,
601 T0: Decodable<D>,
602 T1: Decodable<D>,
603 T2: Decodable<D>,
604 T3: Decodable<D>
605 > Decodable<D> for (T0, T1, T2, T3) {
606 fn decode(d: &mut D) -> (T0, T1, T2, T3) {
607 do d.read_seq |d, len| {
608 assert_eq!(len, 4);
609 (
610 d.read_seq_elt(0, |d| Decodable::decode(d)),
611 d.read_seq_elt(1, |d| Decodable::decode(d)),
612 d.read_seq_elt(2, |d| Decodable::decode(d)),
613 d.read_seq_elt(3, |d| Decodable::decode(d))
614 )
615 }
616 }
617 }
618
619 impl<
620 S: Encoder,
621 T0: Encodable<S>,
622 T1: Encodable<S>,
623 T2: Encodable<S>,
624 T3: Encodable<S>,
625 T4: Encodable<S>
626 > Encodable<S> for (T0, T1, T2, T3, T4) {
627 fn encode(&self, s: &mut S) {
628 match *self {
629 (ref t0, ref t1, ref t2, ref t3, ref t4) => {
630 do s.emit_seq(5) |s| {
631 s.emit_seq_elt(0, |s| t0.encode(s));
632 s.emit_seq_elt(1, |s| t1.encode(s));
633 s.emit_seq_elt(2, |s| t2.encode(s));
634 s.emit_seq_elt(3, |s| t3.encode(s));
635 s.emit_seq_elt(4, |s| t4.encode(s));
636 }
637 }
638 }
639 }
640 }
641
642 impl<
643 D: Decoder,
644 T0: Decodable<D>,
645 T1: Decodable<D>,
646 T2: Decodable<D>,
647 T3: Decodable<D>,
648 T4: Decodable<D>
649 > Decodable<D> for (T0, T1, T2, T3, T4) {
650 fn decode(d: &mut D) -> (T0, T1, T2, T3, T4) {
651 do d.read_seq |d, len| {
652 assert_eq!(len, 5);
653 (
654 d.read_seq_elt(0, |d| Decodable::decode(d)),
655 d.read_seq_elt(1, |d| Decodable::decode(d)),
656 d.read_seq_elt(2, |d| Decodable::decode(d)),
657 d.read_seq_elt(3, |d| Decodable::decode(d)),
658 d.read_seq_elt(4, |d| Decodable::decode(d))
659 )
660 }
661 }
662 }
663
664 impl<
665 S: Encoder,
666 T: Encodable<S>
667 > Encodable<S> for DList<T> {
668 fn encode(&self, s: &mut S) {
669 do s.emit_seq(self.len()) |s| {
670 for (i, e) in self.iter().enumerate() {
671 s.emit_seq_elt(i, |s| e.encode(s));
672 }
673 }
674 }
675 }
676
677 impl<D:Decoder,T:Decodable<D>> Decodable<D> for DList<T> {
678 fn decode(d: &mut D) -> DList<T> {
679 let mut list = DList::new();
680 do d.read_seq |d, len| {
681 for i in range(0u, len) {
682 list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d)));
683 }
684 }
685 list
686 }
687 }
688
689 impl<
690 S: Encoder,
691 T: Encodable<S>
692 > Encodable<S> for RingBuf<T> {
693 fn encode(&self, s: &mut S) {
694 do s.emit_seq(self.len()) |s| {
695 for (i, e) in self.iter().enumerate() {
696 s.emit_seq_elt(i, |s| e.encode(s));
697 }
698 }
699 }
700 }
701
702 impl<D:Decoder,T:Decodable<D>> Decodable<D> for RingBuf<T> {
703 fn decode(d: &mut D) -> RingBuf<T> {
704 let mut deque = RingBuf::new();
705 do d.read_seq |d, len| {
706 for i in range(0u, len) {
707 deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d)));
708 }
709 }
710 deque
711 }
712 }
713
714 impl<
715 E: Encoder,
716 K: Encodable<E> + Hash + IterBytes + Eq,
717 V: Encodable<E>
718 > Encodable<E> for HashMap<K, V> {
719 fn encode(&self, e: &mut E) {
720 do e.emit_map(self.len()) |e| {
721 let mut i = 0;
722 for (key, val) in self.iter() {
723 e.emit_map_elt_key(i, |e| key.encode(e));
724 e.emit_map_elt_val(i, |e| val.encode(e));
725 i += 1;
726 }
727 }
728 }
729 }
730
731 impl<
732 D: Decoder,
733 K: Decodable<D> + Hash + IterBytes + Eq,
734 V: Decodable<D>
735 > Decodable<D> for HashMap<K, V> {
736 fn decode(d: &mut D) -> HashMap<K, V> {
737 do d.read_map |d, len| {
738 let mut map = HashMap::with_capacity(len);
739 for i in range(0u, len) {
740 let key = d.read_map_elt_key(i, |d| Decodable::decode(d));
741 let val = d.read_map_elt_val(i, |d| Decodable::decode(d));
742 map.insert(key, val);
743 }
744 map
745 }
746 }
747 }
748
749 impl<
750 S: Encoder,
751 T: Encodable<S> + Hash + IterBytes + Eq
752 > Encodable<S> for HashSet<T> {
753 fn encode(&self, s: &mut S) {
754 do s.emit_seq(self.len()) |s| {
755 let mut i = 0;
756 for e in self.iter() {
757 s.emit_seq_elt(i, |s| e.encode(s));
758 i += 1;
759 }
760 }
761 }
762 }
763
764 impl<
765 D: Decoder,
766 T: Decodable<D> + Hash + IterBytes + Eq
767 > Decodable<D> for HashSet<T> {
768 fn decode(d: &mut D) -> HashSet<T> {
769 do d.read_seq |d, len| {
770 let mut set = HashSet::with_capacity(len);
771 for i in range(0u, len) {
772 set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
773 }
774 set
775 }
776 }
777 }
778
779 impl<
780 E: Encoder,
781 V: Encodable<E>
782 > Encodable<E> for TrieMap<V> {
783 fn encode(&self, e: &mut E) {
784 do e.emit_map(self.len()) |e| {
785 let mut i = 0;
786 do self.each |key, val| {
787 e.emit_map_elt_key(i, |e| key.encode(e));
788 e.emit_map_elt_val(i, |e| val.encode(e));
789 i += 1;
790 true
791 };
792 }
793 }
794 }
795
796 impl<
797 D: Decoder,
798 V: Decodable<D>
799 > Decodable<D> for TrieMap<V> {
800 fn decode(d: &mut D) -> TrieMap<V> {
801 do d.read_map |d, len| {
802 let mut map = TrieMap::new();
803 for i in range(0u, len) {
804 let key = d.read_map_elt_key(i, |d| Decodable::decode(d));
805 let val = d.read_map_elt_val(i, |d| Decodable::decode(d));
806 map.insert(key, val);
807 }
808 map
809 }
810 }
811 }
812
813 impl<S: Encoder> Encodable<S> for TrieSet {
814 fn encode(&self, s: &mut S) {
815 do s.emit_seq(self.len()) |s| {
816 let mut i = 0;
817 do self.each |e| {
818 s.emit_seq_elt(i, |s| e.encode(s));
819 i += 1;
820 true
821 };
822 }
823 }
824 }
825
826 impl<D: Decoder> Decodable<D> for TrieSet {
827 fn decode(d: &mut D) -> TrieSet {
828 do d.read_seq |d, len| {
829 let mut set = TrieSet::new();
830 for i in range(0u, len) {
831 set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
832 }
833 set
834 }
835 }
836 }
837
838 impl<
839 E: Encoder,
840 K: Encodable<E> + Eq + TotalOrd,
841 V: Encodable<E> + Eq
842 > Encodable<E> for TreeMap<K, V> {
843 fn encode(&self, e: &mut E) {
844 do e.emit_map(self.len()) |e| {
845 let mut i = 0;
846 for (key, val) in self.iter() {
847 e.emit_map_elt_key(i, |e| key.encode(e));
848 e.emit_map_elt_val(i, |e| val.encode(e));
849 i += 1;
850 }
851 }
852 }
853 }
854
855 impl<
856 D: Decoder,
857 K: Decodable<D> + Eq + TotalOrd,
858 V: Decodable<D> + Eq
859 > Decodable<D> for TreeMap<K, V> {
860 fn decode(d: &mut D) -> TreeMap<K, V> {
861 do d.read_map |d, len| {
862 let mut map = TreeMap::new();
863 for i in range(0u, len) {
864 let key = d.read_map_elt_key(i, |d| Decodable::decode(d));
865 let val = d.read_map_elt_val(i, |d| Decodable::decode(d));
866 map.insert(key, val);
867 }
868 map
869 }
870 }
871 }
872
873 impl<
874 S: Encoder,
875 T: Encodable<S> + Eq + TotalOrd
876 > Encodable<S> for TreeSet<T> {
877 fn encode(&self, s: &mut S) {
878 do s.emit_seq(self.len()) |s| {
879 let mut i = 0;
880 for e in self.iter() {
881 s.emit_seq_elt(i, |s| e.encode(s));
882 i += 1;
883 }
884 }
885 }
886 }
887
888 impl<
889 D: Decoder,
890 T: Decodable<D> + Eq + TotalOrd
891 > Decodable<D> for TreeSet<T> {
892 fn decode(d: &mut D) -> TreeSet<T> {
893 do d.read_seq |d, len| {
894 let mut set = TreeSet::new();
895 for i in range(0u, len) {
896 set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
897 }
898 set
899 }
900 }
901 }
902
903 // ___________________________________________________________________________
904 // Helper routines
905 //
906 // In some cases, these should eventually be coded as traits.
907
908 pub trait EncoderHelpers {
909 fn emit_from_vec<T>(&mut self, v: &[T], f: &fn(&mut Self, v: &T));
910 }
911
912 impl<S:Encoder> EncoderHelpers for S {
913 fn emit_from_vec<T>(&mut self, v: &[T], f: &fn(&mut S, &T)) {
914 do self.emit_seq(v.len()) |this| {
915 for (i, e) in v.iter().enumerate() {
916 do this.emit_seq_elt(i) |this| {
917 f(this, e)
918 }
919 }
920 }
921 }
922 }
923
924 pub trait DecoderHelpers {
925 fn read_to_vec<T>(&mut self, f: &fn(&mut Self) -> T) -> ~[T];
926 }
927
928 impl<D:Decoder> DecoderHelpers for D {
929 fn read_to_vec<T>(&mut self, f: &fn(&mut D) -> T) -> ~[T] {
930 do self.read_seq |this, len| {
931 do vec::from_fn(len) |i| {
932 this.read_seq_elt(i, |this| f(this))
933 }
934 }
935 }
936 }
libextra/serialize.rs:29:1-29:1 -trait- definition:
pub trait Encoder {
references:-90: fn emit_map(&mut self, len: uint, f: &fn(&mut Self));
171: pub trait Encodable<S:Encoder> {
70: fn emit_struct(&mut self, name: &str, len: uint, f: &fn(&mut Self));
407: impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~T {
239: impl<S:Encoder> Encodable<S> for int {
443: impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self [T] {
389: impl<S:Encoder> Encodable<S> for () {
431: impl<S:Encoder,T:Encodable<S>> Encodable<S> for @mut T {
76: fn emit_tuple(&mut self, len: uint, f: &fn(&mut Self));
227: impl<S:Encoder> Encodable<S> for u64 {
64: f: &fn(&mut Self));
92: fn emit_map_elt_val(&mut self, idx: uint, f: &fn(&mut Self));
419: impl<S:Encoder,T:Encodable<S>> Encodable<S> for @T {
377: impl<S:Encoder> Encodable<S> for char {
58: fn emit_enum_variant_arg(&mut self, a_idx: uint, f: &fn(&mut Self));
251: impl<S:Encoder> Encodable<S> for i8 {
77: fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut Self));
191: impl<S:Encoder> Encodable<S> for u8 {
85: fn emit_option_some(&mut self, f: &fn(&mut Self));
750: S: Encoder,
305: impl<S:Encoder> Encodable<S> for ~str {
329: impl<S:Encoder> Encodable<S> for float {
203: impl<S:Encoder> Encodable<S> for u16 {
83: fn emit_option(&mut self, f: &fn(&mut Self));
317: impl<S:Encoder> Encodable<S> for @str {
341: impl<S:Encoder> Encodable<S> for f32 {
179: impl<S:Encoder> Encodable<S> for uint {
263: impl<S:Encoder> Encodable<S> for i16 {
215: impl<S:Encoder> Encodable<S> for u32 {
665: S: Encoder,
(79)(473)(620)(579)(493)(780)(51)(542)(874)(80)(353)(87)(91)(287)(690)(401)(715)(365)(57)(68)(74)(839)(453)(912)(275)(813)(516)(299)(88)libextra/flatpipes.rs:
(431)(415)(456)libextra/ebml.rs:
(794)libextra/json.rs:
(453)(99)(260)libextra/time.rs:
(107)(35)libextra/workcache.rs:
(112)(92)(109)libextra/test.rs:
(96)libextra/serialize.rs:170:1-170:1 -trait- definition:
pub trait Encodable<S:Encoder> {
references:-717: V: Encodable<E>
251: impl<S:Encoder> Encodable<S> for i8 {
841: V: Encodable<E> + Eq
431: impl<S:Encoder,T:Encodable<S>> Encodable<S> for @mut T {
718: > Encodable<E> for HashMap<K, V> {
582: T2: Encodable<S>,
341: impl<S:Encoder> Encodable<S> for f32 {
751: T: Encodable<S> + Hash + IterBytes + Eq
443: impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self [T] {
544: T1: Encodable<S>,
583: T3: Encodable<S>
431: impl<S:Encoder,T:Encodable<S>> Encodable<S> for @mut T {
407: impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~T {
299: impl<'self, S:Encoder> Encodable<S> for &'self str {
781: V: Encodable<E>
813: impl<S: Encoder> Encodable<S> for TrieSet {
203: impl<S:Encoder> Encodable<S> for u16 {
691: T: Encodable<S>
239: impl<S:Encoder> Encodable<S> for int {
493: impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> {
377: impl<S:Encoder> Encodable<S> for char {
305: impl<S:Encoder> Encodable<S> for ~str {
389: impl<S:Encoder> Encodable<S> for () {
191: impl<S:Encoder> Encodable<S> for u8 {
227: impl<S:Encoder> Encodable<S> for u64 {
580: T0: Encodable<S>,
621: T0: Encodable<S>,
287: impl<S:Encoder> Encodable<S> for i64 {
624: T3: Encodable<S>,
401: impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self T {
(692)(666)(752)(876)(545)(353)(419)(317)(329)(473)(493)(840)(453)(667)(584)(275)(263)(516)(782)(473)(401)(581)(516)(625)(453)(875)(407)(716)(626)(546)(622)(365)(543)(179)(842)(623)(215)(443)(419)(516)libextra/flatpipes.rs:
(153)(142)(415)(121)(457)(431)libextra/json.rs:
(453)libextra/time.rs:
(107)(35)libextra/workcache.rs:
(92)(275)(112)(416)(409)(461)(258)(109)libextra/test.rs:
(96)libextra/serialize.rs:174:1-174:1 -trait- definition:
pub trait Decodable<D:Decoder> {
references:-733: K: Decodable<D> + Hash + IterBytes + Eq,
798: V: Decodable<D>
857: K: Decodable<D> + Eq + TotalOrd,
767: > Decodable<D> for HashSet<T> {
601: T0: Decodable<D>,
649: > Decodable<D> for (T0, T1, T2, T3, T4) {
766: T: Decodable<D> + Hash + IterBytes + Eq
257: impl<D:Decoder> Decodable<D> for i8 {
176: fn decode(d: &mut D) -> Self;
644: T0: Decodable<D>,
677: impl<D:Decoder,T:Decodable<D>> Decodable<D> for DList<T> {
858: V: Decodable<D> + Eq
463: impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~[T] {
425: impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @T {
891: > Decodable<D> for TreeSet<T> {
323: impl<D:Decoder> Decodable<D> for @str {
859: > Decodable<D> for TreeMap<K, V> {
645: T1: Decodable<D>,
562: T0: Decodable<D>,
799: > Decodable<D> for TrieMap<V> {
413: impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~T {
702: impl<D:Decoder,T:Decodable<D>> Decodable<D> for RingBuf<T> {
335: impl<D:Decoder> Decodable<D> for float {
735: > Decodable<D> for HashMap<K, V> {
702: impl<D:Decoder,T:Decodable<D>> Decodable<D> for RingBuf<T> {
209: impl<D:Decoder> Decodable<D> for u16 {
437: impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @mut T {
197: impl<D:Decoder> Decodable<D> for u8 {
504: impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> {
185: impl<D:Decoder> Decodable<D> for uint {
(269)(605)(347)(383)(504)(604)(648)(483)(395)(221)(602)(564)(529)(647)(890)(311)(483)(603)(293)(437)(413)(359)(529)(826)(425)(563)(734)(529)(371)(245)(281)(233)(646)(463)(565)(677)libextra/flatpipes.rs:
(408)(446)(131)(111)(422)(154)libextra/time.rs:
(107)(35)libextra/workcache.rs:
(410)(417)(462)(266)(109)(112)(92)libextra/test.rs:
(96)libextra/serialize.rs:94:1-94:1 -trait- definition:
pub trait Decoder {
references:-425: impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @T {
209: impl<D:Decoder> Decodable<D> for u16 {
483: impl<D:Decoder,T:Decodable<D>> Decodable<D> for @[T] {
413: impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~T {
889: D: Decoder,
395: impl<D:Decoder> Decodable<D> for () {
161: fn read_option<T>(&mut self, f: &fn(&mut Self, bool) -> T) -> T;
293: impl<D:Decoder> Decodable<D> for i64 {
702: impl<D:Decoder,T:Decodable<D>> Decodable<D> for RingBuf<T> {
732: D: Decoder,
561: D: Decoder,
164: fn read_seq_elt<T>(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T;
677: impl<D:Decoder,T:Decodable<D>> Decodable<D> for DList<T> {
323: impl<D:Decoder> Decodable<D> for @str {
928: impl<D:Decoder> DecoderHelpers for D {
281: impl<D:Decoder> Decodable<D> for i32 {
245: impl<D:Decoder> Decodable<D> for int {
335: impl<D:Decoder> Decodable<D> for float {
359: impl<D:Decoder> Decodable<D> for f64 {
197: impl<D:Decoder> Decodable<D> for u8 {
153: f: &fn(&mut Self, uint) -> T)
168: fn read_map_elt_val<T>(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T;
529: impl<D:Decoder,T0:Decodable<D>,T1:Decodable<D>> Decodable<D> for (T0, T1) {
124: f: &fn(&mut Self) -> T)
185: impl<D:Decoder> Decodable<D> for uint {
437: impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @mut T {
175: pub trait Decodable<D:Decoder> {
157: f: &fn(&mut Self) -> T)
221: impl<D:Decoder> Decodable<D> for u32 {
765: D: Decoder,
(120)(129)(383)(311)(140)(257)(463)(233)(797)(826)(148)(145)(504)(856)(134)(163)(643)(600)(149)(167)(347)(269)(371)(166)(116)libextra/flatpipes.rs:
(445)(422)(408)libextra/ebml.rs:
(376)libextra/json.rs:
(886)libextra/time.rs:
(107)(35)libextra/workcache.rs:
(92)(112)(109)libextra/test.rs:
(96)libextra/serialize.rs:907:1-907:1 -trait- definition:
pub trait EncoderHelpers {
references:-912: impl<S:Encoder> EncoderHelpers for S {
909: fn emit_from_vec<T>(&mut self, v: &[T], f: &fn(&mut Self, v: &T));
libextra/serialize.rs:923:1-923:1 -trait- definition:
pub trait DecoderHelpers {
references:-928: impl<D:Decoder> DecoderHelpers for D {
925: fn read_to_vec<T>(&mut self, f: &fn(&mut Self) -> T) -> ~[T];