@@ -51,32 +51,34 @@ pub enum EbmlEncoderTag {
51
51
EsI16 , // 8
52
52
EsI8 , // 9
53
53
EsBool , // 10
54
- EsStr , // 11
55
- EsF64 , // 12
56
- EsF32 , // 13
57
- EsFloat , // 14
58
- EsEnum , // 15
59
- EsEnumVid , // 16
60
- EsEnumBody , // 17
61
- EsVec , // 18
62
- EsVecLen , // 19
63
- EsVecElt , // 20
54
+ EsChar , // 11
55
+ EsStr , // 12
56
+ EsF64 , // 13
57
+ EsF32 , // 14
58
+ EsFloat , // 15
59
+ EsEnum , // 16
60
+ EsEnumVid , // 17
61
+ EsEnumBody , // 18
62
+ EsVec , // 19
63
+ EsVecLen , // 20
64
+ EsVecElt , // 21
65
+ EsMap , // 22
66
+ EsMapLen , // 23
67
+ EsMapKey , // 24
68
+ EsMapVal , // 25
64
69
65
70
EsOpaque ,
66
71
67
- EsLabel // Used only when debugging
72
+ EsLabel , // Used only when debugging
68
73
}
69
74
// --------------------------------------
70
75
71
76
pub mod reader {
72
- use core :: prelude :: * ;
77
+ use super :: * ;
73
78
74
- use ebml:: { Doc , EbmlEncoderTag , EsBool , EsEnum , EsEnumBody , EsEnumVid } ;
75
- use ebml:: { EsI16 , EsI32 , EsI64 , EsI8 , EsInt } ;
76
- use ebml:: { EsLabel , EsOpaque , EsStr , EsU16 , EsU32 , EsU64 , EsU8 , EsUint } ;
77
- use ebml:: { EsVec , EsVecElt , EsVecLen , TaggedDoc } ;
78
79
use serialize;
79
80
81
+ use core:: prelude:: * ;
80
82
use core:: cast:: transmute;
81
83
use core:: int;
82
84
use core:: io;
@@ -321,12 +323,14 @@ pub mod reader {
321
323
r_doc
322
324
}
323
325
324
- fn push_doc < T > ( & mut self , d : Doc , f : & fn ( ) -> T ) -> T {
326
+ fn push_doc < T > ( & mut self , exp_tag : EbmlEncoderTag ,
327
+ f : & fn ( & mut Decoder ) -> T ) -> T {
328
+ let d = self . next_doc ( exp_tag) ;
325
329
let old_parent = self . parent ;
326
330
let old_pos = self . pos ;
327
331
self . parent = d;
328
332
self . pos = d. start ;
329
- let r = f ( ) ;
333
+ let r = f ( self ) ;
330
334
self . parent = old_parent;
331
335
self . pos = old_pos;
332
336
r
@@ -395,10 +399,21 @@ pub mod reader {
395
399
doc_as_u8 ( self . next_doc ( EsBool ) ) as bool
396
400
}
397
401
398
- fn read_f64 ( & mut self ) -> f64 { fail ! ( "read_f64()" ) ; }
399
- fn read_f32 ( & mut self ) -> f32 { fail ! ( "read_f32()" ) ; }
400
- fn read_float ( & mut self ) -> float { fail ! ( "read_float()" ) ; }
401
- fn read_char ( & mut self ) -> char { fail ! ( "read_char()" ) ; }
402
+ fn read_f64 ( & mut self ) -> f64 {
403
+ let bits = doc_as_u64 ( self . next_doc ( EsF64 ) ) ;
404
+ unsafe { transmute ( bits) }
405
+ }
406
+ fn read_f32 ( & mut self ) -> f32 {
407
+ let bits = doc_as_u32 ( self . next_doc ( EsF32 ) ) ;
408
+ unsafe { transmute ( bits) }
409
+ }
410
+ fn read_float ( & mut self ) -> float {
411
+ let bits = doc_as_u64 ( self . next_doc ( EsFloat ) ) ;
412
+ ( unsafe { transmute :: < u64 , f64 > ( bits) } ) as float
413
+ }
414
+ fn read_char ( & mut self ) -> char {
415
+ doc_as_u32 ( self . next_doc ( EsChar ) ) as char
416
+ }
402
417
fn read_str ( & mut self ) -> ~str { doc_as_str ( self . next_doc ( EsStr ) ) }
403
418
404
419
// Compound types:
@@ -541,66 +556,50 @@ pub mod reader {
541
556
542
557
fn read_seq < T > ( & mut self , f : & fn ( & mut Decoder , uint ) -> T ) -> T {
543
558
debug ! ( "read_seq()" ) ;
544
- let doc = self . next_doc ( EsVec ) ;
545
-
546
- let ( old_parent, old_pos) = ( self . parent , self . pos ) ;
547
- self . parent = doc;
548
- self . pos = self . parent . start ;
549
-
550
- let len = self . _next_uint ( EsVecLen ) ;
551
- debug ! ( " len=%u" , len) ;
552
- let result = f ( self , len) ;
553
-
554
- self . parent = old_parent;
555
- self . pos = old_pos;
556
- result
559
+ do self. push_doc ( EsVec ) |d| {
560
+ let len = d. _next_uint ( EsVecLen ) ;
561
+ debug ! ( " len=%u" , len) ;
562
+ f ( d, len)
563
+ }
557
564
}
558
565
559
566
fn read_seq_elt < T > ( & mut self , idx : uint , f : & fn ( & mut Decoder ) -> T )
560
567
-> T {
561
568
debug ! ( "read_seq_elt(idx=%u)" , idx) ;
562
- let doc = self . next_doc ( EsVecElt ) ;
563
-
564
- let ( old_parent, old_pos) = ( self . parent , self . pos ) ;
565
- self . parent = doc;
566
- self . pos = self . parent . start ;
567
-
568
- let result = f ( self ) ;
569
-
570
- self . parent = old_parent;
571
- self . pos = old_pos;
572
- result
569
+ self . push_doc ( EsVecElt , f)
573
570
}
574
571
575
- fn read_map < T > ( & mut self , _ : & fn ( & mut Decoder , uint ) -> T ) -> T {
572
+ fn read_map < T > ( & mut self , f : & fn ( & mut Decoder , uint ) -> T ) -> T {
576
573
debug ! ( "read_map()" ) ;
577
- fail ! ( "read_map is unimplemented" ) ;
574
+ do self. push_doc ( EsMap ) |d| {
575
+ let len = d. _next_uint ( EsMapLen ) ;
576
+ debug ! ( " len=%u" , len) ;
577
+ f ( d, len)
578
+ }
578
579
}
579
580
580
581
fn read_map_elt_key < T > ( & mut self ,
581
582
idx : uint ,
582
- _ : & fn ( & mut Decoder ) -> T )
583
+ f : & fn ( & mut Decoder ) -> T )
583
584
-> T {
584
585
debug ! ( "read_map_elt_key(idx=%u)" , idx) ;
585
- fail ! ( "read_map_elt_val is unimplemented" ) ;
586
+ self . push_doc ( EsMapKey , f )
586
587
}
587
588
588
589
fn read_map_elt_val < T > ( & mut self ,
589
590
idx : uint ,
590
- _ : & fn ( & mut Decoder ) -> T )
591
+ f : & fn ( & mut Decoder ) -> T )
591
592
-> T {
592
593
debug ! ( "read_map_elt_val(idx=%u)" , idx) ;
593
- fail ! ( "read_map_elt_val is unimplemented" ) ;
594
+ self . push_doc ( EsMapVal , f )
594
595
}
595
596
}
596
597
}
597
598
598
599
pub mod writer {
599
- use ebml:: { EbmlEncoderTag , EsBool , EsEnum , EsEnumBody , EsEnumVid } ;
600
- use ebml:: { EsI16 , EsI32 , EsI64 , EsI8 , EsInt } ;
601
- use ebml:: { EsLabel , EsOpaque , EsStr , EsU16 , EsU32 , EsU64 , EsU8 , EsUint } ;
602
- use ebml:: { EsVec , EsVecElt , EsVecLen } ;
600
+ use super :: * ;
603
601
602
+ use core:: cast;
604
603
use core:: io;
605
604
use core:: str;
606
605
@@ -806,19 +805,21 @@ pub mod writer {
806
805
self . wr_tagged_u8 ( EsBool as uint , v as u8 )
807
806
}
808
807
809
- // FIXME (#2742): implement these
810
- fn emit_f64 ( & mut self , _v : f64 ) {
811
- fail ! ( "Unimplemented: serializing an f64" ) ;
808
+ fn emit_f64 ( & mut self , v : f64 ) {
809
+ let bits = unsafe { cast :: transmute ( v ) } ;
810
+ self . wr_tagged_u64 ( EsF64 as uint , bits ) ;
812
811
}
813
- fn emit_f32 ( & mut self , _v : f32 ) {
814
- fail ! ( "Unimplemented: serializing an f32" ) ;
812
+ fn emit_f32 ( & mut self , v : f32 ) {
813
+ let bits = unsafe { cast:: transmute ( v) } ;
814
+ self . wr_tagged_u32 ( EsF32 as uint , bits) ;
815
815
}
816
- fn emit_float ( & mut self , _v : float ) {
817
- fail ! ( "Unimplemented: serializing a float" ) ;
816
+ fn emit_float ( & mut self , v : float ) {
817
+ let bits = unsafe { cast:: transmute ( v as f64 ) } ;
818
+ self . wr_tagged_u64 ( EsFloat as uint , bits) ;
818
819
}
819
820
820
- fn emit_char ( & mut self , _v : char ) {
821
- fail ! ( "Unimplemented: serializing a char" ) ;
821
+ fn emit_char ( & mut self , v : char ) {
822
+ self . wr_tagged_u32 ( EsChar as uint , v as u32 ) ;
822
823
}
823
824
824
825
fn emit_str ( & mut self , v : & str ) {
@@ -914,16 +915,23 @@ pub mod writer {
914
915
self . end_tag ( ) ;
915
916
}
916
917
917
- fn emit_map ( & mut self , _len : uint , _f : & fn ( & mut Encoder ) ) {
918
- fail ! ( "emit_map is unimplemented" ) ;
918
+ fn emit_map ( & mut self , len : uint , f : & fn ( & mut Encoder ) ) {
919
+ self . start_tag ( EsMap as uint ) ;
920
+ self . _emit_tagged_uint ( EsMapLen , len) ;
921
+ f ( self ) ;
922
+ self . end_tag ( ) ;
919
923
}
920
924
921
- fn emit_map_elt_key ( & mut self , _idx : uint , _f : & fn ( & mut Encoder ) ) {
922
- fail ! ( "emit_map_elt_key is unimplemented" ) ;
925
+ fn emit_map_elt_key ( & mut self , _idx : uint , f : & fn ( & mut Encoder ) ) {
926
+ self . start_tag ( EsMapKey as uint ) ;
927
+ f ( self ) ;
928
+ self . end_tag ( ) ;
923
929
}
924
930
925
- fn emit_map_elt_val ( & mut self , _idx : uint , _f : & fn ( & mut Encoder ) ) {
926
- fail ! ( "emit_map_elt_val is unimplemented" ) ;
931
+ fn emit_map_elt_val ( & mut self , _idx : uint , f : & fn ( & mut Encoder ) ) {
932
+ self . start_tag ( EsMapVal as uint ) ;
933
+ f ( self ) ;
934
+ self . end_tag ( ) ;
927
935
}
928
936
}
929
937
}
0 commit comments