@@ -593,13 +593,22 @@ pub mod writer {
593
593
use std:: io:: extensions:: u64_to_be_bytes;
594
594
595
595
// ebml writing
596
- pub struct Encoder < ' a > {
596
+ pub struct Encoder {
597
597
// FIXME(#5665): this should take a trait object
598
- writer : & ' a mut MemWriter ,
598
+ writer : @ mut MemWriter ,
599
599
priv size_positions : ~[ uint ] ,
600
600
}
601
601
602
- fn write_sized_vuint ( w : & mut MemWriter , n : uint , size : uint ) {
602
+ impl Clone for Encoder {
603
+ fn clone ( & self ) -> Encoder {
604
+ Encoder {
605
+ writer : self . writer ,
606
+ size_positions : self . size_positions . clone ( ) ,
607
+ }
608
+ }
609
+ }
610
+
611
+ fn write_sized_vuint ( w : @mut MemWriter , n : uint , size : uint ) {
603
612
match size {
604
613
1 u => w. write ( & [ 0x80u8 | ( n as u8 ) ] ) ,
605
614
2 u => w. write ( & [ 0x40u8 | ( ( n >> 8_ u) as u8 ) , n as u8 ] ) ,
@@ -611,15 +620,15 @@ pub mod writer {
611
620
} ;
612
621
}
613
622
614
- fn write_vuint ( w : & mut MemWriter , n : uint ) {
623
+ fn write_vuint ( w : @ mut MemWriter , n : uint ) {
615
624
if n < 0x7f_ u { write_sized_vuint ( w, n, 1 u) ; return ; }
616
625
if n < 0x4000_ u { write_sized_vuint ( w, n, 2 u) ; return ; }
617
626
if n < 0x200000_ u { write_sized_vuint ( w, n, 3 u) ; return ; }
618
627
if n < 0x10000000_ u { write_sized_vuint ( w, n, 4 u) ; return ; }
619
628
fail ! ( "vint to write too big: {}" , n) ;
620
629
}
621
630
622
- pub fn Encoder < ' a > ( w : & ' a mut MemWriter ) -> Encoder < ' a > {
631
+ pub fn Encoder ( w : @ mut MemWriter ) -> Encoder {
623
632
let size_positions: ~[ uint ] = ~[ ] ;
624
633
Encoder {
625
634
writer : w,
@@ -628,15 +637,7 @@ pub mod writer {
628
637
}
629
638
630
639
// FIXME (#2741): Provide a function to write the standard ebml header.
631
- impl < ' a > Encoder < ' a > {
632
- /// XXX(pcwalton): Workaround for badness in trans. DO NOT USE ME.
633
- pub unsafe fn unsafe_clone ( & self ) -> Encoder < ' a > {
634
- Encoder {
635
- writer : cast:: transmute_copy ( & self . writer ) ,
636
- size_positions : self . size_positions . clone ( ) ,
637
- }
638
- }
639
-
640
+ impl Encoder {
640
641
pub fn start_tag ( & mut self , tag_id : uint ) {
641
642
debug ! ( "Start tag {}" , tag_id) ;
642
643
@@ -738,7 +739,7 @@ pub mod writer {
738
739
// Totally lame approach.
739
740
static DEBUG : bool = true ;
740
741
741
- impl < ' a > Encoder < ' a > {
742
+ impl Encoder {
742
743
// used internally to emit things like the vector length and so on
743
744
fn _emit_tagged_uint ( & mut self , t : EbmlEncoderTag , v : uint ) {
744
745
assert ! ( v <= 0xFFFF_FFFF_ u) ;
@@ -754,15 +755,17 @@ pub mod writer {
754
755
// try and check failures more quickly.
755
756
if DEBUG { self . wr_tagged_str ( EsLabel as uint , label) }
756
757
}
758
+ }
757
759
760
+ impl Encoder {
758
761
pub fn emit_opaque ( & mut self , f: |& mut Encoder |) {
759
762
self . start_tag ( EsOpaque as uint ) ;
760
763
f ( self ) ;
761
764
self . end_tag ( ) ;
762
765
}
763
766
}
764
767
765
- impl < ' a > :: serialize:: Encoder for Encoder < ' a > {
768
+ impl :: serialize:: Encoder for Encoder {
766
769
fn emit_nil ( & mut self ) { }
767
770
768
771
fn emit_uint ( & mut self , v : uint ) {
@@ -817,7 +820,7 @@ pub mod writer {
817
820
self . wr_tagged_str ( EsStr as uint , v)
818
821
}
819
822
820
- fn emit_enum ( & mut self , name : & str , f: |& mut Encoder < ' a > |) {
823
+ fn emit_enum ( & mut self , name : & str , f: |& mut Encoder |) {
821
824
self . _emit_label ( name) ;
822
825
self . start_tag ( EsEnum as uint ) ;
823
826
f ( self ) ;
@@ -828,103 +831,98 @@ pub mod writer {
828
831
_: & str ,
829
832
v_id : uint ,
830
833
_: uint ,
831
- f: |& mut Encoder < ' a > |) {
834
+ f: |& mut Encoder |) {
832
835
self . _emit_tagged_uint ( EsEnumVid , v_id) ;
833
836
self . start_tag ( EsEnumBody as uint ) ;
834
837
f ( self ) ;
835
838
self . end_tag ( ) ;
836
839
}
837
840
838
- fn emit_enum_variant_arg( & mut self , _: uint , f: |& mut Encoder < ' a > |) {
841
+ fn emit_enum_variant_arg ( & mut self , _: uint , f: |& mut Encoder |) {
839
842
f ( self )
840
843
}
841
844
842
845
fn emit_enum_struct_variant ( & mut self ,
843
846
v_name : & str ,
844
847
v_id : uint ,
845
848
cnt : uint ,
846
- f: |& mut Encoder < ' a > |) {
849
+ f: |& mut Encoder |) {
847
850
self . emit_enum_variant ( v_name, v_id, cnt, f)
848
851
}
849
852
850
853
fn emit_enum_struct_variant_field ( & mut self ,
851
854
_: & str ,
852
855
idx : uint ,
853
- f: |& mut Encoder < ' a > |) {
856
+ f: |& mut Encoder |) {
854
857
self . emit_enum_variant_arg ( idx, f)
855
858
}
856
859
857
- fn emit_struct( & mut self ,
858
- _: & str,
859
- _len: uint,
860
- f: |& mut Encoder < ' a > |) {
860
+ fn emit_struct ( & mut self , _: & str , _len : uint , f: |& mut Encoder |) {
861
861
f ( self )
862
862
}
863
863
864
864
fn emit_struct_field ( & mut self ,
865
865
name : & str ,
866
866
_: uint ,
867
- f: |& mut Encoder < ' a > |) {
867
+ f: |& mut Encoder |) {
868
868
self . _emit_label ( name) ;
869
869
f ( self )
870
870
}
871
871
872
- fn emit_tuple( & mut self , len: uint, f: |& mut Encoder < ' a > |) {
872
+ fn emit_tuple ( & mut self , len : uint , f: |& mut Encoder |) {
873
873
self . emit_seq ( len, f)
874
874
}
875
- fn emit_tuple_arg( & mut self , idx: uint, f: |& mut Encoder < ' a > |) {
875
+ fn emit_tuple_arg ( & mut self , idx : uint , f: |& mut Encoder |) {
876
876
self . emit_seq_elt ( idx, f)
877
877
}
878
878
879
879
fn emit_tuple_struct ( & mut self ,
880
880
_: & str ,
881
881
len : uint ,
882
- f: |& mut Encoder < ' a > |) {
882
+ f: |& mut Encoder |) {
883
883
self . emit_seq ( len, f)
884
884
}
885
- fn emit_tuple_struct_arg( & mut self ,
886
- idx: uint,
887
- f: |& mut Encoder < ' a > |) {
885
+ fn emit_tuple_struct_arg ( & mut self , idx : uint , f: |& mut Encoder |) {
888
886
self . emit_seq_elt ( idx, f)
889
887
}
890
888
891
- fn emit_option( & mut self , f: |& mut Encoder < ' a > |) {
889
+ fn emit_option ( & mut self , f: |& mut Encoder |) {
892
890
self . emit_enum ( "Option" , f) ;
893
891
}
894
892
fn emit_option_none ( & mut self ) {
895
893
self . emit_enum_variant ( "None" , 0 , 0 , |_| ( ) )
896
894
}
897
- fn emit_option_some( & mut self , f: |& mut Encoder < ' a > |) {
895
+ fn emit_option_some ( & mut self , f: |& mut Encoder |) {
898
896
self . emit_enum_variant ( "Some" , 1 , 1 , f)
899
897
}
900
898
901
- fn emit_seq( & mut self , len: uint, f: |& mut Encoder < ' a > |) {
899
+ fn emit_seq ( & mut self , len : uint , f: |& mut Encoder |) {
902
900
self . start_tag ( EsVec as uint ) ;
903
901
self . _emit_tagged_uint ( EsVecLen , len) ;
904
902
f ( self ) ;
905
903
self . end_tag ( ) ;
906
904
}
907
905
908
- fn emit_seq_elt( & mut self , _idx: uint, f: |& mut Encoder < ' a > |) {
906
+ fn emit_seq_elt ( & mut self , _idx : uint , f: |& mut Encoder |) {
909
907
self . start_tag ( EsVecElt as uint ) ;
910
908
f ( self ) ;
911
909
self . end_tag ( ) ;
912
910
}
913
911
914
- fn emit_map( & mut self , len: uint, f: |& mut Encoder < ' a > |) {
912
+ fn emit_map ( & mut self , len : uint , f: |& mut Encoder |) {
915
913
self . start_tag ( EsMap as uint ) ;
916
914
self . _emit_tagged_uint ( EsMapLen , len) ;
917
915
f ( self ) ;
918
916
self . end_tag ( ) ;
919
917
}
920
918
921
- fn emit_map_elt_key( & mut self , _idx: uint, f: |& mut Encoder < ' a > |) {
919
+ fn emit_map_elt_key ( & mut self , _idx : uint , f: |& mut Encoder |) {
922
920
self . start_tag ( EsMapKey as uint ) ;
923
921
f ( self ) ;
924
922
self . end_tag ( ) ;
925
923
}
926
924
927
- fn emit_map_elt_val( & mut self , _idx: uint, f: |& mut Encoder < ' a > |) {
925
+ fn emit_map_elt_val ( & mut self , _idx : uint , f: |& mut Encoder |) {
928
926
self . start_tag ( EsMapVal as uint ) ;
929
927
f ( self ) ;
930
928
self . end_tag ( ) ;
@@ -950,11 +948,9 @@ mod tests {
950
948
fn test_option_int ( ) {
951
949
fn test_v ( v : Option < int > ) {
952
950
debug ! ( "v == {:?}" , v) ;
953
- let mut wr = MemWriter :: new( ) ;
954
- {
955
- let mut ebml_w = writer:: Encoder ( & mut wr) ;
956
- v. encode( & mut ebml_w) ;
957
- }
951
+ let wr = @mut MemWriter :: new ( ) ;
952
+ let mut ebml_w = writer:: Encoder ( wr) ;
953
+ v. encode ( & mut ebml_w) ;
958
954
let ebml_doc = reader:: Doc ( * wr. inner_ref ( ) ) ;
959
955
let mut deser = reader:: Decoder ( ebml_doc) ;
960
956
let v1 = serialize:: Decodable :: decode ( & mut deser) ;
0 commit comments