diff options
| author | bors <bors@rust-lang.org> | 2013-05-02 20:39:36 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-05-02 20:39:36 -0700 |
| commit | b37a685958a99ae37eef4e8bf7e8498ea7b698c1 (patch) | |
| tree | 0500311b3c436c860a5fe622a9b8e171963d2e07 /src/libstd | |
| parent | baa1c1834f608c8c789db6d2495626ff9d28dd96 (diff) | |
| parent | dc5df61bc1914224d50d92cdd5599b6337ac68f2 (diff) | |
| download | rust-b37a685958a99ae37eef4e8bf7e8498ea7b698c1.tar.gz rust-b37a685958a99ae37eef4e8bf7e8498ea7b698c1.zip | |
auto merge of #6201 : pcwalton/rust/inhtwama-serializer, r=graydon
This PR removes mutable fields from the serializer and makes the encoder and decoder use INHTWAMA properly (i.e. `&mut self`). r? @graydon
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/arena.rs | 119 | ||||
| -rw-r--r-- | src/libstd/ebml.rs | 818 | ||||
| -rw-r--r-- | src/libstd/flatpipes.rs | 33 | ||||
| -rw-r--r-- | src/libstd/json.rs | 871 | ||||
| -rw-r--r-- | src/libstd/list.rs | 25 | ||||
| -rw-r--r-- | src/libstd/serialize.rs | 1145 | ||||
| -rw-r--r-- | src/libstd/workcache.rs | 53 |
7 files changed, 2810 insertions, 254 deletions
diff --git a/src/libstd/arena.rs b/src/libstd/arena.rs index 8e2c7345045..0e9b2ed3da8 100644 --- a/src/libstd/arena.rs +++ b/src/libstd/arena.rs @@ -32,11 +32,11 @@ // overhead when initializing plain-old-data and means we don't need // to waste time running the destructors of POD. +use list::{MutList, MutCons, MutNil}; use list; -use list::{List, Cons, Nil}; use core::at_vec; -use core::cast::transmute; +use core::cast::{transmute, transmute_mut_region}; use core::cast; use core::libc::size_t; use core::ptr; @@ -74,7 +74,7 @@ static tydesc_drop_glue_index: size_t = 3 as size_t; // will always stay at 0. struct Chunk { data: @[u8], - mut fill: uint, + fill: uint, is_pod: bool, } @@ -82,9 +82,9 @@ pub struct Arena { // The head is seperated out from the list as a unbenchmarked // microoptimization, to avoid needing to case on the list to // access the head. - priv mut head: Chunk, - priv mut pod_head: Chunk, - priv mut chunks: @List<Chunk>, + priv head: Chunk, + priv pod_head: Chunk, + priv chunks: @mut MutList<Chunk>, } #[unsafe_destructor] @@ -92,8 +92,10 @@ impl Drop for Arena { fn finalize(&self) { unsafe { destroy_chunk(&self.head); - for list::each(self.chunks) |chunk| { - if !chunk.is_pod { destroy_chunk(chunk); } + for self.chunks.each |chunk| { + if !chunk.is_pod { + destroy_chunk(chunk); + } } } } @@ -113,7 +115,7 @@ pub fn arena_with_size(initial_size: uint) -> Arena { Arena { head: chunk(initial_size, false), pod_head: chunk(initial_size, true), - chunks: @Nil, + chunks: @mut MutNil, } } @@ -170,11 +172,11 @@ unsafe fn un_bitpack_tydesc_ptr(p: uint) -> (*TypeDesc, bool) { pub impl Arena { // Functions for the POD part of the arena - priv fn alloc_pod_grow(&self, n_bytes: uint, align: uint) -> *u8 { + priv fn alloc_pod_grow(&mut self, n_bytes: uint, align: uint) -> *u8 { // Allocate a new chunk. let chunk_size = at_vec::capacity(self.pod_head.data); let new_min_chunk_size = uint::max(n_bytes, chunk_size); - self.chunks = @Cons(copy self.pod_head, self.chunks); + self.chunks = @mut MutCons(copy self.pod_head, self.chunks); self.pod_head = chunk(uint::next_power_of_two(new_min_chunk_size + 1u), true); @@ -182,27 +184,28 @@ pub impl Arena { } #[inline(always)] - priv fn alloc_pod_inner(&self, n_bytes: uint, align: uint) -> *u8 { - let head = &mut self.pod_head; + priv fn alloc_pod_inner(&mut self, n_bytes: uint, align: uint) -> *u8 { + unsafe { + // XXX: Borrow check + let head = transmute_mut_region(&mut self.pod_head); - let start = round_up_to(head.fill, align); - let end = start + n_bytes; - if end > at_vec::capacity(head.data) { - return self.alloc_pod_grow(n_bytes, align); - } - head.fill = end; + let start = round_up_to(head.fill, align); + let end = start + n_bytes; + if end > at_vec::capacity(head.data) { + return self.alloc_pod_grow(n_bytes, align); + } + head.fill = end; - //debug!("idx = %u, size = %u, align = %u, fill = %u", - // start, n_bytes, align, head.fill); + //debug!("idx = %u, size = %u, align = %u, fill = %u", + // start, n_bytes, align, head.fill); - unsafe { ptr::offset(vec::raw::to_ptr(head.data), start) } } #[inline(always)] #[cfg(stage0)] - priv fn alloc_pod<T>(&self, op: &fn() -> T) -> &'self T { + priv fn alloc_pod<T>(&mut self, op: &fn() -> T) -> &'self T { unsafe { let tydesc = sys::get_type_desc::<T>(); let ptr = self.alloc_pod_inner((*tydesc).size, (*tydesc).align); @@ -216,7 +219,7 @@ pub impl Arena { #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] - priv fn alloc_pod<'a, T>(&'a self, op: &fn() -> T) -> &'a T { + priv fn alloc_pod<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T { unsafe { let tydesc = sys::get_type_desc::<T>(); let ptr = self.alloc_pod_inner((*tydesc).size, (*tydesc).align); @@ -227,11 +230,12 @@ pub impl Arena { } // Functions for the non-POD part of the arena - priv fn alloc_nonpod_grow(&self, n_bytes: uint, align: uint) -> (*u8, *u8) { + priv fn alloc_nonpod_grow(&mut self, n_bytes: uint, align: uint) + -> (*u8, *u8) { // Allocate a new chunk. let chunk_size = at_vec::capacity(self.head.data); let new_min_chunk_size = uint::max(n_bytes, chunk_size); - self.chunks = @Cons(copy self.head, self.chunks); + self.chunks = @mut MutCons(copy self.head, self.chunks); self.head = chunk(uint::next_power_of_two(new_min_chunk_size + 1u), false); @@ -239,22 +243,23 @@ pub impl Arena { } #[inline(always)] - priv fn alloc_nonpod_inner(&self, n_bytes: uint, align: uint) -> (*u8, *u8) { - let head = &mut self.head; - - let tydesc_start = head.fill; - let after_tydesc = head.fill + sys::size_of::<*TypeDesc>(); - let start = round_up_to(after_tydesc, align); - let end = start + n_bytes; - if end > at_vec::capacity(head.data) { - return self.alloc_nonpod_grow(n_bytes, align); - } - head.fill = round_up_to(end, sys::pref_align_of::<*TypeDesc>()); + priv fn alloc_nonpod_inner(&mut self, n_bytes: uint, align: uint) + -> (*u8, *u8) { + unsafe { + let head = transmute_mut_region(&mut self.head); + + let tydesc_start = head.fill; + let after_tydesc = head.fill + sys::size_of::<*TypeDesc>(); + let start = round_up_to(after_tydesc, align); + let end = start + n_bytes; + if end > at_vec::capacity(head.data) { + return self.alloc_nonpod_grow(n_bytes, align); + } + head.fill = round_up_to(end, sys::pref_align_of::<*TypeDesc>()); - //debug!("idx = %u, size = %u, align = %u, fill = %u", - // start, n_bytes, align, head.fill); + //debug!("idx = %u, size = %u, align = %u, fill = %u", + // start, n_bytes, align, head.fill); - unsafe { let buf = vec::raw::to_ptr(head.data); return (ptr::offset(buf, tydesc_start), ptr::offset(buf, start)); } @@ -262,7 +267,7 @@ pub impl Arena { #[inline(always)] #[cfg(stage0)] - priv fn alloc_nonpod<T>(&self, op: &fn() -> T) -> &'self T { + priv fn alloc_nonpod<T>(&mut self, op: &fn() -> T) -> &'self T { unsafe { let tydesc = sys::get_type_desc::<T>(); let (ty_ptr, ptr) = @@ -286,7 +291,7 @@ pub impl Arena { #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] - priv fn alloc_nonpod<'a, T>(&'a self, op: &fn() -> T) -> &'a T { + priv fn alloc_nonpod<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T { unsafe { let tydesc = sys::get_type_desc::<T>(); let (ty_ptr, ptr) = @@ -309,13 +314,16 @@ pub impl Arena { // The external interface #[inline(always)] #[cfg(stage0)] - fn alloc<T>(&self, op: &fn() -> T) -> &'self T { + fn alloc<T>(&mut self, op: &fn() -> T) -> &'self T { unsafe { + // XXX: Borrow check + let this = transmute_mut_region(self); if !rusti::needs_drop::<T>() { - self.alloc_pod(op) - } else { - self.alloc_nonpod(op) + return this.alloc_pod(op); } + // XXX: Borrow check + let this = transmute_mut_region(self); + this.alloc_nonpod(op) } } @@ -324,20 +332,23 @@ pub impl Arena { #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] - fn alloc<'a, T>(&'a self, op: &fn() -> T) -> &'a T { + fn alloc<'a, T>(&'a mut self, op: &fn() -> T) -> &'a T { unsafe { + // XXX: Borrow check + let this = transmute_mut_region(self); if !rusti::needs_drop::<T>() { - self.alloc_pod(op) - } else { - self.alloc_nonpod(op) + return this.alloc_pod(op); } + // XXX: Borrow check + let this = transmute_mut_region(self); + this.alloc_nonpod(op) } } } #[test] fn test_arena_destructors() { - let arena = Arena(); + let mut arena = Arena(); for uint::range(0, 10) |i| { // Arena allocate something with drop glue to make sure it // doesn't leak. @@ -348,9 +359,11 @@ fn test_arena_destructors() { } } -#[test] #[should_fail] #[ignore(cfg(windows))] +#[test] +#[should_fail] +#[ignore(cfg(windows))] fn test_arena_destructors_fail() { - let arena = Arena(); + let mut arena = Arena(); // Put some stuff in the arena. for uint::range(0, 10) |i| { // Arena allocate something with drop glue to make sure it diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index 2598e96a141..41c5a0f7690 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -36,13 +36,27 @@ pub struct TaggedDoc { } pub enum EbmlEncoderTag { - EsUint, EsU64, EsU32, EsU16, EsU8, - EsInt, EsI64, EsI32, EsI16, EsI8, - EsBool, - EsStr, - EsF64, EsF32, EsFloat, - EsEnum, EsEnumVid, EsEnumBody, - EsVec, EsVecLen, EsVecElt, + EsUint, // 0 + EsU64, // 1 + EsU32, // 2 + EsU16, // 3 + EsU8, // 4 + EsInt, // 5 + EsI64, // 6 + EsI32, // 7 + EsI16, // 8 + EsI8, // 9 + EsBool, // 10 + EsStr, // 11 + EsF64, // 12 + EsF32, // 13 + EsFloat, // 14 + EsEnum, // 15 + EsEnumVid, // 16 + EsEnumBody, // 17 + EsVec, // 18 + EsVecLen, // 19 + EsVecElt, // 20 EsOpaque, @@ -249,17 +263,27 @@ pub mod reader { pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 } pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 } - + #[cfg(stage0)] pub struct Decoder { priv mut parent: Doc, priv mut pos: uint, } + #[cfg(not(stage0))] + pub struct Decoder { + priv parent: Doc, + priv pos: uint, + } + pub fn Decoder(d: Doc) -> Decoder { - Decoder { parent: d, pos: d.start } + Decoder { + parent: d, + pos: d.start + } } priv impl Decoder { + #[cfg(stage0)] fn _check_label(&self, lbl: &str) { if self.pos < self.parent.end { let TaggedDoc { tag: r_tag, doc: r_doc } = @@ -269,13 +293,33 @@ pub mod reader { self.pos = r_doc.end; let str = doc_as_str(r_doc); if lbl != str { - fail!(fmt!("Expected label %s but found %s", lbl, - str)); + fail!(fmt!("Expected label %s but found %s", + lbl, + str)); + } + } + } + } + + #[cfg(not(stage0))] + fn _check_label(&mut self, lbl: &str) { + if self.pos < self.parent.end { + let TaggedDoc { tag: r_tag, doc: r_doc } = + doc_at(self.parent.data, self.pos); + + if r_tag == (EsLabel as uint) { + self.pos = r_doc.end; + let str = doc_as_str(r_doc); + if lbl != str { + fail!(fmt!("Expected label %s but found %s", + lbl, + str)); } } } } + #[cfg(stage0)] fn next_doc(&self, exp_tag: EbmlEncoderTag) -> Doc { debug!(". next_doc(exp_tag=%?)", exp_tag); if self.pos >= self.parent.end { @@ -298,6 +342,30 @@ pub mod reader { r_doc } + #[cfg(not(stage0))] + fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> Doc { + debug!(". next_doc(exp_tag=%?)", exp_tag); + if self.pos >= self.parent.end { + fail!(~"no more documents in current node!"); + } + let TaggedDoc { tag: r_tag, doc: r_doc } = + doc_at(self.parent.data, self.pos); + debug!("self.parent=%?-%? self.pos=%? r_tag=%? r_doc=%?-%?", + copy self.parent.start, copy self.parent.end, + copy self.pos, r_tag, r_doc.start, r_doc.end); + if r_tag != (exp_tag as uint) { + fail!(fmt!("expected EBML doc with tag %? but found tag %?", + exp_tag, r_tag)); + } + if r_doc.end > self.parent.end { + fail!(fmt!("invalid EBML, child extends to 0x%x, \ + parent to 0x%x", r_doc.end, self.parent.end)); + } + self.pos = r_doc.end; + r_doc + } + + #[cfg(stage0)] fn push_doc<T>(&self, d: Doc, f: &fn() -> T) -> T { let old_parent = self.parent; let old_pos = self.pos; @@ -309,21 +377,58 @@ pub mod reader { r } + #[cfg(not(stage0))] + fn push_doc<T>(&mut self, d: Doc, f: &fn() -> T) -> T { + let old_parent = self.parent; + let old_pos = self.pos; + self.parent = d; + self.pos = d.start; + let r = f(); + self.parent = old_parent; + self.pos = old_pos; + r + } + + #[cfg(stage0)] fn _next_uint(&self, exp_tag: EbmlEncoderTag) -> uint { let r = doc_as_u32(self.next_doc(exp_tag)); debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); r as uint } + + #[cfg(not(stage0))] + fn _next_uint(&mut self, exp_tag: EbmlEncoderTag) -> uint { + let r = doc_as_u32(self.next_doc(exp_tag)); + debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); + r as uint + } } pub impl Decoder { + #[cfg(stage0)] fn read_opaque<R>(&self, op: &fn(Doc) -> R) -> R { do self.push_doc(self.next_doc(EsOpaque)) { op(copy self.parent) } } + + #[cfg(not(stage0))] + fn read_opaque<R>(&mut self, op: &fn(&mut Decoder, Doc) -> R) -> R { + let doc = self.next_doc(EsOpaque); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = doc.start; + + let result = op(self, doc); + + self.parent = old_parent; + self.pos = old_pos; + result + } } + #[cfg(stage0)] impl serialize::Decoder for Decoder { fn read_nil(&self) -> () { () } @@ -339,10 +444,18 @@ pub mod reader { v as uint } - fn read_i64(&self) -> i64 { doc_as_u64(self.next_doc(EsI64)) as i64 } - fn read_i32(&self) -> i32 { doc_as_u32(self.next_doc(EsI32)) as i32 } - fn read_i16(&self) -> i16 { doc_as_u16(self.next_doc(EsI16)) as i16 } - fn read_i8 (&self) -> i8 { doc_as_u8 (self.next_doc(EsI8 )) as i8 } + fn read_i64(&self) -> i64 { + doc_as_u64(self.next_doc(EsI64)) as i64 + } + fn read_i32(&self) -> i32 { + doc_as_u32(self.next_doc(EsI32)) as i32 + } + fn read_i16(&self) -> i16 { + doc_as_u16(self.next_doc(EsI16)) as i16 + } + fn read_i8 (&self) -> i8 { + doc_as_u8(self.next_doc(EsI8 )) as i8 + } fn read_int(&self) -> int { let v = doc_as_u64(self.next_doc(EsInt)) as i64; if v > (int::max_value as i64) || v < (int::min_value as i64) { @@ -351,8 +464,9 @@ pub mod reader { v as int } - fn read_bool(&self) -> bool { doc_as_u8(self.next_doc(EsBool)) - as bool } + fn read_bool(&self) -> bool { + doc_as_u8(self.next_doc(EsBool)) as bool + } fn read_f64(&self) -> f64 { fail!(~"read_f64()"); } fn read_f32(&self) -> f32 { fail!(~"read_f32()"); } @@ -367,7 +481,10 @@ pub mod reader { self.push_doc(self.next_doc(EsEnum), f) } - fn read_enum_variant<T>(&self, _names: &[&str], f: &fn(uint) -> T) -> T { + fn read_enum_variant<T>(&self, + _: &[&str], + f: &fn(uint) -> T) + -> T { debug!("read_enum_variant()"); let idx = self._next_uint(EsEnumVid); debug!(" idx=%u", idx); @@ -376,12 +493,17 @@ pub mod reader { } } - fn read_enum_variant_arg<T>(&self, idx: uint, f: &fn() -> T) -> T { + fn read_enum_variant_arg<T>(&self, + idx: uint, + f: &fn() -> T) -> T { debug!("read_enum_variant_arg(idx=%u)", idx); f() } - fn read_enum_struct_variant<T>(&self, _names: &[&str], f: &fn(uint) -> T) -> T { + fn read_enum_struct_variant<T>(&self, + _: &[&str], + f: &fn(uint) -> T) + -> T { debug!("read_enum_struct_variant()"); let idx = self._next_uint(EsEnumVid); debug!(" idx=%u", idx); @@ -390,32 +512,34 @@ pub mod reader { } } - fn read_enum_struct_variant_field<T>(&self, name: &str, idx: uint, f: &fn() -> T) -> T { + fn read_enum_struct_variant_field<T>(&self, + name: &str, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_enum_struct_variant_arg(name=%?, idx=%u)", name, idx); f() } - fn read_struct<T>(&self, name: &str, _len: uint, f: &fn() -> T) -> T { + fn read_struct<T>(&self, + name: &str, + _: uint, + f: &fn() -> T) + -> T { debug!("read_struct(name=%s)", name); f() } - #[cfg(stage0)] - fn read_field<T>(&self, name: &str, idx: uint, f: &fn() -> T) -> T { + fn read_field<T>(&self, + name: &str, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_field(name=%?, idx=%u)", name, idx); self._check_label(name); f() } - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn read_struct_field<T>(&self, name: &str, idx: uint, f: &fn() -> T) -> T { - debug!("read_struct_field(name=%?, idx=%u)", name, idx); - self._check_label(name); - f() - } - fn read_tuple<T>(&self, f: &fn(uint) -> T) -> T { debug!("read_tuple()"); self.read_seq(f) @@ -426,12 +550,18 @@ pub mod reader { self.read_seq_elt(idx, f) } - fn read_tuple_struct<T>(&self, name: &str, f: &fn(uint) -> T) -> T { + fn read_tuple_struct<T>(&self, + name: &str, + f: &fn(uint) -> T) + -> T { debug!("read_tuple_struct(name=%?)", name); self.read_tuple(f) } - fn read_tuple_struct_arg<T>(&self, idx: uint, f: &fn() -> T) -> T { + fn read_tuple_struct_arg<T>(&self, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_tuple_struct_arg(idx=%u)", idx); self.read_tuple_arg(idx, f) } @@ -478,6 +608,245 @@ pub mod reader { fail!(~"read_map_elt_val is unimplemented"); } } + + #[cfg(not(stage0))] + impl serialize::Decoder for Decoder { + fn read_nil(&mut self) -> () { () } + + fn read_u64(&mut self) -> u64 { doc_as_u64(self.next_doc(EsU64)) } + fn read_u32(&mut self) -> u32 { doc_as_u32(self.next_doc(EsU32)) } + fn read_u16(&mut self) -> u16 { doc_as_u16(self.next_doc(EsU16)) } + fn read_u8 (&mut self) -> u8 { doc_as_u8 (self.next_doc(EsU8 )) } + fn read_uint(&mut self) -> uint { + let v = doc_as_u64(self.next_doc(EsUint)); + if v > (::core::uint::max_value as u64) { + fail!(fmt!("uint %? too large for this architecture", v)); + } + v as uint + } + + fn read_i64(&mut self) -> i64 { + doc_as_u64(self.next_doc(EsI64)) as i64 + } + fn read_i32(&mut self) -> i32 { + doc_as_u32(self.next_doc(EsI32)) as i32 + } + fn read_i16(&mut self) -> i16 { + doc_as_u16(self.next_doc(EsI16)) as i16 + } + fn read_i8 (&mut self) -> i8 { + doc_as_u8(self.next_doc(EsI8 )) as i8 + } + fn read_int(&mut self) -> int { + let v = doc_as_u64(self.next_doc(EsInt)) as i64; + if v > (int::max_value as i64) || v < (int::min_value as i64) { + fail!(fmt!("int %? out of range for this architecture", v)); + } + v as int + } + + fn read_bool(&mut self) -> bool { + doc_as_u8(self.next_doc(EsBool)) as bool + } + + fn read_f64(&mut self) -> f64 { fail!(~"read_f64()"); } + fn read_f32(&mut self) -> f32 { fail!(~"read_f32()"); } + fn read_float(&mut self) -> float { fail!(~"read_float()"); } + fn read_char(&mut self) -> char { fail!(~"read_char()"); } + fn read_str(&mut self) -> ~str { doc_as_str(self.next_doc(EsStr)) } + + // Compound types: + fn read_enum<T>(&mut self, + name: &str, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_enum(%s)", name); + self._check_label(name); + + let doc = self.next_doc(EsEnum); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let result = f(self); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_enum_variant<T>(&mut self, + _: &[&str], + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_enum_variant()"); + let idx = self._next_uint(EsEnumVid); + debug!(" idx=%u", idx); + + let doc = self.next_doc(EsEnumBody); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let result = f(self, idx); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_enum_variant_arg<T>(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) -> T { + debug!("read_enum_variant_arg(idx=%u)", idx); + f(self) + } + + fn read_enum_struct_variant<T>(&mut self, + _: &[&str], + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_enum_struct_variant()"); + let idx = self._next_uint(EsEnumVid); + debug!(" idx=%u", idx); + + let doc = self.next_doc(EsEnumBody); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let result = f(self, idx); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_enum_struct_variant_field<T>(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_enum_struct_variant_arg(name=%?, idx=%u)", name, idx); + f(self) + } + + fn read_struct<T>(&mut self, + name: &str, + _: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_struct(name=%s)", name); + f(self) + } + + fn read_struct_field<T>(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_struct_field(name=%?, idx=%u)", name, idx); + self._check_label(name); + f(self) + } + + fn read_tuple<T>(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_tuple()"); + self.read_seq(f) + } + + fn read_tuple_arg<T>(&mut self, idx: uint, f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_tuple_arg(idx=%u)", idx); + self.read_seq_elt(idx, f) + } + + fn read_tuple_struct<T>(&mut self, + name: &str, + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_tuple_struct(name=%?)", name); + self.read_tuple(f) + } + + fn read_tuple_struct_arg<T>(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_tuple_struct_arg(idx=%u)", idx); + self.read_tuple_arg(idx, f) + } + + fn read_option<T>(&mut self, f: &fn(&mut Decoder, bool) -> T) -> T { + debug!("read_option()"); + do self.read_enum("Option") |this| { + do this.read_enum_variant(["None", "Some"]) |this, idx| { + match idx { + 0 => f(this, false), + 1 => f(this, true), + _ => fail!(), + } + } + } + } + + fn read_seq<T>(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_seq()"); + let doc = self.next_doc(EsVec); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let len = self._next_uint(EsVecLen); + debug!(" len=%u", len); + let result = f(self, len); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_seq_elt<T>(&mut self, idx: uint, f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_seq_elt(idx=%u)", idx); + let doc = self.next_doc(EsVecElt); + + let (old_parent, old_pos) = (self.parent, self.pos); + self.parent = doc; + self.pos = self.parent.start; + + let result = f(self); + + self.parent = old_parent; + self.pos = old_pos; + result + } + + fn read_map<T>(&mut self, _: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_map()"); + fail!(~"read_map is unimplemented"); + } + + fn read_map_elt_key<T>(&mut self, + idx: uint, + _: &fn(&mut Decoder) -> T) + -> T { + debug!("read_map_elt_key(idx=%u)", idx); + fail!(~"read_map_elt_val is unimplemented"); + } + + fn read_map_elt_val<T>(&mut self, + idx: uint, + _: &fn(&mut Decoder) -> T) + -> T { + debug!("read_map_elt_val(idx=%u)", idx); + fail!(~"read_map_elt_val is unimplemented"); + } + } } pub mod writer { @@ -522,6 +891,7 @@ pub mod writer { } // FIXME (#2741): Provide a function to write the standard ebml header. + #[cfg(stage0)] pub impl Encoder { fn start_tag(&self, tag_id: uint) { debug!("Start tag %u", tag_id); @@ -617,13 +987,111 @@ pub mod writer { } } + // FIXME (#2741): Provide a function to write the standard ebml header. + #[cfg(not(stage0))] + pub impl Encoder { + fn start_tag(&mut self, tag_id: uint) { + debug!("Start tag %u", tag_id); + + // Write the enum ID: + write_vuint(self.writer, tag_id); + + // Write a placeholder four-byte size. + self.size_positions.push(self.writer.tell()); + let zeroes: &[u8] = &[0u8, 0u8, 0u8, 0u8]; + self.writer.write(zeroes); + } + + fn end_tag(&mut self) { + let last_size_pos = self.size_positions.pop(); + let cur_pos = self.writer.tell(); + self.writer.seek(last_size_pos as int, io::SeekSet); + let size = (cur_pos - last_size_pos - 4u); + write_sized_vuint(self.writer, size, 4u); + self.writer.seek(cur_pos as int, io::SeekSet); + + debug!("End tag (size = %u)", size); + } + + fn wr_tag(&mut self, tag_id: uint, blk: &fn()) { + self.start_tag(tag_id); + blk(); + self.end_tag(); + } + + fn wr_tagged_bytes(&mut self, tag_id: uint, b: &[u8]) { + write_vuint(self.writer, tag_id); + write_vuint(self.writer, vec::len(b)); + self.writer.write(b); + } + + fn wr_tagged_u64(&mut self, tag_id: uint, v: u64) { + do io::u64_to_be_bytes(v, 8u) |v| { + self.wr_tagged_bytes(tag_id, v); + } + } + + fn wr_tagged_u32(&mut self, tag_id: uint, v: u32) { + do io::u64_to_be_bytes(v as u64, 4u) |v| { + self.wr_tagged_bytes(tag_id, v); + } + } + + fn wr_tagged_u16(&mut self, tag_id: uint, v: u16) { + do io::u64_to_be_bytes(v as u64, 2u) |v| { + self.wr_tagged_bytes(tag_id, v); + } + } + + fn wr_tagged_u8(&mut self, tag_id: uint, v: u8) { + self.wr_tagged_bytes(tag_id, &[v]); + } + + fn wr_tagged_i64(&mut self, tag_id: uint, v: i64) { + do io::u64_to_be_bytes(v as u64, 8u) |v| { + self.wr_tagged_bytes(tag_id, v); + } + } + + fn wr_tagged_i32(&mut self, tag_id: uint, v: i32) { + do io::u64_to_be_bytes(v as u64, 4u) |v| { + self.wr_tagged_bytes(tag_id, v); + } + } + + fn wr_tagged_i16(&mut self, tag_id: uint, v: i16) { + do io::u64_to_be_bytes(v as u64, 2u) |v| { + self.wr_tagged_bytes(tag_id, v); + } + } + + fn wr_tagged_i8(&mut self, tag_id: uint, v: i8) { + self.wr_tagged_bytes(tag_id, &[v as u8]); + } + + fn wr_tagged_str(&mut self, tag_id: uint, v: &str) { + str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b)); + } + + fn wr_bytes(&mut self, b: &[u8]) { + debug!("Write %u bytes", vec::len(b)); + self.writer.write(b); + } + + fn wr_str(&mut self, s: &str) { + debug!("Write str: %?", s); + self.writer.write(str::to_bytes(s)); + } + } + // FIXME (#2743): optionally perform "relaxations" on end_tag to more // efficiently encode sizes; this is a fixed point iteration // Set to true to generate more debugging in EBML code. // Totally lame approach. - static debug: bool = false; + static debug: bool = true; + #[cfg(stage0)] priv impl Encoder { // used internally to emit things like the vector length and so on fn _emit_tagged_uint(&self, t: EbmlEncoderTag, v: uint) { @@ -642,6 +1110,26 @@ pub mod writer { } } + #[cfg(not(stage0))] + priv impl Encoder { + // used internally to emit things like the vector length and so on + fn _emit_tagged_uint(&mut self, t: EbmlEncoderTag, v: uint) { + assert!(v <= 0xFFFF_FFFF_u); + self.wr_tagged_u32(t as uint, v as u32); + } + + fn _emit_label(&mut self, label: &str) { + // There are various strings that we have access to, such as + // the name of a record field, which do not actually appear in + // the encoded EBML (normally). This is just for + // efficiency. When debugging, though, we can emit such + // labels and then they will be checked by decoder to + // try and check failures more quickly. + if debug { self.wr_tagged_str(EsLabel as uint, label) } + } + } + + #[cfg(stage0)] pub impl Encoder { fn emit_opaque(&self, f: &fn()) { do self.wr_tag(EsOpaque as uint) { @@ -650,24 +1138,50 @@ pub mod writer { } } + #[cfg(not(stage0))] + pub impl Encoder { + fn emit_opaque(&mut self, f: &fn(&mut Encoder)) { + self.start_tag(EsOpaque as uint); + f(self); + self.end_tag(); + } + } + + #[cfg(stage0)] impl ::serialize::Encoder for Encoder { fn emit_nil(&self) {} fn emit_uint(&self, v: uint) { self.wr_tagged_u64(EsUint as uint, v as u64); } - fn emit_u64(&self, v: u64) { self.wr_tagged_u64(EsU64 as uint, v); } - fn emit_u32(&self, v: u32) { self.wr_tagged_u32(EsU32 as uint, v); } - fn emit_u16(&self, v: u16) { self.wr_tagged_u16(EsU16 as uint, v); } - fn emit_u8(&self, v: u8) { self.wr_tagged_u8 (EsU8 as uint, v); } + fn emit_u64(&self, v: u64) { + self.wr_tagged_u64(EsU64 as uint, v); + } + fn emit_u32(&self, v: u32) { + self.wr_tagged_u32(EsU32 as uint, v); + } + fn emit_u16(&self, v: u16) { + self.wr_tagged_u16(EsU16 as uint, v); + } + fn emit_u8(&self, v: u8) { + self.wr_tagged_u8(EsU8 as uint, v); + } fn emit_int(&self, v: int) { self.wr_tagged_i64(EsInt as uint, v as i64); } - fn emit_i64(&self, v: i64) { self.wr_tagged_i64(EsI64 as uint, v); } - fn emit_i32(&self, v: i32) { self.wr_tagged_i32(EsI32 as uint, v); } - fn emit_i16(&self, v: i16) { self.wr_tagged_i16(EsI16 as uint, v); } - fn emit_i8(&self, v: i8) { self.wr_tagged_i8 (EsI8 as uint, v); } + fn emit_i64(&self, v: i64) { + self.wr_tagged_i64(EsI64 as uint, v); + } + fn emit_i32(&self, v: i32) { + self.wr_tagged_i32(EsI32 as uint, v); + } + fn emit_i16(&self, v: i16) { + self.wr_tagged_i16(EsI16 as uint, v); + } + fn emit_i8(&self, v: i8) { + self.wr_tagged_i8(EsI8 as uint, v); + } fn emit_bool(&self, v: bool) { self.wr_tagged_u8(EsBool as uint, v as u8) @@ -697,41 +1211,56 @@ pub mod writer { self.wr_tag(EsEnum as uint, f) } - fn emit_enum_variant(&self, _v_name: &str, v_id: uint, _cnt: uint, + fn emit_enum_variant(&self, + _: &str, + v_id: uint, + _: uint, f: &fn()) { self._emit_tagged_uint(EsEnumVid, v_id); self.wr_tag(EsEnumBody as uint, f) } - fn emit_enum_variant_arg(&self, _idx: uint, f: &fn()) { f() } + fn emit_enum_variant_arg(&self, _: uint, f: &fn()) { + f() + } - fn emit_enum_struct_variant(&self, v_name: &str, v_id: uint, cnt: uint, f: &fn()) { + fn emit_enum_struct_variant(&self, + v_name: &str, + v_id: uint, + cnt: uint, + f: &fn()) { self.emit_enum_variant(v_name, v_id, cnt, f) } - fn emit_enum_struct_variant_field(&self, _f_name: &str, idx: uint, f: &fn()) { + fn emit_enum_struct_variant_field(&self, + _: &str, + idx: uint, + f: &fn()) { self.emit_enum_variant_arg(idx, f) } - fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) { f() } - #[cfg(stage0)] - fn emit_field(&self, name: &str, _idx: uint, f: &fn()) { - self._emit_label(name); + fn emit_struct(&self, _: &str, _len: uint, f: &fn()) { f() } - #[cfg(stage1)] - #[cfg(stage2)] - #[cfg(stage3)] - fn emit_struct_field(&self, name: &str, _idx: uint, f: &fn()) { + + fn emit_field(&self, name: &str, _idx: uint, f: &fn()) { self._emit_label(name); f() } - fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple(&self, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } fn emit_option(&self, f: &fn()) { self.emit_enum("Option", f); @@ -766,6 +1295,167 @@ pub mod writer { fail!(~"emit_map_elt_val is unimplemented"); } } + + #[cfg(not(stage0))] + impl ::serialize::Encoder for Encoder { + fn emit_nil(&mut self) {} + + fn emit_uint(&mut self, v: uint) { + self.wr_tagged_u64(EsUint as uint, v as u64); + } + fn emit_u64(&mut self, v: u64) { + self.wr_tagged_u64(EsU64 as uint, v); + } + fn emit_u32(&mut self, v: u32) { + self.wr_tagged_u32(EsU32 as uint, v); + } + fn emit_u16(&mut self, v: u16) { + self.wr_tagged_u16(EsU16 as uint, v); + } + fn emit_u8(&mut self, v: u8) { + self.wr_tagged_u8(EsU8 as uint, v); + } + + fn emit_int(&mut self, v: int) { + self.wr_tagged_i64(EsInt as uint, v as i64); + } + fn emit_i64(&mut self, v: i64) { + self.wr_tagged_i64(EsI64 as uint, v); + } + fn emit_i32(&mut self, v: i32) { + self.wr_tagged_i32(EsI32 as uint, v); + } + fn emit_i16(&mut self, v: i16) { + self.wr_tagged_i16(EsI16 as uint, v); + } + fn emit_i8(&mut self, v: i8) { + self.wr_tagged_i8(EsI8 as uint, v); + } + + fn emit_bool(&mut self, v: bool) { + self.wr_tagged_u8(EsBool as uint, v as u8) + } + + // FIXME (#2742): implement these + fn emit_f64(&mut self, _v: f64) { + fail!(~"Unimplemented: serializing an f64"); + } + fn emit_f32(&mut self, _v: f32) { + fail!(~"Unimplemented: serializing an f32"); + } + fn emit_float(&mut self, _v: float) { + fail!(~"Unimplemented: serializing a float"); + } + + fn emit_char(&mut self, _v: char) { + fail!(~"Unimplemented: serializing a char"); + } + + fn emit_str(&mut self, v: &str) { + self.wr_tagged_str(EsStr as uint, v) + } + + fn emit_enum(&mut self, name: &str, f: &fn(&mut Encoder)) { + self._emit_label(name); + self.start_tag(EsEnum as uint); + f(self); + self.end_tag(); + } + + fn emit_enum_variant(&mut self, + _: &str, + v_id: uint, + _: uint, + f: &fn(&mut Encoder)) { + self._emit_tagged_uint(EsEnumVid, v_id); + self.start_tag(EsEnumBody as uint); + f(self); + self.end_tag(); + } + + fn emit_enum_variant_arg(&mut self, _: uint, f: &fn(&mut Encoder)) { + f(self) + } + + fn emit_enum_struct_variant(&mut self, + v_name: &str, + v_id: uint, + cnt: uint, + f: &fn(&mut Encoder)) { + self.emit_enum_variant(v_name, v_id, cnt, f) + } + + fn emit_enum_struct_variant_field(&mut self, + _: &str, + idx: uint, + f: &fn(&mut Encoder)) { + self.emit_enum_variant_arg(idx, f) + } + + fn emit_struct(&mut self, _: &str, _len: uint, f: &fn(&mut Encoder)) { + f(self) + } + + fn emit_struct_field(&mut self, + name: &str, + _: uint, + f: &fn(&mut Encoder)) { + self._emit_label(name); + f(self) + } + + fn emit_tuple(&mut self, len: uint, f: &fn(&mut Encoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_tuple_struct(&mut self, + _: &str, + len: uint, + f: &fn(&mut Encoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_option(&mut self, f: &fn(&mut Encoder)) { + self.emit_enum("Option", f); + } + fn emit_option_none(&mut self) { + self.emit_enum_variant("None", 0, 0, |_| ()) + } + fn emit_option_some(&mut self, f: &fn(&mut Encoder)) { + self.emit_enum_variant("Some", 1, 1, f) + } + + fn emit_seq(&mut self, len: uint, f: &fn(&mut Encoder)) { + self.start_tag(EsVec as uint); + self._emit_tagged_uint(EsVecLen, len); + f(self); + self.end_tag(); + } + + fn emit_seq_elt(&mut self, _idx: uint, f: &fn(&mut Encoder)) { + self.start_tag(EsVecElt as uint); + f(self); + self.end_tag(); + } + + fn emit_map(&mut self, _len: uint, _f: &fn(&mut Encoder)) { + fail!(~"emit_map is unimplemented"); + } + + fn emit_map_elt_key(&mut self, _idx: uint, _f: &fn(&mut Encoder)) { + fail!(~"emit_map_elt_key is unimplemented"); + } + + fn emit_map_elt_val(&mut self, _idx: uint, _f: &fn(&mut Encoder)) { + fail!(~"emit_map_elt_val is unimplemented"); + } + } } // ___________________________________________________________________________ @@ -786,12 +1476,12 @@ mod tests { fn test_v(v: Option<int>) { debug!("v == %?", v); let bytes = do io::with_bytes_writer |wr| { - let ebml_w = writer::Encoder(wr); - v.encode(&ebml_w) + let mut ebml_w = writer::Encoder(wr); + v.encode(&mut ebml_w) }; let ebml_doc = reader::Doc(@bytes); - let deser = reader::Decoder(ebml_doc); - let v1 = serialize::Decodable::decode(&deser); + let mut deser = reader::Decoder(ebml_doc); + let v1 = serialize::Decodable::decode(&mut deser); debug!("v1 == %?", v1); assert!(v == v1); } diff --git a/src/libstd/flatpipes.rs b/src/libstd/flatpipes.rs index bd0acb849fc..55ea9c2948b 100644 --- a/src/libstd/flatpipes.rs +++ b/src/libstd/flatpipes.rs @@ -438,8 +438,11 @@ pub mod flatteners { SerializingFlattener */ + #[cfg(stage0)] pub fn deserialize_buffer<D: Decoder + FromReader, - T: Decodable<D>>(buf: &[u8]) -> T { + T: Decodable<D>>( + buf: &[u8]) + -> T { let buf = vec::from_slice(buf); let buf_reader = @BufReader::new(buf); let reader = buf_reader as @Reader; @@ -447,14 +450,40 @@ pub mod flatteners { Decodable::decode(&deser) } + #[cfg(not(stage0))] + pub fn deserialize_buffer<D: Decoder + FromReader, + T: Decodable<D>>( + buf: &[u8]) + -> T { + let buf = vec::from_slice(buf); + let buf_reader = @BufReader::new(buf); + let reader = buf_reader as @Reader; + let mut deser: D = FromReader::from_reader(reader); + Decodable::decode(&mut deser) + } + + #[cfg(stage0)] pub fn serialize_value<D: Encoder + FromWriter, - T: Encodable<D>>(val: &T) -> ~[u8] { + T: Encodable<D>>( + val: &T) + -> ~[u8] { do io::with_bytes_writer |writer| { let ser = FromWriter::from_writer(writer); val.encode(&ser); } } + #[cfg(not(stage0))] + pub fn serialize_value<D: Encoder + FromWriter, + T: Encodable<D>>( + val: &T) + -> ~[u8] { + do io::with_bytes_writer |writer| { + let mut ser = FromWriter::from_writer(writer); + val.encode(&mut ser); + } + } + pub trait FromReader { fn from_reader(r: @Reader) -> Self; } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 7353bec7333..6951ee377c9 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -72,9 +72,12 @@ pub struct Encoder { } pub fn Encoder(wr: @io::Writer) -> Encoder { - Encoder { wr: wr } + Encoder { + wr: wr + } } +#[cfg(stage0)] impl serialize::Encoder for Encoder { fn emit_nil(&self) { self.wr.write_str("null") } @@ -109,7 +112,11 @@ impl serialize::Encoder for Encoder { fn emit_enum(&self, _name: &str, f: &fn()) { f() } - fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) { + fn emit_enum_variant(&self, + name: &str, + _id: uint, + cnt: uint, + f: &fn()) { // enums are encoded as strings or vectors: // Bunny => "Bunny" // Kangaroo(34,"William") => ["Kangaroo",[34,"William"]] @@ -130,19 +137,27 @@ impl serialize::Encoder for Encoder { f(); } - fn emit_enum_struct_variant(&self, name: &str, id: uint, cnt: uint, f: &fn()) { + fn emit_enum_struct_variant(&self, + name: &str, + id: uint, + cnt: uint, + f: &fn()) { self.emit_enum_variant(name, id, cnt, f) } - fn emit_enum_struct_variant_field(&self, _field: &str, idx: uint, f: &fn()) { + fn emit_enum_struct_variant_field(&self, + _: &str, + idx: uint, + f: &fn()) { self.emit_enum_variant_arg(idx, f) } - fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) { + fn emit_struct(&self, _: &str, _: uint, f: &fn()) { self.wr.write_char('{'); f(); self.wr.write_char('}'); } + #[cfg(stage0)] fn emit_field(&self, name: &str, idx: uint, f: &fn()) { if idx != 0 { self.wr.write_char(','); } @@ -150,6 +165,7 @@ impl serialize::Encoder for Encoder { self.wr.write_char(':'); f(); } + #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] @@ -161,10 +177,16 @@ impl serialize::Encoder for Encoder { } fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } fn emit_option(&self, f: &fn()) { f(); } fn emit_option_none(&self) { self.emit_nil(); } @@ -198,15 +220,163 @@ impl serialize::Encoder for Encoder { } } +#[cfg(not(stage0))] +impl serialize::Encoder for Encoder { + fn emit_nil(&mut self) { self.wr.write_str("null") } + + fn emit_uint(&mut self, v: uint) { self.emit_float(v as float); } + fn emit_u64(&mut self, v: u64) { self.emit_float(v as float); } + fn emit_u32(&mut self, v: u32) { self.emit_float(v as float); } + fn emit_u16(&mut self, v: u16) { self.emit_float(v as float); } + fn emit_u8(&mut self, v: u8) { self.emit_float(v as float); } + + fn emit_int(&mut self, v: int) { self.emit_float(v as float); } + fn emit_i64(&mut self, v: i64) { self.emit_float(v as float); } + fn emit_i32(&mut self, v: i32) { self.emit_float(v as float); } + fn emit_i16(&mut self, v: i16) { self.emit_float(v as float); } + fn emit_i8(&mut self, v: i8) { self.emit_float(v as float); } + + fn emit_bool(&mut self, v: bool) { + if v { + self.wr.write_str("true"); + } else { + self.wr.write_str("false"); + } + } + + fn emit_f64(&mut self, v: f64) { self.emit_float(v as float); } + fn emit_f32(&mut self, v: f32) { self.emit_float(v as float); } + fn emit_float(&mut self, v: float) { + self.wr.write_str(float::to_str_digits(v, 6u)); + } + + fn emit_char(&mut self, v: char) { self.emit_str(str::from_char(v)) } + fn emit_str(&mut self, v: &str) { self.wr.write_str(escape_str(v)) } + + fn emit_enum(&mut self, _name: &str, f: &fn(&mut Encoder)) { f(self) } + + fn emit_enum_variant(&mut self, + name: &str, + _id: uint, + cnt: uint, + f: &fn(&mut Encoder)) { + // enums are encoded as strings or vectors: + // Bunny => "Bunny" + // Kangaroo(34,"William") => ["Kangaroo",[34,"William"]] + + if cnt == 0 { + self.wr.write_str(escape_str(name)); + } else { + self.wr.write_char('['); + self.wr.write_str(escape_str(name)); + self.wr.write_char(','); + f(self); + self.wr.write_char(']'); + } + } + + fn emit_enum_variant_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + if idx != 0 { + self.wr.write_char(','); + } + f(self); + } + + fn emit_enum_struct_variant(&mut self, + name: &str, + id: uint, + cnt: uint, + f: &fn(&mut Encoder)) { + self.emit_enum_variant(name, id, cnt, f) + } + + fn emit_enum_struct_variant_field(&mut self, + _: &str, + idx: uint, + f: &fn(&mut Encoder)) { + self.emit_enum_variant_arg(idx, f) + } + + fn emit_struct(&mut self, _: &str, _: uint, f: &fn(&mut Encoder)) { + self.wr.write_char('{'); + f(self); + self.wr.write_char('}'); + } + + fn emit_struct_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Encoder)) { + if idx != 0 { self.wr.write_char(','); } + self.wr.write_str(escape_str(name)); + self.wr.write_char(':'); + f(self); + } + + fn emit_tuple(&mut self, len: uint, f: &fn(&mut Encoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_tuple_struct(&mut self, + _name: &str, + len: uint, + f: &fn(&mut Encoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&mut self, idx: uint, f: &fn(&mut Encoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_option(&mut self, f: &fn(&mut Encoder)) { f(self); } + fn emit_option_none(&mut self) { self.emit_nil(); } + fn emit_option_some(&mut self, f: &fn(&mut Encoder)) { f(self); } + + fn emit_seq(&mut self, _len: uint, f: &fn(&mut Encoder)) { + self.wr.write_char('['); + f(self); + self.wr.write_char(']'); + } + + fn emit_seq_elt(&mut self, idx: uint, f: &fn(&mut Encoder)) { + if idx != 0 { + self.wr.write_char(','); + } + f(self) + } + + fn emit_map(&mut self, _len: uint, f: &fn(&mut Encoder)) { + self.wr.write_char('{'); + f(self); + self.wr.write_char('}'); + } + + fn emit_map_elt_key(&mut self, idx: uint, f: &fn(&mut Encoder)) { + if idx != 0 { self.wr.write_char(','); } + f(self) + } + + fn emit_map_elt_val(&mut self, _idx: uint, f: &fn(&mut Encoder)) { + self.wr.write_char(':'); + f(self) + } +} + pub struct PrettyEncoder { priv wr: @io::Writer, priv mut indent: uint, } pub fn PrettyEncoder(wr: @io::Writer) -> PrettyEncoder { - PrettyEncoder { wr: wr, indent: 0 } + PrettyEncoder { + wr: wr, + indent: 0, + } } +#[cfg(stage0)] impl serialize::Encoder for PrettyEncoder { fn emit_nil(&self) { self.wr.write_str("null") } @@ -241,7 +411,11 @@ impl serialize::Encoder for PrettyEncoder { fn emit_enum(&self, _name: &str, f: &fn()) { f() } - fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) { + fn emit_enum_variant(&self, + name: &str, + _: uint, + cnt: uint, + f: &fn()) { if cnt == 0 { self.wr.write_str(escape_str(name)); } else { @@ -267,11 +441,18 @@ impl serialize::Encoder for PrettyEncoder { f() } - fn emit_enum_struct_variant(&self, name: &str, id: uint, cnt: uint, f: &fn()) { + fn emit_enum_struct_variant(&self, + name: &str, + id: uint, + cnt: uint, + f: &fn()) { self.emit_enum_variant(name, id, cnt, f) } - fn emit_enum_struct_variant_field(&self, _field: &str, idx: uint, f: &fn()) { + fn emit_enum_struct_variant_field(&self, + _: &str, + idx: uint, + f: &fn()) { self.emit_enum_variant_arg(idx, f) } @@ -289,6 +470,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_char('}'); } } + #[cfg(stage0)] fn emit_field(&self, name: &str, idx: uint, f: &fn()) { if idx == 0 { @@ -301,6 +483,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_str(": "); f(); } + #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] @@ -316,11 +499,19 @@ impl serialize::Encoder for PrettyEncoder { f(); } - fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple(&self, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } - fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) } - fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) } + fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { + self.emit_seq_elt(idx, f) + } fn emit_option(&self, f: &fn()) { f(); } fn emit_option_none(&self) { self.emit_nil(); } @@ -339,6 +530,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_char(']'); } } + fn emit_seq_elt(&self, idx: uint, f: &fn()) { if idx == 0 { self.wr.write_char('\n'); @@ -362,6 +554,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_char('}'); } } + fn emit_map_elt_key(&self, idx: uint, f: &fn()) { if idx == 0 { self.wr.write_char('\n'); @@ -378,6 +571,201 @@ impl serialize::Encoder for PrettyEncoder { } } +#[cfg(not(stage0))] +impl serialize::Encoder for PrettyEncoder { + fn emit_nil(&mut self) { self.wr.write_str("null") } + + fn emit_uint(&mut self, v: uint) { self.emit_float(v as float); } + fn emit_u64(&mut self, v: u64) { self.emit_float(v as float); } + fn emit_u32(&mut self, v: u32) { self.emit_float(v as float); } + fn emit_u16(&mut self, v: u16) { self.emit_float(v as float); } + fn emit_u8(&mut self, v: u8) { self.emit_float(v as float); } + + fn emit_int(&mut self, v: int) { self.emit_float(v as float); } + fn emit_i64(&mut self, v: i64) { self.emit_float(v as float); } + fn emit_i32(&mut self, v: i32) { self.emit_float(v as float); } + fn emit_i16(&mut self, v: i16) { self.emit_float(v as float); } + fn emit_i8(&mut self, v: i8) { self.emit_float(v as float); } + + fn emit_bool(&mut self, v: bool) { + if v { + self.wr.write_str("true"); + } else { + self.wr.write_str("false"); + } + } + + fn emit_f64(&mut self, v: f64) { self.emit_float(v as float); } + fn emit_f32(&mut self, v: f32) { self.emit_float(v as float); } + fn emit_float(&mut self, v: float) { + self.wr.write_str(float::to_str_digits(v, 6u)); + } + + fn emit_char(&mut self, v: char) { self.emit_str(str::from_char(v)) } + fn emit_str(&mut self, v: &str) { self.wr.write_str(escape_str(v)); } + + fn emit_enum(&mut self, _name: &str, f: &fn(&mut PrettyEncoder)) { + f(self) + } + + fn emit_enum_variant(&mut self, + name: &str, + _: uint, + cnt: uint, + f: &fn(&mut PrettyEncoder)) { + if cnt == 0 { + self.wr.write_str(escape_str(name)); + } else { + self.wr.write_char('['); + self.indent += 2; + self.wr.write_char('\n'); + self.wr.write_str(spaces(self.indent)); + self.wr.write_str(escape_str(name)); + self.wr.write_str(",\n"); + f(self); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char(']'); + } + } + + fn emit_enum_variant_arg(&mut self, + idx: uint, + f: &fn(&mut PrettyEncoder)) { + if idx != 0 { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + f(self) + } + + fn emit_enum_struct_variant(&mut self, + name: &str, + id: uint, + cnt: uint, + f: &fn(&mut PrettyEncoder)) { + self.emit_enum_variant(name, id, cnt, f) + } + + fn emit_enum_struct_variant_field(&mut self, + _: &str, + idx: uint, + f: &fn(&mut PrettyEncoder)) { + self.emit_enum_variant_arg(idx, f) + } + + + fn emit_struct(&mut self, + _: &str, + len: uint, + f: &fn(&mut PrettyEncoder)) { + if len == 0 { + self.wr.write_str("{}"); + } else { + self.wr.write_char('{'); + self.indent += 2; + f(self); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char('}'); + } + } + + fn emit_struct_field(&mut self, + name: &str, + idx: uint, + f: &fn(&mut PrettyEncoder)) { + if idx == 0 { + self.wr.write_char('\n'); + } else { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + self.wr.write_str(escape_str(name)); + self.wr.write_str(": "); + f(self); + } + + fn emit_tuple(&mut self, len: uint, f: &fn(&mut PrettyEncoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut PrettyEncoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_tuple_struct(&mut self, + _: &str, + len: uint, + f: &fn(&mut PrettyEncoder)) { + self.emit_seq(len, f) + } + fn emit_tuple_struct_arg(&mut self, + idx: uint, + f: &fn(&mut PrettyEncoder)) { + self.emit_seq_elt(idx, f) + } + + fn emit_option(&mut self, f: &fn(&mut PrettyEncoder)) { f(self); } + fn emit_option_none(&mut self) { self.emit_nil(); } + fn emit_option_some(&mut self, f: &fn(&mut PrettyEncoder)) { f(self); } + + fn emit_seq(&mut self, len: uint, f: &fn(&mut PrettyEncoder)) { + if len == 0 { + self.wr.write_str("[]"); + } else { + self.wr.write_char('['); + self.indent += 2; + f(self); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char(']'); + } + } + + fn emit_seq_elt(&mut self, idx: uint, f: &fn(&mut PrettyEncoder)) { + if idx == 0 { + self.wr.write_char('\n'); + } else { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + f(self) + } + + fn emit_map(&mut self, len: uint, f: &fn(&mut PrettyEncoder)) { + if len == 0 { + self.wr.write_str("{}"); + } else { + self.wr.write_char('{'); + self.indent += 2; + f(self); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char('}'); + } + } + + fn emit_map_elt_key(&mut self, idx: uint, f: &fn(&mut PrettyEncoder)) { + if idx == 0 { + self.wr.write_char('\n'); + } else { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + f(self); + } + + fn emit_map_elt_val(&mut self, _idx: uint, f: &fn(&mut PrettyEncoder)) { + self.wr.write_str(": "); + f(self); + } +} + +#[cfg(stage0)] impl<E: serialize::Encoder> serialize::Encodable<E> for Json { fn encode(&self, e: &E) { match *self { @@ -391,9 +779,32 @@ impl<E: serialize::Encoder> serialize::Encodable<E> for Json { } } +#[cfg(not(stage0))] +impl<E: serialize::Encoder> serialize::Encodable<E> for Json { + fn encode(&self, e: &mut E) { + match *self { + Number(v) => v.encode(e), + String(ref v) => v.encode(e), + Boolean(v) => v.encode(e), + List(ref v) => v.encode(e), + Object(ref v) => v.encode(e), + Null => e.emit_nil(), + } + } +} + /// Encodes a json value into a io::writer +#[cfg(stage0)] pub fn to_writer(wr: @io::Writer, json: &Json) { - json.encode(&Encoder(wr)) + let encoder = Encoder(wr); + json.encode(&encoder) +} + +/// Encodes a json value into a io::writer +#[cfg(not(stage0))] +pub fn to_writer(wr: @io::Writer, json: &Json) { + let mut encoder = Encoder(wr); + json.encode(&mut encoder) } /// Encodes a json value into a string @@ -402,8 +813,17 @@ pub fn to_str(json: &Json) -> ~str { } /// Encodes a json value into a io::writer +#[cfg(stage0)] pub fn to_pretty_writer(wr: @io::Writer, json: &Json) { - json.encode(&PrettyEncoder(wr)) + let encoder = PrettyEncoder(wr); + json.encode(&encoder) +} + +/// Encodes a json value into a io::writer +#[cfg(not(stage0))] +pub fn to_pretty_writer(wr: @io::Writer, json: &Json) { + let mut encoder = PrettyEncoder(wr); + json.encode(&mut encoder) } /// Encodes a json value into a string @@ -794,9 +1214,12 @@ pub struct Decoder { } pub fn Decoder(json: Json) -> Decoder { - Decoder { stack: ~[json] } + Decoder { + stack: ~[json] + } } +#[cfg(stage0)] impl serialize::Decoder for Decoder { fn read_nil(&self) -> () { debug!("read_nil"); @@ -856,7 +1279,10 @@ impl serialize::Decoder for Decoder { f() } - fn read_enum_variant<T>(&self, names: &[&str], f: &fn(uint) -> T) -> T { + fn read_enum_variant<T>(&self, + names: &[&str], + f: &fn(uint) -> T) + -> T { debug!("read_enum_variant(names=%?)", names); let name = match self.stack.pop() { String(s) => s, @@ -883,13 +1309,20 @@ impl serialize::Decoder for Decoder { f() } - fn read_enum_struct_variant<T>(&self, names: &[&str], f: &fn(uint) -> T) -> T { + fn read_enum_struct_variant<T>(&self, + names: &[&str], + f: &fn(uint) -> T) + -> T { debug!("read_enum_struct_variant(names=%?)", names); self.read_enum_variant(names, f) } - fn read_enum_struct_variant_field<T>(&self, name: &str, idx: uint, f: &fn() -> T) -> T { + fn read_enum_struct_variant_field<T>(&self, + name: &str, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_enum_struct_variant_field(name=%?, idx=%u)", name, idx); self.read_enum_variant_arg(idx, f) } @@ -924,7 +1357,11 @@ impl serialize::Decoder for Decoder { #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] - fn read_struct_field<T>(&self, name: &str, idx: uint, f: &fn() -> T) -> T { + fn read_struct_field<T>(&self, + name: &str, + idx: uint, + f: &fn() -> T) + -> T { debug!("read_struct_field(name=%?, idx=%u)", name, idx); match self.stack.pop() { Object(obj) => { @@ -1018,6 +1455,262 @@ impl serialize::Decoder for Decoder { } } +#[cfg(not(stage0))] +impl serialize::Decoder for Decoder { + fn read_nil(&mut self) -> () { + debug!("read_nil"); + match self.stack.pop() { + Null => (), + value => fail!(fmt!("not a null: %?", value)) + } + } + + fn read_u64(&mut self) -> u64 { self.read_float() as u64 } + fn read_u32(&mut self) -> u32 { self.read_float() as u32 } + fn read_u16(&mut self) -> u16 { self.read_float() as u16 } + fn read_u8 (&mut self) -> u8 { self.read_float() as u8 } + fn read_uint(&mut self) -> uint { self.read_float() as uint } + + fn read_i64(&mut self) -> i64 { self.read_float() as i64 } + fn read_i32(&mut self) -> i32 { self.read_float() as i32 } + fn read_i16(&mut self) -> i16 { self.read_float() as i16 } + fn read_i8 (&mut self) -> i8 { self.read_float() as i8 } + fn read_int(&mut self) -> int { self.read_float() as int } + + fn read_bool(&mut self) -> bool { + debug!("read_bool"); + match self.stack.pop() { + Boolean(b) => b, + value => fail!(fmt!("not a boolean: %?", value)) + } + } + + fn read_f64(&mut self) -> f64 { self.read_float() as f64 } + fn read_f32(&mut self) -> f32 { self.read_float() as f32 } + fn read_float(&mut self) -> float { + debug!("read_float"); + match self.stack.pop() { + Number(f) => f, + value => fail!(fmt!("not a number: %?", value)) + } + } + + fn read_char(&mut self) -> char { + let mut v = ~[]; + for str::each_char(self.read_str()) |c| { v.push(c) } + if v.len() != 1 { fail!(~"string must have one character") } + v[0] + } + + fn read_str(&mut self) -> ~str { + debug!("read_str"); + match self.stack.pop() { + String(s) => s, + json => fail!(fmt!("not a string: %?", json)) + } + } + + fn read_enum<T>(&mut self, name: &str, f: &fn(&mut Decoder) -> T) -> T { + debug!("read_enum(%s)", name); + f(self) + } + + fn read_enum_variant<T>(&mut self, + names: &[&str], + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_enum_variant(names=%?)", names); + let name = match self.stack.pop() { + String(s) => s, + List(list) => { + do vec::consume_reverse(list) |_i, v| { + self.stack.push(v); + } + match self.stack.pop() { + String(s) => s, + value => fail!(fmt!("invalid variant name: %?", value)), + } + } + ref json => fail!(fmt!("invalid variant: %?", *json)), + }; + let idx = match vec::position(names, |n| str::eq_slice(*n, name)) { + Some(idx) => idx, + None => fail!(fmt!("Unknown variant name: %?", name)), + }; + f(self, idx) + } + + fn read_enum_variant_arg<T>(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_enum_variant_arg(idx=%u)", idx); + f(self) + } + + fn read_enum_struct_variant<T>(&mut self, + names: &[&str], + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_enum_struct_variant(names=%?)", names); + self.read_enum_variant(names, f) + } + + + fn read_enum_struct_variant_field<T>(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_enum_struct_variant_field(name=%?, idx=%u)", name, idx); + self.read_enum_variant_arg(idx, f) + } + + fn read_struct<T>(&mut self, + name: &str, + len: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_struct(name=%s, len=%u)", name, len); + let value = f(self); + self.stack.pop(); + value + } + + #[cfg(stage0)] + fn read_field<T>(&mut self, name: &str, idx: uint, f: &fn() -> T) -> T { + debug!("read_field(name=%?, idx=%u)", name, idx); + match self.stack.pop() { + Object(obj) => { + let mut obj = obj; + let value = match obj.pop(&name.to_owned()) { + None => fail!(fmt!("no such field: %s", name)), + Some(json) => { + self.stack.push(json); + f() + } + }; + self.stack.push(Object(obj)); + value + } + value => fail!(fmt!("not an object: %?", value)) + } + } + + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn read_struct_field<T>(&mut self, + name: &str, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_struct_field(name=%?, idx=%u)", name, idx); + match self.stack.pop() { + Object(obj) => { + let mut obj = obj; + let value = match obj.pop(&name.to_owned()) { + None => fail!(fmt!("no such field: %s", name)), + Some(json) => { + self.stack.push(json); + f(self) + } + }; + self.stack.push(Object(obj)); + value + } + value => fail!(fmt!("not an object: %?", value)) + } + } + + fn read_tuple<T>(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_tuple()"); + self.read_seq(f) + } + + fn read_tuple_arg<T>(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_tuple_arg(idx=%u)", idx); + self.read_seq_elt(idx, f) + } + + fn read_tuple_struct<T>(&mut self, + name: &str, + f: &fn(&mut Decoder, uint) -> T) + -> T { + debug!("read_tuple_struct(name=%?)", name); + self.read_tuple(f) + } + + fn read_tuple_struct_arg<T>(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_tuple_struct_arg(idx=%u)", idx); + self.read_tuple_arg(idx, f) + } + + fn read_option<T>(&mut self, f: &fn(&mut Decoder, bool) -> T) -> T { + match self.stack.pop() { + Null => f(self, false), + value => { self.stack.push(value); f(self, true) } + } + } + + fn read_seq<T>(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_seq()"); + let len = match self.stack.pop() { + List(list) => { + let len = list.len(); + do vec::consume_reverse(list) |_i, v| { + self.stack.push(v); + } + len + } + _ => fail!(~"not a list"), + }; + f(self, len) + } + + fn read_seq_elt<T>(&mut self, idx: uint, f: &fn(&mut Decoder) -> T) -> T { + debug!("read_seq_elt(idx=%u)", idx); + f(self) + } + + fn read_map<T>(&mut self, f: &fn(&mut Decoder, uint) -> T) -> T { + debug!("read_map()"); + let len = match self.stack.pop() { + Object(obj) => { + let mut obj = obj; + let len = obj.len(); + do obj.consume |key, value| { + self.stack.push(value); + self.stack.push(String(key)); + } + len + } + json => fail!(fmt!("not an object: %?", json)), + }; + f(self, len) + } + + fn read_map_elt_key<T>(&mut self, + idx: uint, + f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_map_elt_key(idx=%u)", idx); + f(self) + } + + fn read_map_elt_val<T>(&mut self, idx: uint, f: &fn(&mut Decoder) -> T) + -> T { + debug!("read_map_elt_val(idx=%u)", idx); + f(self) + } +} + impl Eq for Json { fn eq(&self, other: &Json) -> bool { match (self) { @@ -1452,15 +2145,15 @@ mod tests { let animal = Dog; assert_eq!( do io::with_str_writer |wr| { - let encoder = Encoder(wr); - animal.encode(&encoder); + let mut encoder = Encoder(wr); + animal.encode(&mut encoder); }, ~"\"Dog\"" ); assert_eq!( do io::with_str_writer |wr| { - let encoder = PrettyEncoder(wr); - animal.encode(&encoder); + let mut encoder = PrettyEncoder(wr); + animal.encode(&mut encoder); }, ~"\"Dog\"" ); @@ -1468,15 +2161,15 @@ mod tests { let animal = Frog(~"Henry", 349); assert_eq!( do io::with_str_writer |wr| { - let encoder = Encoder(wr); - animal.encode(&encoder); + let mut encoder = Encoder(wr); + animal.encode(&mut encoder); }, ~"[\"Frog\",\"Henry\",349]" ); assert_eq!( do io::with_str_writer |wr| { - let encoder = PrettyEncoder(wr); - animal.encode(&encoder); + let mut encoder = PrettyEncoder(wr); + animal.encode(&mut encoder); }, ~"\ [\n \ @@ -1491,15 +2184,15 @@ mod tests { fn test_write_some() { let value = Some(~"jodhpurs"); let s = do io::with_str_writer |wr| { - let encoder = Encoder(wr); - value.encode(&encoder); + let mut encoder = Encoder(wr); + value.encode(&mut encoder); }; assert_eq!(s, ~"\"jodhpurs\""); let value = Some(~"jodhpurs"); let s = do io::with_str_writer |wr| { - let encoder = PrettyEncoder(wr); - value.encode(&encoder); + let mut encoder = PrettyEncoder(wr); + value.encode(&mut encoder); }; assert_eq!(s, ~"\"jodhpurs\""); } @@ -1508,14 +2201,14 @@ mod tests { fn test_write_none() { let value: Option<~str> = None; let s = do io::with_str_writer |wr| { - let encoder = Encoder(wr); - value.encode(&encoder); + let mut encoder = Encoder(wr); + value.encode(&mut encoder); }; assert_eq!(s, ~"null"); let s = do io::with_str_writer |wr| { - let encoder = Encoder(wr); - value.encode(&encoder); + let mut encoder = Encoder(wr); + value.encode(&mut encoder); }; assert_eq!(s, ~"null"); } @@ -1563,13 +2256,16 @@ mod tests { #[test] fn test_decode_identifiers() { - let v: () = Decodable::decode(&Decoder(from_str(~"null").unwrap())); + let mut decoder = Decoder(from_str(~"null").unwrap()); + let v: () = Decodable::decode(&mut decoder); assert_eq!(v, ()); - let v: bool = Decodable::decode(&Decoder(from_str(~"true").unwrap())); + let mut decoder = Decoder(from_str(~"true").unwrap()); + let v: bool = Decodable::decode(&mut decoder); assert_eq!(v, true); - let v: bool = Decodable::decode(&Decoder(from_str(~"false").unwrap())); + let mut decoder = Decoder(from_str(~"false").unwrap()); + let v: bool = Decodable::decode(&mut decoder); assert_eq!(v, false); } @@ -1603,25 +2299,32 @@ mod tests { #[test] fn test_decode_numbers() { - let v: float = Decodable::decode(&Decoder(from_str(~"3").unwrap())); + let mut decoder = Decoder(from_str(~"3").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 3f); - let v: float = Decodable::decode(&Decoder(from_str(~"3.1").unwrap())); + let mut decoder = Decoder(from_str(~"3.1").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 3.1f); - let v: float = Decodable::decode(&Decoder(from_str(~"-1.2").unwrap())); + let mut decoder = Decoder(from_str(~"-1.2").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, -1.2f); - let v: float = Decodable::decode(&Decoder(from_str(~"0.4").unwrap())); + let mut decoder = Decoder(from_str(~"0.4").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 0.4f); - let v: float = Decodable::decode(&Decoder(from_str(~"0.4e5").unwrap())); + let mut decoder = Decoder(from_str(~"0.4e5").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 0.4e5f); - let v: float = Decodable::decode(&Decoder(from_str(~"0.4e15").unwrap())); + let mut decoder = Decoder(from_str(~"0.4e15").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 0.4e15f); - let v: float = Decodable::decode(&Decoder(from_str(~"0.4e-01").unwrap())); + let mut decoder = Decoder(from_str(~"0.4e-01").unwrap()); + let v: float = Decodable::decode(&mut decoder); assert_eq!(v, 0.4e-01f); } @@ -1648,31 +2351,40 @@ mod tests { #[test] fn test_decode_str() { - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~""); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"foo\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"foo\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"foo"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\\"\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\\"\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\""); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\b\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\b\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\x08"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\n\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\n\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\n"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\r\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\r\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\r"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\t\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\t\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\t"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\u12ab\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\u12ab\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\u12ab"); - let v: ~str = Decodable::decode(&Decoder(from_str(~"\"\\uAB12\"").unwrap())); + let mut decoder = Decoder(from_str(~"\"\\uAB12\"").unwrap()); + let v: ~str = Decodable::decode(&mut decoder); assert_eq!(v, ~"\uAB12"); } @@ -1704,23 +2416,28 @@ mod tests { #[test] fn test_decode_list() { - let v: ~[()] = Decodable::decode(&Decoder(from_str(~"[]").unwrap())); + let mut decoder = Decoder(from_str(~"[]").unwrap()); + let v: ~[()] = Decodable::decode(&mut decoder); assert_eq!(v, ~[]); - let v: ~[()] = Decodable::decode(&Decoder(from_str(~"[null]").unwrap())); + let mut decoder = Decoder(from_str(~"[null]").unwrap()); + let v: ~[()] = Decodable::decode(&mut decoder); assert_eq!(v, ~[()]); - - let v: ~[bool] = Decodable::decode(&Decoder(from_str(~"[true]").unwrap())); + let mut decoder = Decoder(from_str(~"[true]").unwrap()); + let v: ~[bool] = Decodable::decode(&mut decoder); assert_eq!(v, ~[true]); - let v: ~[bool] = Decodable::decode(&Decoder(from_str(~"[true]").unwrap())); + let mut decoder = Decoder(from_str(~"[true]").unwrap()); + let v: ~[bool] = Decodable::decode(&mut decoder); assert_eq!(v, ~[true]); - let v: ~[int] = Decodable::decode(&Decoder(from_str(~"[3, 1]").unwrap())); + let mut decoder = Decoder(from_str(~"[3, 1]").unwrap()); + let v: ~[int] = Decodable::decode(&mut decoder); assert_eq!(v, ~[3, 1]); - let v: ~[~[uint]] = Decodable::decode(&Decoder(from_str(~"[[3], [1, 2]]").unwrap())); + let mut decoder = Decoder(from_str(~"[[3], [1, 2]]").unwrap()); + let v: ~[~[uint]] = Decodable::decode(&mut decoder); assert_eq!(v, ~[~[3], ~[1, 2]]); } @@ -1822,7 +2539,8 @@ mod tests { { \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] } ] }"; - let v: Outer = Decodable::decode(&Decoder(from_str(s).unwrap())); + let mut decoder = Decoder(from_str(s).unwrap()); + let v: Outer = Decodable::decode(&mut decoder); assert_eq!( v, Outer { @@ -1835,31 +2553,32 @@ mod tests { #[test] fn test_decode_option() { - let decoder = Decoder(from_str(~"null").unwrap()); - let value: Option<~str> = Decodable::decode(&decoder); + let mut decoder = Decoder(from_str(~"null").unwrap()); + let value: Option<~str> = Decodable::decode(&mut decoder); assert_eq!(value, None); - let decoder = Decoder(from_str(~"\"jodhpurs\"").unwrap()); - let value: Option<~str> = Decodable::decode(&decoder); + let mut decoder = Decoder(from_str(~"\"jodhpurs\"").unwrap()); + let value: Option<~str> = Decodable::decode(&mut decoder); assert_eq!(value, Some(~"jodhpurs")); } #[test] fn test_decode_enum() { - let decoder = Decoder(from_str(~"\"Dog\"").unwrap()); - let value: Animal = Decodable::decode(&decoder); + let mut decoder = Decoder(from_str(~"\"Dog\"").unwrap()); + let value: Animal = Decodable::decode(&mut decoder); assert_eq!(value, Dog); - let decoder = Decoder(from_str(~"[\"Frog\",\"Henry\",349]").unwrap()); - let value: Animal = Decodable::decode(&decoder); + let mut decoder = + Decoder(from_str(~"[\"Frog\",\"Henry\",349]").unwrap()); + let value: Animal = Decodable::decode(&mut decoder); assert_eq!(value, Frog(~"Henry", 349)); } #[test] fn test_decode_map() { let s = ~"{\"a\": \"Dog\", \"b\": [\"Frog\", \"Henry\", 349]}"; - let decoder = Decoder(from_str(s).unwrap()); - let mut map: HashMap<~str, Animal> = Decodable::decode(&decoder); + let mut decoder = Decoder(from_str(s).unwrap()); + let mut map: HashMap<~str, Animal> = Decodable::decode(&mut decoder); assert_eq!(map.pop(&~"a"), Some(Dog)); assert_eq!(map.pop(&~"b"), Some(Frog(~"Henry", 349))); diff --git a/src/libstd/list.rs b/src/libstd/list.rs index 2b6fa0bc056..8d15508b26e 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -16,6 +16,12 @@ pub enum List<T> { Nil, } +#[deriving(Eq)] +pub enum MutList<T> { + MutCons(T, @mut MutList<T>), + MutNil, +} + /// Create a list from a vector pub fn from_vec<T:Copy>(v: &[T]) -> @List<T> { vec::foldr(v, @Nil::<T>, |h, t| @Cons(*h, t)) @@ -147,6 +153,25 @@ pub fn each<T>(l: @List<T>, f: &fn(&T) -> bool) { } } +impl<T> MutList<T> { + /// Iterate over a mutable list + pub fn each(@mut self, f: &fn(&mut T) -> bool) { + let mut cur = self; + loop { + let borrowed = &mut *cur; + cur = match *borrowed { + MutCons(ref mut hd, tl) => { + if !f(hd) { + return; + } + tl + } + MutNil => break + } + } + } +} + #[cfg(test)] mod tests { use list::*; diff --git a/src/libstd/serialize.rs b/src/libstd/serialize.rs index 1ad581ba993..33efb2c6a5a 100644 --- a/src/libstd/serialize.rs +++ b/src/libstd/serialize.rs @@ -25,6 +25,7 @@ use dlist::DList; #[cfg(stage3)] use treemap::{TreeMap, TreeSet}; +#[cfg(stage0)] pub trait Encoder { // Primitive types: fn emit_nil(&self); @@ -48,11 +49,22 @@ pub trait Encoder { // Compound types: fn emit_enum(&self, name: &str, f: &fn()); - fn emit_enum_variant(&self, v_name: &str, v_id: uint, len: uint, f: &fn()); + fn emit_enum_variant(&self, + v_name: &str, + v_id: uint, + len: uint, + f: &fn()); fn emit_enum_variant_arg(&self, a_idx: uint, f: &fn()); - fn emit_enum_struct_variant(&self, v_name: &str, v_id: uint, len: uint, f: &fn()); - fn emit_enum_struct_variant_field(&self, f_name: &str, f_idx: uint, f: &fn()); + fn emit_enum_struct_variant(&self, + v_name: &str, + v_id: uint, + len: uint, + f: &fn()); + fn emit_enum_struct_variant_field(&self, + f_name: &str, + f_idx: uint, + f: &fn()); fn emit_struct(&self, name: &str, len: uint, f: &fn()); #[cfg(stage0)] @@ -81,6 +93,73 @@ pub trait Encoder { fn emit_map_elt_val(&self, idx: uint, f: &fn()); } +#[cfg(not(stage0))] +pub trait Encoder { + // Primitive types: + fn emit_nil(&mut self); + fn emit_uint(&mut self, v: uint); + fn emit_u64(&mut self, v: u64); + fn emit_u32(&mut self, v: u32); + fn emit_u16(&mut self, v: u16); + fn emit_u8(&mut self, v: u8); + fn emit_int(&mut self, v: int); + fn emit_i64(&mut self, v: i64); + fn emit_i32(&mut self, v: i32); + fn emit_i16(&mut self, v: i16); + fn emit_i8(&mut self, v: i8); + fn emit_bool(&mut self, v: bool); + fn emit_float(&mut self, v: float); + fn emit_f64(&mut self, v: f64); + fn emit_f32(&mut self, v: f32); + fn emit_char(&mut self, v: char); + fn emit_str(&mut self, v: &str); + + // Compound types: + fn emit_enum(&mut self, name: &str, f: &fn(&mut Self)); + + fn emit_enum_variant(&mut self, + v_name: &str, + v_id: uint, + len: uint, + f: &fn(&mut Self)); + fn emit_enum_variant_arg(&mut self, a_idx: uint, f: &fn(&mut Self)); + + fn emit_enum_struct_variant(&mut self, + v_name: &str, + v_id: uint, + len: uint, + f: &fn(&mut Self)); + fn emit_enum_struct_variant_field(&mut self, + f_name: &str, + f_idx: uint, + f: &fn(&mut Self)); + + fn emit_struct(&mut self, name: &str, len: uint, f: &fn(&mut Self)); + fn emit_struct_field(&mut self, + f_name: &str, + f_idx: uint, + f: &fn(&mut Self)); + + fn emit_tuple(&mut self, len: uint, f: &fn(&mut Self)); + fn emit_tuple_arg(&mut self, idx: uint, f: &fn(&mut Self)); + + fn emit_tuple_struct(&mut self, name: &str, len: uint, f: &fn(&mut Self)); + fn emit_tuple_struct_arg(&mut self, f_idx: uint, f: &fn(&mut Self)); + + // Specialized types: + fn emit_option(&mut self, f: &fn(&mut Self)); + fn emit_option_none(&mut self); + fn emit_option_some(&mut self, f: &fn(&mut Self)); + + fn emit_seq(&mut self, len: uint, f: &fn(this: &mut Self)); + fn emit_seq_elt(&mut self, idx: uint, f: &fn(this: &mut Self)); + + fn emit_map(&mut self, len: uint, f: &fn(&mut Self)); + fn emit_map_elt_key(&mut self, idx: uint, f: &fn(&mut Self)); + fn emit_map_elt_val(&mut self, idx: uint, f: &fn(&mut Self)); +} + +#[cfg(stage0)] pub trait Decoder { // Primitive types: fn read_nil(&self) -> (); @@ -104,19 +183,37 @@ pub trait Decoder { // Compound types: fn read_enum<T>(&self, name: &str, f: &fn() -> T) -> T; - fn read_enum_variant<T>(&self, names: &[&str], f: &fn(uint) -> T) -> T; + fn read_enum_variant<T>(&self, + names: &[&str], + f: &fn(uint) -> T) + -> T; fn read_enum_variant_arg<T>(&self, a_idx: uint, f: &fn() -> T) -> T; - fn read_enum_struct_variant<T>(&self, names: &[&str], f: &fn(uint) -> T) -> T; - fn read_enum_struct_variant_field<T>(&self, &f_name: &str, f_idx: uint, f: &fn() -> T) -> T; + fn read_enum_struct_variant<T>(&self, + names: &[&str], + f: &fn(uint) -> T) + -> T; + fn read_enum_struct_variant_field<T>(&self, + &f_name: &str, + f_idx: uint, + f: &fn() -> T) + -> T; fn read_struct<T>(&self, s_name: &str, len: uint, f: &fn() -> T) -> T; #[cfg(stage0)] - fn read_field<T>(&self, f_name: &str, f_idx: uint, f: &fn() -> T) -> T; + fn read_field<T>(&self, + f_name: &str, + f_idx: uint, + f: &fn() -> T) + -> T; #[cfg(stage1)] #[cfg(stage2)] #[cfg(stage3)] - fn read_struct_field<T>(&self, f_name: &str, f_idx: uint, f: &fn() -> T) -> T; + fn read_struct_field<T>(&self, + f_name: &str, + f_idx: uint, + f: &fn() -> T) + -> T; fn read_tuple<T>(&self, f: &fn(uint) -> T) -> T; fn read_tuple_arg<T>(&self, a_idx: uint, f: &fn() -> T) -> T; @@ -135,215 +232,673 @@ pub trait Decoder { fn read_map_elt_val<T>(&self, idx: uint, f: &fn() -> T) -> T; } +#[cfg(not(stage0))] +pub trait Decoder { + // Primitive types: + fn read_nil(&mut self) -> (); + fn read_uint(&mut self) -> uint; + fn read_u64(&mut self) -> u64; + fn read_u32(&mut self) -> u32; + fn read_u16(&mut self) -> u16; + fn read_u8(&mut self) -> u8; + fn read_int(&mut self) -> int; + fn read_i64(&mut self) -> i64; + fn read_i32(&mut self) -> i32; + fn read_i16(&mut self) -> i16; + fn read_i8(&mut self) -> i8; + fn read_bool(&mut self) -> bool; + fn read_f64(&mut self) -> f64; + fn read_f32(&mut self) -> f32; + fn read_float(&mut self) -> float; + fn read_char(&mut self) -> char; + fn read_str(&mut self) -> ~str; + + // Compound types: + fn read_enum<T>(&mut self, name: &str, f: &fn(&mut Self) -> T) -> T; + + fn read_enum_variant<T>(&mut self, + names: &[&str], + f: &fn(&mut Self, uint) -> T) + -> T; + fn read_enum_variant_arg<T>(&mut self, + a_idx: uint, + f: &fn(&mut Self) -> T) + -> T; + + fn read_enum_struct_variant<T>(&mut self, + names: &[&str], + f: &fn(&mut Self, uint) -> T) + -> T; + fn read_enum_struct_variant_field<T>(&mut self, + &f_name: &str, + f_idx: uint, + f: &fn(&mut Self) -> T) + -> T; + + fn read_struct<T>(&mut self, + s_name: &str, + len: uint, + f: &fn(&mut Self) -> T) + -> T; + #[cfg(stage0)] + fn read_field<T>(&mut self, + f_name: &str, + f_idx: uint, + f: &fn() -> T) + -> T; + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn read_struct_field<T>(&mut self, + f_name: &str, + f_idx: uint, + f: &fn(&mut Self) -> T) + -> T; + + fn read_tuple<T>(&mut self, f: &fn(&mut Self, uint) -> T) -> T; + fn read_tuple_arg<T>(&mut self, a_idx: uint, f: &fn(&mut Self) -> T) -> T; + + fn read_tuple_struct<T>(&mut self, + s_name: &str, + f: &fn(&mut Self, uint) -> T) + -> T; + fn read_tuple_struct_arg<T>(&mut self, + a_idx: uint, + f: &fn(&mut Self) -> T) + -> T; + + // Specialized types: + fn read_option<T>(&mut self, f: &fn(&mut Self, bool) -> T) -> T; + + fn read_seq<T>(&mut self, f: &fn(&mut Self, uint) -> T) -> T; + fn read_seq_elt<T>(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T; + + fn read_map<T>(&mut self, f: &fn(&mut Self, uint) -> T) -> T; + fn read_map_elt_key<T>(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T; + fn read_map_elt_val<T>(&mut self, idx: uint, f: &fn(&mut Self) -> T) -> T; +} + +#[cfg(stage0)] pub trait Encodable<S:Encoder> { fn encode(&self, s: &S); } +#[cfg(not(stage0))] +pub trait Encodable<S:Encoder> { + fn encode(&self, s: &mut S); +} + +#[cfg(stage0)] pub trait Decodable<D:Decoder> { fn decode(d: &D) -> Self; } +#[cfg(not(stage0))] +pub trait Decodable<D:Decoder> { + fn decode(d: &mut D) -> Self; +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for uint { - fn encode(&self, s: &S) { s.emit_uint(*self) } + fn encode(&self, s: &S) { + s.emit_uint(*self) + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for uint { + fn encode(&self, s: &mut S) { + s.emit_uint(*self) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for uint { fn decode(d: &D) -> uint { d.read_uint() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for uint { + fn decode(d: &mut D) -> uint { + d.read_uint() + } +} + +#[cfg(stage0)] +impl<S:Encoder> Encodable<S> for u8 { + fn encode(&self, s: &S) { + s.emit_u8(*self) + } +} + +#[cfg(not(stage0))] impl<S:Encoder> Encodable<S> for u8 { - fn encode(&self, s: &S) { s.emit_u8(*self) } + fn encode(&self, s: &mut S) { + s.emit_u8(*self) + } } +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for u8 { fn decode(d: &D) -> u8 { d.read_u8() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for u8 { + fn decode(d: &mut D) -> u8 { + d.read_u8() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for u16 { - fn encode(&self, s: &S) { s.emit_u16(*self) } + fn encode(&self, s: &S) { + s.emit_u16(*self) + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for u16 { + fn encode(&self, s: &mut S) { + s.emit_u16(*self) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for u16 { fn decode(d: &D) -> u16 { d.read_u16() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for u16 { + fn decode(d: &mut D) -> u16 { + d.read_u16() + } +} + +#[cfg(stage0)] +impl<S:Encoder> Encodable<S> for u32 { + fn encode(&self, s: &S) { + s.emit_u32(*self) + } +} + +#[cfg(not(stage0))] impl<S:Encoder> Encodable<S> for u32 { - fn encode(&self, s: &S) { s.emit_u32(*self) } + fn encode(&self, s: &mut S) { + s.emit_u32(*self) + } } +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for u32 { fn decode(d: &D) -> u32 { d.read_u32() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for u32 { + fn decode(d: &mut D) -> u32 { + d.read_u32() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for u64 { - fn encode(&self, s: &S) { s.emit_u64(*self) } + fn encode(&self, s: &S) { + s.emit_u64(*self) + } +} + +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for u64 { + fn encode(&self, s: &mut S) { + s.emit_u64(*self) + } } +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for u64 { fn decode(d: &D) -> u64 { d.read_u64() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for u64 { + fn decode(d: &mut D) -> u64 { + d.read_u64() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for int { - fn encode(&self, s: &S) { s.emit_int(*self) } + fn encode(&self, s: &S) { + s.emit_int(*self) + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for int { + fn encode(&self, s: &mut S) { + s.emit_int(*self) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for int { fn decode(d: &D) -> int { d.read_int() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for int { + fn decode(d: &mut D) -> int { + d.read_int() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for i8 { - fn encode(&self, s: &S) { s.emit_i8(*self) } + fn encode(&self, s: &S) { + s.emit_i8(*self) + } +} + +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for i8 { + fn encode(&self, s: &mut S) { + s.emit_i8(*self) + } } +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for i8 { fn decode(d: &D) -> i8 { d.read_i8() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for i8 { + fn decode(d: &mut D) -> i8 { + d.read_i8() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for i16 { - fn encode(&self, s: &S) { s.emit_i16(*self) } + fn encode(&self, s: &S) { + s.emit_i16(*self) + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for i16 { + fn encode(&self, s: &mut S) { + s.emit_i16(*self) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for i16 { fn decode(d: &D) -> i16 { d.read_i16() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for i16 { + fn decode(d: &mut D) -> i16 { + d.read_i16() + } +} + +#[cfg(stage0)] +impl<S:Encoder> Encodable<S> for i32 { + fn encode(&self, s: &S) { + s.emit_i32(*self) + } +} + +#[cfg(not(stage0))] impl<S:Encoder> Encodable<S> for i32 { - fn encode(&self, s: &S) { s.emit_i32(*self) } + fn encode(&self, s: &mut S) { + s.emit_i32(*self) + } } +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for i32 { fn decode(d: &D) -> i32 { d.read_i32() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for i32 { + fn decode(d: &mut D) -> i32 { + d.read_i32() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for i64 { - fn encode(&self, s: &S) { s.emit_i64(*self) } + fn encode(&self, s: &S) { + s.emit_i64(*self) + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for i64 { + fn encode(&self, s: &mut S) { + s.emit_i64(*self) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for i64 { fn decode(d: &D) -> i64 { d.read_i64() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for i64 { + fn decode(d: &mut D) -> i64 { + d.read_i64() + } +} + +#[cfg(stage0)] impl<'self, S:Encoder> Encodable<S> for &'self str { - fn encode(&self, s: &S) { s.emit_str(*self) } + fn encode(&self, s: &S) { + s.emit_str(*self) + } } +#[cfg(not(stage0))] +impl<'self, S:Encoder> Encodable<S> for &'self str { + fn encode(&self, s: &mut S) { + s.emit_str(*self) + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for ~str { - fn encode(&self, s: &S) { s.emit_str(*self) } + fn encode(&self, s: &S) { + s.emit_str(*self) + } +} + +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for ~str { + fn encode(&self, s: &mut S) { + s.emit_str(*self) + } } +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for ~str { fn decode(d: &D) -> ~str { d.read_str() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for ~str { + fn decode(d: &mut D) -> ~str { + d.read_str() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for @str { - fn encode(&self, s: &S) { s.emit_str(*self) } + fn encode(&self, s: &S) { + s.emit_str(*self) + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for @str { + fn encode(&self, s: &mut S) { + s.emit_str(*self) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for @str { - fn decode(d: &D) -> @str { d.read_str().to_managed() } + fn decode(d: &D) -> @str { + d.read_str().to_managed() + } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for @str { + fn decode(d: &mut D) -> @str { + d.read_str().to_managed() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for float { - fn encode(&self, s: &S) { s.emit_float(*self) } + fn encode(&self, s: &S) { + s.emit_float(*self) + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for float { + fn encode(&self, s: &mut S) { + s.emit_float(*self) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for float { fn decode(d: &D) -> float { d.read_float() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for float { + fn decode(d: &mut D) -> float { + d.read_float() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for f32 { - fn encode(&self, s: &S) { s.emit_f32(*self) } + fn encode(&self, s: &S) { + s.emit_f32(*self) + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for f32 { + fn encode(&self, s: &mut S) { + s.emit_f32(*self) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for f32 { fn decode(d: &D) -> f32 { - d.read_f32() } + d.read_f32() + } +} + +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for f32 { + fn decode(d: &mut D) -> f32 { + d.read_f32() + } +} + +#[cfg(stage0)] +impl<S:Encoder> Encodable<S> for f64 { + fn encode(&self, s: &S) { + s.emit_f64(*self) + } } +#[cfg(not(stage0))] impl<S:Encoder> Encodable<S> for f64 { - fn encode(&self, s: &S) { s.emit_f64(*self) } + fn encode(&self, s: &mut S) { + s.emit_f64(*self) + } } +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for f64 { fn decode(d: &D) -> f64 { d.read_f64() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for f64 { + fn decode(d: &mut D) -> f64 { + d.read_f64() + } +} + +#[cfg(stage0)] +impl<S:Encoder> Encodable<S> for bool { + fn encode(&self, s: &S) { + s.emit_bool(*self) + } +} + +#[cfg(not(stage0))] impl<S:Encoder> Encodable<S> for bool { - fn encode(&self, s: &S) { s.emit_bool(*self) } + fn encode(&self, s: &mut S) { + s.emit_bool(*self) + } } +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for bool { fn decode(d: &D) -> bool { d.read_bool() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for bool { + fn decode(d: &mut D) -> bool { + d.read_bool() + } +} + +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for () { - fn encode(&self, s: &S) { s.emit_nil() } + fn encode(&self, s: &S) { + s.emit_nil() + } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for () { + fn encode(&self, s: &mut S) { + s.emit_nil() + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for () { fn decode(d: &D) -> () { d.read_nil() } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for () { + fn decode(d: &mut D) -> () { + d.read_nil() + } +} + +#[cfg(stage0)] impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self T { fn encode(&self, s: &S) { (**self).encode(s) } } +#[cfg(not(stage0))] +impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self T { + fn encode(&self, s: &mut S) { + (**self).encode(s) + } +} + +#[cfg(stage0)] impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~T { fn encode(&self, s: &S) { (**self).encode(s) } } +#[cfg(not(stage0))] +impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~T { + fn encode(&self, s: &mut S) { + (**self).encode(s) + } +} + +#[cfg(stage0)] impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~T { fn decode(d: &D) -> ~T { ~Decodable::decode(d) } } +#[cfg(not(stage0))] +impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~T { + fn decode(d: &mut D) -> ~T { + ~Decodable::decode(d) + } +} + +#[cfg(stage0)] impl<S:Encoder,T:Encodable<S>> Encodable<S> for @T { fn encode(&self, s: &S) { (**self).encode(s) } } +#[cfg(not(stage0))] +impl<S:Encoder,T:Encodable<S>> Encodable<S> for @T { + fn encode(&self, s: &mut S) { + (**self).encode(s) + } +} + +#[cfg(stage0)] impl<D:Decoder,T:Decodable<D>> Decodable<D> for @T { fn decode(d: &D) -> @T { @Decodable::decode(d) } } +#[cfg(not(stage0))] +impl<D:Decoder,T:Decodable<D>> Decodable<D> for @T { + fn decode(d: &mut D) -> @T { + @Decodable::decode(d) + } +} + +#[cfg(stage0)] impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self [T] { fn encode(&self, s: &S) { do s.emit_seq(self.len()) { @@ -354,6 +909,18 @@ impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self [T] { } } +#[cfg(not(stage0))] +impl<'self, S:Encoder,T:Encodable<S>> Encodable<S> for &'self [T] { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + for self.eachi |i, e| { + s.emit_seq_elt(i, |s| e.encode(s)) + } + } + } +} + +#[cfg(stage0)] impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~[T] { fn encode(&self, s: &S) { do s.emit_seq(self.len()) { @@ -364,6 +931,18 @@ impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~[T] { } } +#[cfg(not(stage0))] +impl<S:Encoder,T:Encodable<S>> Encodable<S> for ~[T] { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + for self.eachi |i, e| { + s.emit_seq_elt(i, |s| e.encode(s)) + } + } + } +} + +#[cfg(stage0)] impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~[T] { fn decode(d: &D) -> ~[T] { do d.read_seq |len| { @@ -374,6 +953,18 @@ impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~[T] { } } +#[cfg(not(stage0))] +impl<D:Decoder,T:Decodable<D>> Decodable<D> for ~[T] { + fn decode(d: &mut D) -> ~[T] { + do d.read_seq |d, len| { + do vec::from_fn(len) |i| { + d.read_seq_elt(i, |d| Decodable::decode(d)) + } + } + } +} + +#[cfg(stage0)] impl<S:Encoder,T:Encodable<S>> Encodable<S> for @[T] { fn encode(&self, s: &S) { do s.emit_seq(self.len()) { @@ -384,6 +975,18 @@ impl<S:Encoder,T:Encodable<S>> Encodable<S> for @[T] { } } +#[cfg(not(stage0))] +impl<S:Encoder,T:Encodable<S>> Encodable<S> for @[T] { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + for self.eachi |i, e| { + s.emit_seq_elt(i, |s| e.encode(s)) + } + } + } +} + +#[cfg(stage0)] impl<D:Decoder,T:Decodable<D>> Decodable<D> for @[T] { fn decode(d: &D) -> @[T] { do d.read_seq |len| { @@ -394,6 +997,18 @@ impl<D:Decoder,T:Decodable<D>> Decodable<D> for @[T] { } } +#[cfg(not(stage0))] +impl<D:Decoder,T:Decodable<D>> Decodable<D> for @[T] { + fn decode(d: &mut D) -> @[T] { + do d.read_seq |d, len| { + do at_vec::from_fn(len) |i| { + d.read_seq_elt(i, |d| Decodable::decode(d)) + } + } + } +} + +#[cfg(stage0)] impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> { fn encode(&self, s: &S) { do s.emit_option { @@ -405,6 +1020,19 @@ impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> { } } +#[cfg(not(stage0))] +impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> { + fn encode(&self, s: &mut S) { + do s.emit_option |s| { + match *self { + None => s.emit_option_none(), + Some(ref v) => s.emit_option_some(|s| v.encode(s)), + } + } + } +} + +#[cfg(stage0)] impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> { fn decode(d: &D) -> Option<T> { do d.read_option |b| { @@ -417,6 +1045,20 @@ impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> { } } +#[cfg(not(stage0))] +impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> { + fn decode(d: &mut D) -> Option<T> { + do d.read_option |d, b| { + if b { + Some(Decodable::decode(d)) + } else { + None + } + } + } +} + +#[cfg(stage0)] impl<S:Encoder,T0:Encodable<S>,T1:Encodable<S>> Encodable<S> for (T0, T1) { fn encode(&self, s: &S) { match *self { @@ -430,6 +1072,21 @@ impl<S:Encoder,T0:Encodable<S>,T1:Encodable<S>> Encodable<S> for (T0, T1) { } } +#[cfg(not(stage0))] +impl<S:Encoder,T0:Encodable<S>,T1:Encodable<S>> Encodable<S> for (T0, T1) { + fn encode(&self, s: &mut S) { + match *self { + (ref t0, ref t1) => { + do s.emit_seq(2) |s| { + s.emit_seq_elt(0, |s| t0.encode(s)); + s.emit_seq_elt(1, |s| t1.encode(s)); + } + } + } + } +} + +#[cfg(stage0)] impl<D:Decoder,T0:Decodable<D>,T1:Decodable<D>> Decodable<D> for (T0, T1) { fn decode(d: &D) -> (T0, T1) { do d.read_seq |len| { @@ -442,6 +1099,20 @@ impl<D:Decoder,T0:Decodable<D>,T1:Decodable<D>> Decodable<D> for (T0, T1) { } } +#[cfg(not(stage0))] +impl<D:Decoder,T0:Decodable<D>,T1:Decodable<D>> Decodable<D> for (T0, T1) { + fn decode(d: &mut D) -> (T0, T1) { + do d.read_seq |d, len| { + assert!(len == 2); + ( + d.read_seq_elt(0, |d| Decodable::decode(d)), + d.read_seq_elt(1, |d| Decodable::decode(d)) + ) + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T0: Encodable<S>, @@ -461,6 +1132,27 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T0: Encodable<S>, + T1: Encodable<S>, + T2: Encodable<S> +> Encodable<S> for (T0, T1, T2) { + fn encode(&self, s: &mut S) { + match *self { + (ref t0, ref t1, ref t2) => { + do s.emit_seq(3) |s| { + s.emit_seq_elt(0, |s| t0.encode(s)); + s.emit_seq_elt(1, |s| t1.encode(s)); + s.emit_seq_elt(2, |s| t2.encode(s)); + } + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, T0: Decodable<D>, @@ -479,6 +1171,26 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + T0: Decodable<D>, + T1: Decodable<D>, + T2: Decodable<D> +> Decodable<D> for (T0, T1, T2) { + fn decode(d: &mut D) -> (T0, T1, T2) { + do d.read_seq |d, len| { + assert!(len == 3); + ( + d.read_seq_elt(0, |d| Decodable::decode(d)), + d.read_seq_elt(1, |d| Decodable::decode(d)), + d.read_seq_elt(2, |d| Decodable::decode(d)) + ) + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T0: Encodable<S>, @@ -500,6 +1212,29 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T0: Encodable<S>, + T1: Encodable<S>, + T2: Encodable<S>, + T3: Encodable<S> +> Encodable<S> for (T0, T1, T2, T3) { + fn encode(&self, s: &mut S) { + match *self { + (ref t0, ref t1, ref t2, ref t3) => { + do s.emit_seq(4) |s| { + s.emit_seq_elt(0, |s| t0.encode(s)); + s.emit_seq_elt(1, |s| t1.encode(s)); + s.emit_seq_elt(2, |s| t2.encode(s)); + s.emit_seq_elt(3, |s| t3.encode(s)); + } + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, T0: Decodable<D>, @@ -520,6 +1255,28 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + T0: Decodable<D>, + T1: Decodable<D>, + T2: Decodable<D>, + T3: Decodable<D> +> Decodable<D> for (T0, T1, T2, T3) { + fn decode(d: &mut D) -> (T0, T1, T2, T3) { + do d.read_seq |d, len| { + assert!(len == 4); + ( + d.read_seq_elt(0, |d| Decodable::decode(d)), + d.read_seq_elt(1, |d| Decodable::decode(d)), + d.read_seq_elt(2, |d| Decodable::decode(d)), + d.read_seq_elt(3, |d| Decodable::decode(d)) + ) + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T0: Encodable<S>, @@ -543,6 +1300,31 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T0: Encodable<S>, + T1: Encodable<S>, + T2: Encodable<S>, + T3: Encodable<S>, + T4: Encodable<S> +> Encodable<S> for (T0, T1, T2, T3, T4) { + fn encode(&self, s: &mut S) { + match *self { + (ref t0, ref t1, ref t2, ref t3, ref t4) => { + do s.emit_seq(5) |s| { + s.emit_seq_elt(0, |s| t0.encode(s)); + s.emit_seq_elt(1, |s| t1.encode(s)); + s.emit_seq_elt(2, |s| t2.encode(s)); + s.emit_seq_elt(3, |s| t3.encode(s)); + s.emit_seq_elt(4, |s| t4.encode(s)); + } + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, T0: Decodable<D>, @@ -551,8 +1333,7 @@ impl< T3: Decodable<D>, T4: Decodable<D> > Decodable<D> for (T0, T1, T2, T3, T4) { - fn decode(d: &D) - -> (T0, T1, T2, T3, T4) { + fn decode(d: &D) -> (T0, T1, T2, T3, T4) { do d.read_seq |len| { assert!(len == 5); ( @@ -566,6 +1347,30 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + T0: Decodable<D>, + T1: Decodable<D>, + T2: Decodable<D>, + T3: Decodable<D>, + T4: Decodable<D> +> Decodable<D> for (T0, T1, T2, T3, T4) { + fn decode(d: &mut D) -> (T0, T1, T2, T3, T4) { + do d.read_seq |d, len| { + assert!(len == 5); + ( + d.read_seq_elt(0, |d| Decodable::decode(d)), + d.read_seq_elt(1, |d| Decodable::decode(d)), + d.read_seq_elt(2, |d| Decodable::decode(d)), + d.read_seq_elt(3, |d| Decodable::decode(d)), + d.read_seq_elt(4, |d| Decodable::decode(d)) + ) + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T: Encodable<S> + Copy @@ -581,6 +1386,23 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T: Encodable<S> + Copy +> Encodable<S> for @mut DList<T> { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.size) |s| { + let mut i = 0; + for self.each |e| { + s.emit_seq_elt(i, |s| e.encode(s)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl<D:Decoder,T:Decodable<D>> Decodable<D> for @mut DList<T> { fn decode(d: &D) -> @mut DList<T> { let list = DList(); @@ -593,6 +1415,20 @@ impl<D:Decoder,T:Decodable<D>> Decodable<D> for @mut DList<T> { } } +#[cfg(not(stage0))] +impl<D:Decoder,T:Decodable<D>> Decodable<D> for @mut DList<T> { + fn decode(d: &mut D) -> @mut DList<T> { + let list = DList(); + do d.read_seq |d, len| { + for uint::range(0, len) |i| { + list.push(d.read_seq_elt(i, |d| Decodable::decode(d))); + } + } + list + } +} + +#[cfg(stage0)] impl< S: Encoder, T: Encodable<S> @@ -606,6 +1442,21 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T: Encodable<S> +> Encodable<S> for Deque<T> { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + for self.eachi |i, e| { + s.emit_seq_elt(i, |s| e.encode(s)); + } + } + } +} + +#[cfg(stage0)] impl<D:Decoder,T:Decodable<D>> Decodable<D> for Deque<T> { fn decode(d: &D) -> Deque<T> { let mut deque = Deque::new(); @@ -618,6 +1469,20 @@ impl<D:Decoder,T:Decodable<D>> Decodable<D> for Deque<T> { } } +#[cfg(not(stage0))] +impl<D:Decoder,T:Decodable<D>> Decodable<D> for Deque<T> { + fn decode(d: &mut D) -> Deque<T> { + let mut deque = Deque::new(); + do d.read_seq |d, len| { + for uint::range(0, len) |i| { + deque.add_back(d.read_seq_elt(i, |d| Decodable::decode(d))); + } + } + deque + } +} + +#[cfg(stage0)] impl< E: Encoder, K: Encodable<E> + Hash + IterBytes + Eq, @@ -635,6 +1500,25 @@ impl< } } +#[cfg(not(stage0))] +impl< + E: Encoder, + K: Encodable<E> + Hash + IterBytes + Eq, + V: Encodable<E> +> Encodable<E> for HashMap<K, V> { + fn encode(&self, e: &mut E) { + do e.emit_map(self.len()) |e| { + let mut i = 0; + for self.each |key, val| { + e.emit_map_elt_key(i, |e| key.encode(e)); + e.emit_map_elt_val(i, |e| val.encode(e)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, K: Decodable<D> + Hash + IterBytes + Eq, @@ -653,6 +1537,26 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + K: Decodable<D> + Hash + IterBytes + Eq, + V: Decodable<D> +> Decodable<D> for HashMap<K, V> { + fn decode(d: &mut D) -> HashMap<K, V> { + do d.read_map |d, len| { + let mut map = HashMap::with_capacity(len); + for uint::range(0, len) |i| { + let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); + let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); + map.insert(key, val); + } + map + } + } +} + +#[cfg(stage0)] impl< S: Encoder, T: Encodable<S> + Hash + IterBytes + Eq @@ -668,6 +1572,23 @@ impl< } } +#[cfg(not(stage0))] +impl< + S: Encoder, + T: Encodable<S> + Hash + IterBytes + Eq +> Encodable<S> for HashSet<T> { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + let mut i = 0; + for self.each |e| { + s.emit_seq_elt(i, |s| e.encode(s)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, T: Decodable<D> + Hash + IterBytes + Eq @@ -683,6 +1604,23 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + T: Decodable<D> + Hash + IterBytes + Eq +> Decodable<D> for HashSet<T> { + fn decode(d: &mut D) -> HashSet<T> { + do d.read_seq |d, len| { + let mut set = HashSet::with_capacity(len); + for uint::range(0, len) |i| { + set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); + } + set + } + } +} + +#[cfg(stage0)] impl< E: Encoder, V: Encodable<E> @@ -699,6 +1637,24 @@ impl< } } +#[cfg(not(stage0))] +impl< + E: Encoder, + V: Encodable<E> +> Encodable<E> for TrieMap<V> { + fn encode(&self, e: &mut E) { + do e.emit_map(self.len()) |e| { + let mut i = 0; + for self.each |key, val| { + e.emit_map_elt_key(i, |e| key.encode(e)); + e.emit_map_elt_val(i, |e| val.encode(e)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl< D: Decoder, V: Decodable<D> @@ -716,6 +1672,25 @@ impl< } } +#[cfg(not(stage0))] +impl< + D: Decoder, + V: Decodable<D> +> Decodable<D> for TrieMap<V> { + fn decode(d: &mut D) -> TrieMap<V> { + do d.read_map |d, len| { + let mut map = TrieMap::new(); + for uint::range(0, len) |i| { + let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); + let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); + map.insert(key, val); + } + map + } + } +} + +#[cfg(stage0)] impl<S: Encoder> Encodable<S> for TrieSet { fn encode(&self, s: &S) { do s.emit_seq(self.len()) { @@ -728,6 +1703,20 @@ impl<S: Encoder> Encodable<S> for TrieSet { } } +#[cfg(not(stage0))] +impl<S: Encoder> Encodable<S> for TrieSet { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { + let mut i = 0; + for self.each |e| { + s.emit_seq_elt(i, |s| e.encode(s)); + i += 1; + } + } + } +} + +#[cfg(stage0)] impl<D: Decoder> Decodable<D> for TrieSet { fn decode(d: &D) -> TrieSet { do d.read_seq |len| { @@ -740,40 +1729,49 @@ impl<D: Decoder> Decodable<D> for TrieSet { } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] +impl<D: Decoder> Decodable<D> for TrieSet { + fn decode(d: &mut D) -> TrieSet { + do d.read_seq |d, len| { + let mut set = TrieSet::new(); + for uint::range(0, len) |i| { + set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); + } + set + } + } +} + +#[cfg(not(stage0))] impl< E: Encoder, K: Encodable<E> + Eq + TotalOrd, V: Encodable<E> + Eq > Encodable<E> for TreeMap<K, V> { - fn encode(&self, e: &E) { - do e.emit_map(self.len()) { + fn encode(&self, e: &mut E) { + do e.emit_map(self.len()) |e| { let mut i = 0; for self.each |key, val| { - e.emit_map_elt_key(i, || key.encode(e)); - e.emit_map_elt_val(i, || val.encode(e)); + e.emit_map_elt_key(i, |e| key.encode(e)); + e.emit_map_elt_val(i, |e| val.encode(e)); i += 1; } } } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] impl< D: Decoder, K: Decodable<D> + Eq + TotalOrd, V: Decodable<D> + Eq > Decodable<D> for TreeMap<K, V> { - fn decode(d: &D) -> TreeMap<K, V> { - do d.read_map |len| { + fn decode(d: &mut D) -> TreeMap<K, V> { + do d.read_map |d, len| { let mut map = TreeMap::new(); for uint::range(0, len) |i| { - let key = d.read_map_elt_key(i, || Decodable::decode(d)); - let val = d.read_map_elt_val(i, || Decodable::decode(d)); + let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); + let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); map.insert(key, val); } map @@ -781,36 +1779,32 @@ impl< } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] impl< S: Encoder, T: Encodable<S> + Eq + TotalOrd > Encodable<S> for TreeSet<T> { - fn encode(&self, s: &S) { - do s.emit_seq(self.len()) { + fn encode(&self, s: &mut S) { + do s.emit_seq(self.len()) |s| { let mut i = 0; for self.each |e| { - s.emit_seq_elt(i, || e.encode(s)); + s.emit_seq_elt(i, |s| e.encode(s)); i += 1; } } } } -#[cfg(stage1)] -#[cfg(stage2)] -#[cfg(stage3)] +#[cfg(not(stage0))] impl< D: Decoder, T: Decodable<D> + Eq + TotalOrd > Decodable<D> for TreeSet<T> { - fn decode(d: &D) -> TreeSet<T> { - do d.read_seq |len| { + fn decode(d: &mut D) -> TreeSet<T> { + do d.read_seq |d, len| { let mut set = TreeSet::new(); for uint::range(0, len) |i| { - set.insert(d.read_seq_elt(i, || Decodable::decode(d))); + set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); } set } @@ -822,10 +1816,17 @@ impl< // // In some cases, these should eventually be coded as traits. +#[cfg(stage0)] pub trait EncoderHelpers { fn emit_from_vec<T>(&self, v: &[T], f: &fn(v: &T)); } +#[cfg(not(stage0))] +pub trait EncoderHelpers { + fn emit_from_vec<T>(&mut self, v: &[T], f: &fn(&mut Self, v: &T)); +} + +#[cfg(stage0)] impl<S:Encoder> EncoderHelpers for S { fn emit_from_vec<T>(&self, v: &[T], f: &fn(v: &T)) { do self.emit_seq(v.len()) { @@ -838,10 +1839,30 @@ impl<S:Encoder> EncoderHelpers for S { } } +#[cfg(not(stage0))] +impl<S:Encoder> EncoderHelpers for S { + fn emit_from_vec<T>(&mut self, v: &[T], f: &fn(&mut S, &T)) { + do self.emit_seq(v.len()) |this| { + for v.eachi |i, e| { + do this.emit_seq_elt(i) |this| { + f(this, e) + } + } + } + } +} + +#[cfg(stage0)] pub trait DecoderHelpers { fn read_to_vec<T>(&self, f: &fn() -> T) -> ~[T]; } +#[cfg(not(stage0))] +pub trait DecoderHelpers { + fn read_to_vec<T>(&mut self, f: &fn(&mut Self) -> T) -> ~[T]; +} + +#[cfg(stage0)] impl<D:Decoder> DecoderHelpers for D { fn read_to_vec<T>(&self, f: &fn() -> T) -> ~[T] { do self.read_seq |len| { @@ -851,3 +1872,15 @@ impl<D:Decoder> DecoderHelpers for D { } } } + +#[cfg(not(stage0))] +impl<D:Decoder> DecoderHelpers for D { + fn read_to_vec<T>(&mut self, f: &fn(&mut D) -> T) -> ~[T] { + do self.read_seq |this, len| { + do vec::from_fn(len) |i| { + this.read_seq_elt(i, |this| f(this)) + } + } + } +} + diff --git a/src/libstd/workcache.rs b/src/libstd/workcache.rs index bb4a9e97ea1..2cdf36c71c7 100644 --- a/src/libstd/workcache.rs +++ b/src/libstd/workcache.rs @@ -140,6 +140,7 @@ impl WorkMap { fn new() -> WorkMap { WorkMap(HashMap::new()) } } +#[cfg(stage0)] impl<S:Encoder> Encodable<S> for WorkMap { fn encode(&self, s: &S) { let mut d = ~[]; @@ -151,6 +152,19 @@ impl<S:Encoder> Encodable<S> for WorkMap { } } +#[cfg(not(stage0))] +impl<S:Encoder> Encodable<S> for WorkMap { + fn encode(&self, s: &mut S) { + let mut d = ~[]; + for self.each |k, v| { + d.push((copy *k, copy *v)) + } + sort::tim_sort(d); + d.encode(s) + } +} + +#[cfg(stage0)] impl<D:Decoder> Decodable<D> for WorkMap { fn decode(d: &D) -> WorkMap { let v : ~[(WorkKey,~str)] = Decodable::decode(d); @@ -162,6 +176,18 @@ impl<D:Decoder> Decodable<D> for WorkMap { } } +#[cfg(not(stage0))] +impl<D:Decoder> Decodable<D> for WorkMap { + fn decode(d: &mut D) -> WorkMap { + let v : ~[(WorkKey,~str)] = Decodable::decode(d); + let mut w = WorkMap::new(); + for v.each |&(k, v)| { + w.insert(copy k, copy v); + } + w + } +} + struct Database { db_filename: Path, db_cache: HashMap<~str, ~str>, @@ -171,8 +197,8 @@ struct Database { pub impl Database { fn prepare(&mut self, fn_name: &str, - declared_inputs: &WorkMap) -> Option<(WorkMap, WorkMap, ~str)> - { + declared_inputs: &WorkMap) + -> Option<(WorkMap, WorkMap, ~str)> { let k = json_encode(&(fn_name, declared_inputs)); match self.db_cache.find(&k) { None => None, @@ -229,17 +255,38 @@ struct Work<T> { res: Option<Either<T,PortOne<(Exec,T)>>> } +#[cfg(stage0)] fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str { do io::with_str_writer |wr| { t.encode(&json::Encoder(wr)); } } +#[cfg(not(stage0))] +fn json_encode<T:Encodable<json::Encoder>>(t: &T) -> ~str { + do io::with_str_writer |wr| { + let mut encoder = json::Encoder(wr); + t.encode(&mut encoder); + } +} + +// FIXME(#5121) +#[cfg(stage0)] +fn json_decode<T:Decodable<json::Decoder>>(s: &str) -> T { + do io::with_str_reader(s) |rdr| { + let j = result::unwrap(json::from_reader(rdr)); + let decoder = json::Decoder(j); + Decodable::decode(&decoder) + } +} + // FIXME(#5121) +#[cfg(not(stage0))] fn json_decode<T:Decodable<json::Decoder>>(s: &str) -> T { do io::with_str_reader(s) |rdr| { let j = result::unwrap(json::from_reader(rdr)); - Decodable::decode(&json::Decoder(j)) + let mut decoder = json::Decoder(j); + Decodable::decode(&mut decoder) } } |
