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