Skip to content

Commit 3b9046c

Browse files
committed
---
yaml --- r: 147582 b: refs/heads/try2 c: 2fb3328 h: refs/heads/master v: v3
1 parent 221734a commit 3b9046c

File tree

5 files changed

+149
-124
lines changed

5 files changed

+149
-124
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 82f5a380a48262e10e2d7c53e679351c5aedbf5a
8+
refs/heads/try2: 2fb33285e6169552e58d10197c11cd2f8702291f
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libextra/ebml.rs

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -593,22 +593,13 @@ pub mod writer {
593593
use std::io::extensions::u64_to_be_bytes;
594594

595595
// ebml writing
596-
pub struct Encoder {
596+
pub struct Encoder<'a> {
597597
// FIXME(#5665): this should take a trait object
598-
writer: @mut MemWriter,
598+
writer: &'a mut MemWriter,
599599
priv size_positions: ~[uint],
600600
}
601601

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) {
612603
match size {
613604
1u => w.write(&[0x80u8 | (n as u8)]),
614605
2u => w.write(&[0x40u8 | ((n >> 8_u) as u8), n as u8]),
@@ -620,15 +611,15 @@ pub mod writer {
620611
};
621612
}
622613

623-
fn write_vuint(w: @mut MemWriter, n: uint) {
614+
fn write_vuint(w: &mut MemWriter, n: uint) {
624615
if n < 0x7f_u { write_sized_vuint(w, n, 1u); return; }
625616
if n < 0x4000_u { write_sized_vuint(w, n, 2u); return; }
626617
if n < 0x200000_u { write_sized_vuint(w, n, 3u); return; }
627618
if n < 0x10000000_u { write_sized_vuint(w, n, 4u); return; }
628619
fail!("vint to write too big: {}", n);
629620
}
630621

631-
pub fn Encoder(w: @mut MemWriter) -> Encoder {
622+
pub fn Encoder<'a>(w: &'a mut MemWriter) -> Encoder<'a> {
632623
let size_positions: ~[uint] = ~[];
633624
Encoder {
634625
writer: w,
@@ -637,7 +628,15 @@ pub mod writer {
637628
}
638629

639630
// 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+
641640
pub fn start_tag(&mut self, tag_id: uint) {
642641
debug!("Start tag {}", tag_id);
643642

@@ -739,7 +738,7 @@ pub mod writer {
739738
// Totally lame approach.
740739
static DEBUG: bool = true;
741740

742-
impl Encoder {
741+
impl<'a> Encoder<'a> {
743742
// used internally to emit things like the vector length and so on
744743
fn _emit_tagged_uint(&mut self, t: EbmlEncoderTag, v: uint) {
745744
assert!(v <= 0xFFFF_FFFF_u);
@@ -755,17 +754,15 @@ pub mod writer {
755754
// try and check failures more quickly.
756755
if DEBUG { self.wr_tagged_str(EsLabel as uint, label) }
757756
}
758-
}
759757

760-
impl Encoder {
761758
pub fn emit_opaque(&mut self, f: |&mut Encoder|) {
762759
self.start_tag(EsOpaque as uint);
763760
f(self);
764761
self.end_tag();
765762
}
766763
}
767764

768-
impl ::serialize::Encoder for Encoder {
765+
impl<'a> ::serialize::Encoder for Encoder<'a> {
769766
fn emit_nil(&mut self) {}
770767

771768
fn emit_uint(&mut self, v: uint) {
@@ -820,7 +817,7 @@ pub mod writer {
820817
self.wr_tagged_str(EsStr as uint, v)
821818
}
822819

823-
fn emit_enum(&mut self, name: &str, f: |&mut Encoder|) {
820+
fn emit_enum(&mut self, name: &str, f: |&mut Encoder<'a>|) {
824821
self._emit_label(name);
825822
self.start_tag(EsEnum as uint);
826823
f(self);
@@ -831,98 +828,103 @@ pub mod writer {
831828
_: &str,
832829
v_id: uint,
833830
_: uint,
834-
f: |&mut Encoder|) {
831+
f: |&mut Encoder<'a>|) {
835832
self._emit_tagged_uint(EsEnumVid, v_id);
836833
self.start_tag(EsEnumBody as uint);
837834
f(self);
838835
self.end_tag();
839836
}
840837

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>|) {
842839
f(self)
843840
}
844841

845842
fn emit_enum_struct_variant(&mut self,
846843
v_name: &str,
847844
v_id: uint,
848845
cnt: uint,
849-
f: |&mut Encoder|) {
846+
f: |&mut Encoder<'a>|) {
850847
self.emit_enum_variant(v_name, v_id, cnt, f)
851848
}
852849

853850
fn emit_enum_struct_variant_field(&mut self,
854851
_: &str,
855852
idx: uint,
856-
f: |&mut Encoder|) {
853+
f: |&mut Encoder<'a>|) {
857854
self.emit_enum_variant_arg(idx, f)
858855
}
859856

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>|) {
861861
f(self)
862862
}
863863

864864
fn emit_struct_field(&mut self,
865865
name: &str,
866866
_: uint,
867-
f: |&mut Encoder|) {
867+
f: |&mut Encoder<'a>|) {
868868
self._emit_label(name);
869869
f(self)
870870
}
871871

872-
fn emit_tuple(&mut self, len: uint, f: |&mut Encoder|) {
872+
fn emit_tuple(&mut self, len: uint, f: |&mut Encoder<'a>|) {
873873
self.emit_seq(len, f)
874874
}
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>|) {
876876
self.emit_seq_elt(idx, f)
877877
}
878878

879879
fn emit_tuple_struct(&mut self,
880880
_: &str,
881881
len: uint,
882-
f: |&mut Encoder|) {
882+
f: |&mut Encoder<'a>|) {
883883
self.emit_seq(len, f)
884884
}
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>|) {
886888
self.emit_seq_elt(idx, f)
887889
}
888890

889-
fn emit_option(&mut self, f: |&mut Encoder|) {
891+
fn emit_option(&mut self, f: |&mut Encoder<'a>|) {
890892
self.emit_enum("Option", f);
891893
}
892894
fn emit_option_none(&mut self) {
893895
self.emit_enum_variant("None", 0, 0, |_| ())
894896
}
895-
fn emit_option_some(&mut self, f: |&mut Encoder|) {
897+
fn emit_option_some(&mut self, f: |&mut Encoder<'a>|) {
896898
self.emit_enum_variant("Some", 1, 1, f)
897899
}
898900

899-
fn emit_seq(&mut self, len: uint, f: |&mut Encoder|) {
901+
fn emit_seq(&mut self, len: uint, f: |&mut Encoder<'a>|) {
900902
self.start_tag(EsVec as uint);
901903
self._emit_tagged_uint(EsVecLen, len);
902904
f(self);
903905
self.end_tag();
904906
}
905907

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>|) {
907909
self.start_tag(EsVecElt as uint);
908910
f(self);
909911
self.end_tag();
910912
}
911913

912-
fn emit_map(&mut self, len: uint, f: |&mut Encoder|) {
914+
fn emit_map(&mut self, len: uint, f: |&mut Encoder<'a>|) {
913915
self.start_tag(EsMap as uint);
914916
self._emit_tagged_uint(EsMapLen, len);
915917
f(self);
916918
self.end_tag();
917919
}
918920

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>|) {
920922
self.start_tag(EsMapKey as uint);
921923
f(self);
922924
self.end_tag();
923925
}
924926

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>|) {
926928
self.start_tag(EsMapVal as uint);
927929
f(self);
928930
self.end_tag();
@@ -948,9 +950,11 @@ mod tests {
948950
fn test_option_int() {
949951
fn test_v(v: Option<int>) {
950952
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+
}
954958
let ebml_doc = reader::Doc(*wr.inner_ref());
955959
let mut deser = reader::Decoder(ebml_doc);
956960
let v1 = serialize::Decodable::decode(&mut deser);

0 commit comments

Comments
 (0)