From 8b43c620b971bc3cbae61ca8390ec34355add445 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 28 Mar 2013 20:51:05 -0700 Subject: std: remove Encoder::emit_{owned,managed}_str and Decoder::read_{owned,managed}_str --- src/libsyntax/ast.rs | 4 ++-- src/libsyntax/ext/auto_encode.rs | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index c2f098f3c3e..78debbba31c 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -70,7 +70,7 @@ impl Encodable for ident { Some(intr) => intr }; - s.emit_owned_str(*(*intr).get(*self)); + s.emit_str(*(*intr).get(*self)); } } @@ -83,7 +83,7 @@ impl Decodable for ident { Some(intr) => intr }; - (*intr).intern(@d.read_owned_str()) + (*intr).intern(@d.read_str()) } } diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index d7e0e9aa550..54f9967e64a 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -1240,10 +1240,7 @@ mod test { fn emit_float(&self, +_v: float) { self.add_unknown_to_log(); } fn emit_char(&self, +_v: char) { self.add_unknown_to_log(); } - - fn emit_borrowed_str(&self, +_v: &str) { self.add_unknown_to_log(); } - fn emit_owned_str(&self, +_v: &str) { self.add_unknown_to_log(); } - fn emit_managed_str(&self, +_v: &str) { self.add_unknown_to_log(); } + fn emit_str(&self, +_v: &str) { self.add_unknown_to_log(); } fn emit_borrowed(&self, f: &fn()) { self.add_unknown_to_log(); f() } fn emit_owned(&self, f: &fn()) { self.add_unknown_to_log(); f() } -- cgit 1.4.1-3-g733a5 From 63fc88757f9742b763114deac75962cb897b8218 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 28 Mar 2013 20:51:40 -0700 Subject: std: remove Encoder::emit_{owned,managed} and Decoder::read_{owned,managed} --- src/libstd/ebml.rs | 14 -------------- src/libstd/json.rs | 4 ---- src/libstd/serialize.rs | 16 +++++----------- src/libsyntax/ext/auto_encode.rs | 4 ---- 4 files changed, 5 insertions(+), 33 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index 513897997a5..fccb707ed1f 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -315,16 +315,6 @@ pub mod reader { fn read_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) } // Compound types: - fn read_owned(&self, f: &fn() -> T) -> T { - debug!("read_owned()"); - f() - } - - fn read_managed(&self, f: &fn() -> T) -> T { - debug!("read_managed()"); - f() - } - fn read_enum(&self, name: &str, f: &fn() -> T) -> T { debug!("read_enum(%s)", name); self._check_label(name); @@ -651,10 +641,6 @@ pub mod writer { self.wr_tagged_str(EsStr as uint, v) } - fn emit_borrowed(&self, f: &fn()) { f() } - fn emit_owned(&self, f: &fn()) { f() } - fn emit_managed(&self, f: &fn()) { f() } - fn emit_enum(&self, name: &str, f: &fn()) { self._emit_label(name); self.wr_tag(EsEnum as uint, f) diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 373131e9e65..c936f8e4dcd 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -108,10 +108,6 @@ impl serialize::Encoder for Encoder { fn emit_char(&self, v: char) { self.emit_str(str::from_char(v)) } fn emit_str(&self, v: &str) { self.wr.write_str(escape_str(v)) } - fn emit_borrowed(&self, f: &fn()) { f() } - fn emit_owned(&self, f: &fn()) { f() } - fn emit_managed(&self, f: &fn()) { f() } - fn emit_enum(&self, _name: &str, f: &fn()) { f() } fn emit_enum_variant(&self, name: &str, _id: uint, cnt: uint, f: &fn()) { // enums are encoded as strings or vectors: diff --git a/src/libstd/serialize.rs b/src/libstd/serialize.rs index daf834dad2e..42a9e24b1cb 100644 --- a/src/libstd/serialize.rs +++ b/src/libstd/serialize.rs @@ -41,9 +41,6 @@ pub trait Encoder { fn emit_str(&self, v: &str); // Compound types: - fn emit_borrowed(&self, f: &fn()); - fn emit_owned(&self, f: &fn()); - fn emit_managed(&self, f: &fn()); fn emit_enum(&self, name: &str, f: &fn()); fn emit_enum_variant(&self, v_name: &str, v_id: uint, sz: uint, f: &fn()); @@ -97,9 +94,6 @@ pub trait Decoder { fn read_enum_variant(&self, names: &[&str], f: &fn(uint) -> T) -> T; fn read_enum_variant_arg(&self, idx: uint, f: &fn() -> T) -> T; - fn read_owned(&self, f: &fn() -> T) -> T; - fn read_managed(&self, f: &fn() -> T) -> T; - fn read_owned_vec(&self, f: &fn(uint) -> T) -> T; fn read_managed_vec(&self, f: &fn(uint) -> T) -> T; fn read_vec_elt(&self, idx: uint, f: &fn() -> T) -> T; @@ -296,31 +290,31 @@ impl Decodable for () { impl<'self, S:Encoder,T:Encodable> Encodable for &'self T { fn encode(&self, s: &S) { - s.emit_borrowed(|| (**self).encode(s)) + (**self).encode(s) } } impl> Encodable for ~T { fn encode(&self, s: &S) { - s.emit_owned(|| (**self).encode(s)) + (**self).encode(s) } } impl> Decodable for ~T { fn decode(d: &D) -> ~T { - d.read_owned(|| ~Decodable::decode(d)) + ~Decodable::decode(d) } } impl> Encodable for @T { fn encode(&self, s: &S) { - s.emit_managed(|| (**self).encode(s)) + (**self).encode(s) } } impl> Decodable for @T { fn decode(d: &D) -> @T { - d.read_managed(|| @Decodable::decode(d)) + @Decodable::decode(d) } } diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index 54f9967e64a..c9357c9d867 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -1242,10 +1242,6 @@ mod test { fn emit_char(&self, +_v: char) { self.add_unknown_to_log(); } fn emit_str(&self, +_v: &str) { self.add_unknown_to_log(); } - fn emit_borrowed(&self, f: &fn()) { self.add_unknown_to_log(); f() } - fn emit_owned(&self, f: &fn()) { self.add_unknown_to_log(); f() } - fn emit_managed(&self, f: &fn()) { self.add_unknown_to_log(); f() } - fn emit_enum(&self, name: &str, f: &fn()) { self.add_to_log(CallToEmitEnum(name.to_str())); f(); } -- cgit 1.4.1-3-g733a5 From 590bbcebe9401293e642c89a6a623f5461b96dde Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 28 Mar 2013 21:34:47 -0700 Subject: std: remove Encoder::emit_{owned,managed}_vec and Decoder::read_{owned,managed}_vec --- src/libstd/ebml.rs | 29 ++-------- src/libstd/json.rs | 47 ++++------------ src/libstd/serialize.rs | 115 ++++++++++++++++++++------------------- src/libsyntax/ext/auto_encode.rs | 11 +--- 4 files changed, 78 insertions(+), 124 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index fccb707ed1f..18cf807f4f1 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -348,8 +348,8 @@ pub mod reader { f() } - fn read_owned_vec(&self, f: &fn(uint) -> T) -> T { - debug!("read_owned_vec()"); + fn read_seq(&self, f: &fn(uint) -> T) -> T { + debug!("read_seq()"); do self.push_doc(self.next_doc(EsVec)) { let len = self._next_uint(EsVecLen); debug!(" len=%u", len); @@ -357,17 +357,8 @@ pub mod reader { } } - fn read_managed_vec(&self, f: &fn(uint) -> T) -> T { - debug!("read_managed_vec()"); - do self.push_doc(self.next_doc(EsVec)) { - let len = self._next_uint(EsVecLen); - debug!(" len=%u", len); - f(len) - } - } - - fn read_vec_elt(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_vec_elt(idx=%u)", idx); + fn read_seq_elt(&self, idx: uint, f: &fn() -> T) -> T { + debug!("read_seq_elt(idx=%u)", idx); self.push_doc(self.next_doc(EsVecElt), f) } @@ -652,22 +643,14 @@ pub mod writer { } fn emit_enum_variant_arg(&self, _idx: uint, f: &fn()) { f() } - fn emit_borrowed_vec(&self, len: uint, f: &fn()) { + fn emit_seq(&self, len: uint, f: &fn()) { do self.wr_tag(EsVec as uint) { self._emit_tagged_uint(EsVecLen, len); f() } } - fn emit_owned_vec(&self, len: uint, f: &fn()) { - self.emit_borrowed_vec(len, f) - } - - fn emit_managed_vec(&self, len: uint, f: &fn()) { - self.emit_borrowed_vec(len, f) - } - - fn emit_vec_elt(&self, _idx: uint, f: &fn()) { + fn emit_seq_elt(&self, _idx: uint, f: &fn()) { self.wr_tag(EsVecElt as uint, f) } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index c936f8e4dcd..d2dd9bc3432 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -130,19 +130,13 @@ impl serialize::Encoder for Encoder { f(); } - fn emit_borrowed_vec(&self, _len: uint, f: &fn()) { + fn emit_seq(&self, _len: uint, f: &fn()) { self.wr.write_char('['); f(); self.wr.write_char(']'); } - fn emit_owned_vec(&self, len: uint, f: &fn()) { - self.emit_borrowed_vec(len, f) - } - fn emit_managed_vec(&self, len: uint, f: &fn()) { - self.emit_borrowed_vec(len, f) - } - fn emit_vec_elt(&self, idx: uint, f: &fn()) { + fn emit_seq_elt(&self, idx: uint, f: &fn()) { if idx != 0 { self.wr.write_char(','); } f() } @@ -165,10 +159,10 @@ impl serialize::Encoder for Encoder { } fn emit_tup(&self, len: uint, f: &fn()) { - self.emit_borrowed_vec(len, f); + self.emit_seq(len, f); } fn emit_tup_elt(&self, idx: uint, f: &fn()) { - self.emit_vec_elt(idx, f) + self.emit_seq_elt(idx, f) } fn emit_option(&self, f: &fn()) { f(); } @@ -243,7 +237,7 @@ impl serialize::Encoder for PrettyEncoder { f() } - fn emit_borrowed_vec(&self, len: uint, f: &fn()) { + fn emit_seq(&self, len: uint, f: &fn()) { if len == 0 { self.wr.write_str("[]"); } else { @@ -256,13 +250,7 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_char(']'); } } - fn emit_owned_vec(&self, len: uint, f: &fn()) { - self.emit_borrowed_vec(len, f) - } - fn emit_managed_vec(&self, len: uint, f: &fn()) { - self.emit_borrowed_vec(len, f) - } - fn emit_vec_elt(&self, idx: uint, f: &fn()) { + fn emit_seq_elt(&self, idx: uint, f: &fn()) { if idx == 0 { self.wr.write_char('\n'); } else { @@ -300,10 +288,10 @@ impl serialize::Encoder for PrettyEncoder { f(); } fn emit_tup(&self, sz: uint, f: &fn()) { - self.emit_borrowed_vec(sz, f); + self.emit_seq(sz, f); } fn emit_tup_elt(&self, idx: uint, f: &fn()) { - self.emit_vec_elt(idx, f) + self.emit_seq_elt(idx, f) } fn emit_option(&self, f: &fn()) { f(); } @@ -858,19 +846,8 @@ impl<'self> serialize::Decoder for Decoder<'self> { } } - fn read_owned_vec(&self, f: &fn(uint) -> T) -> T { - debug!("read_owned_vec()"); - let len = match *self.peek() { - List(ref list) => list.len(), - _ => fail!(~"not a list"), - }; - let res = f(len); - self.pop(); - res - } - - fn read_managed_vec(&self, f: &fn(uint) -> T) -> T { - debug!("read_owned_vec()"); + fn read_seq(&self, f: &fn(uint) -> T) -> T { + debug!("read_seq()"); let len = match *self.peek() { List(ref list) => list.len(), _ => fail!(~"not a list"), @@ -880,8 +857,8 @@ impl<'self> serialize::Decoder for Decoder<'self> { res } - fn read_vec_elt(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_vec_elt(idx=%u)", idx); + fn read_seq_elt(&self, idx: uint, f: &fn() -> T) -> T { + debug!("read_seq_elt(idx=%u)", idx); match *self.peek() { List(ref list) => { self.stack.push(&list[idx]); diff --git a/src/libstd/serialize.rs b/src/libstd/serialize.rs index 42a9e24b1cb..512a12aace8 100644 --- a/src/libstd/serialize.rs +++ b/src/libstd/serialize.rs @@ -46,10 +46,8 @@ pub trait Encoder { fn emit_enum_variant(&self, v_name: &str, v_id: uint, sz: uint, f: &fn()); fn emit_enum_variant_arg(&self, idx: uint, f: &fn()); - fn emit_borrowed_vec(&self, len: uint, f: &fn()); - fn emit_owned_vec(&self, len: uint, f: &fn()); - fn emit_managed_vec(&self, len: uint, f: &fn()); - fn emit_vec_elt(&self, idx: uint, f: &fn()); + fn emit_seq(&self, len: uint, f: &fn()); + fn emit_seq_elt(&self, idx: uint, f: &fn()); fn emit_rec(&self, f: &fn()); fn emit_struct(&self, name: &str, _len: uint, f: &fn()); @@ -94,9 +92,8 @@ pub trait Decoder { fn read_enum_variant(&self, names: &[&str], f: &fn(uint) -> T) -> T; fn read_enum_variant_arg(&self, idx: uint, f: &fn() -> T) -> T; - fn read_owned_vec(&self, f: &fn(uint) -> T) -> T; - fn read_managed_vec(&self, f: &fn(uint) -> T) -> T; - fn read_vec_elt(&self, idx: uint, f: &fn() -> T) -> T; + fn read_seq(&self, f: &fn(uint) -> T) -> T; + fn read_seq_elt(&self, idx: uint, f: &fn() -> T) -> T; fn read_rec(&self, f: &fn() -> T) -> T; fn read_struct(&self, name: &str, _len: uint, f: &fn() -> T) -> T; @@ -320,9 +317,9 @@ impl> Decodable for @T { impl<'self, S:Encoder,T:Encodable> Encodable for &'self [T] { fn encode(&self, s: &S) { - do s.emit_borrowed_vec(self.len()) { + do s.emit_seq(self.len()) { for self.eachi |i, e| { - s.emit_vec_elt(i, || e.encode(s)) + s.emit_seq_elt(i, || e.encode(s)) } } } @@ -330,9 +327,9 @@ impl<'self, S:Encoder,T:Encodable> Encodable for &'self [T] { impl> Encodable for ~[T] { fn encode(&self, s: &S) { - do s.emit_owned_vec(self.len()) { + do s.emit_seq(self.len()) { for self.eachi |i, e| { - s.emit_vec_elt(i, || e.encode(s)) + s.emit_seq_elt(i, || e.encode(s)) } } } @@ -340,9 +337,9 @@ impl> Encodable for ~[T] { impl> Decodable for ~[T] { fn decode(d: &D) -> ~[T] { - do d.read_owned_vec |len| { + do d.read_seq |len| { do vec::from_fn(len) |i| { - d.read_vec_elt(i, || Decodable::decode(d)) + d.read_seq_elt(i, || Decodable::decode(d)) } } } @@ -350,9 +347,9 @@ impl> Decodable for ~[T] { impl> Encodable for @[T] { fn encode(&self, s: &S) { - do s.emit_managed_vec(self.len()) { + do s.emit_seq(self.len()) { for self.eachi |i, e| { - s.emit_vec_elt(i, || e.encode(s)) + s.emit_seq_elt(i, || e.encode(s)) } } } @@ -360,9 +357,9 @@ impl> Encodable for @[T] { impl> Decodable for @[T] { fn decode(d: &D) -> @[T] { - do d.read_managed_vec |len| { + do d.read_seq |len| { do at_vec::from_fn(len) |i| { - d.read_vec_elt(i, || Decodable::decode(d)) + d.read_seq_elt(i, || Decodable::decode(d)) } } } @@ -395,9 +392,9 @@ impl,T1:Encodable> Encodable for (T0, T1) { fn encode(&self, s: &S) { match *self { (ref t0, ref t1) => { - do s.emit_tup(2) { - s.emit_tup_elt(0, || t0.encode(s)); - s.emit_tup_elt(1, || t1.encode(s)); + do s.emit_seq(2) { + s.emit_seq_elt(0, || t0.encode(s)); + s.emit_seq_elt(1, || t1.encode(s)); } } } @@ -406,10 +403,11 @@ impl,T1:Encodable> Encodable for (T0, T1) { impl,T1:Decodable> Decodable for (T0, T1) { fn decode(d: &D) -> (T0, T1) { - do d.read_tup(2) { + do d.read_seq |len| { + fail_unless!(len == 2); ( - d.read_tup_elt(0, || Decodable::decode(d)), - d.read_tup_elt(1, || Decodable::decode(d)) + d.read_seq_elt(0, || Decodable::decode(d)), + d.read_seq_elt(1, || Decodable::decode(d)) ) } } @@ -424,10 +422,10 @@ impl< fn encode(&self, s: &S) { match *self { (ref t0, ref t1, ref t2) => { - do s.emit_tup(3) { - s.emit_tup_elt(0, || t0.encode(s)); - s.emit_tup_elt(1, || t1.encode(s)); - s.emit_tup_elt(2, || t2.encode(s)); + do s.emit_seq(3) { + s.emit_seq_elt(0, || t0.encode(s)); + s.emit_seq_elt(1, || t1.encode(s)); + s.emit_seq_elt(2, || t2.encode(s)); } } } @@ -441,11 +439,12 @@ impl< T2: Decodable > Decodable for (T0, T1, T2) { fn decode(d: &D) -> (T0, T1, T2) { - do d.read_tup(3) { + do d.read_seq |len| { + fail_unless!(len == 3); ( - d.read_tup_elt(0, || Decodable::decode(d)), - d.read_tup_elt(1, || Decodable::decode(d)), - d.read_tup_elt(2, || Decodable::decode(d)) + d.read_seq_elt(0, || Decodable::decode(d)), + d.read_seq_elt(1, || Decodable::decode(d)), + d.read_seq_elt(2, || Decodable::decode(d)) ) } } @@ -461,11 +460,11 @@ impl< fn encode(&self, s: &S) { match *self { (ref t0, ref t1, ref t2, ref t3) => { - do s.emit_tup(4) { - s.emit_tup_elt(0, || t0.encode(s)); - s.emit_tup_elt(1, || t1.encode(s)); - s.emit_tup_elt(2, || t2.encode(s)); - s.emit_tup_elt(3, || t3.encode(s)); + do s.emit_seq(4) { + s.emit_seq_elt(0, || t0.encode(s)); + s.emit_seq_elt(1, || t1.encode(s)); + s.emit_seq_elt(2, || t2.encode(s)); + s.emit_seq_elt(3, || t3.encode(s)); } } } @@ -480,12 +479,13 @@ impl< T3: Decodable > Decodable for (T0, T1, T2, T3) { fn decode(d: &D) -> (T0, T1, T2, T3) { - do d.read_tup(4) { + do d.read_seq |len| { + fail_unless!(len == 4); ( - d.read_tup_elt(0, || Decodable::decode(d)), - d.read_tup_elt(1, || Decodable::decode(d)), - d.read_tup_elt(2, || Decodable::decode(d)), - d.read_tup_elt(3, || Decodable::decode(d)) + d.read_seq_elt(0, || Decodable::decode(d)), + d.read_seq_elt(1, || Decodable::decode(d)), + d.read_seq_elt(2, || Decodable::decode(d)), + d.read_seq_elt(3, || Decodable::decode(d)) ) } } @@ -502,12 +502,12 @@ impl< fn encode(&self, s: &S) { match *self { (ref t0, ref t1, ref t2, ref t3, ref t4) => { - do s.emit_tup(5) { - s.emit_tup_elt(0, || t0.encode(s)); - s.emit_tup_elt(1, || t1.encode(s)); - s.emit_tup_elt(2, || t2.encode(s)); - s.emit_tup_elt(3, || t3.encode(s)); - s.emit_tup_elt(4, || t4.encode(s)); + do s.emit_seq(5) { + s.emit_seq_elt(0, || t0.encode(s)); + s.emit_seq_elt(1, || t1.encode(s)); + s.emit_seq_elt(2, || t2.encode(s)); + s.emit_seq_elt(3, || t3.encode(s)); + s.emit_seq_elt(4, || t4.encode(s)); } } } @@ -524,13 +524,14 @@ impl< > Decodable for (T0, T1, T2, T3, T4) { fn decode(d: &D) -> (T0, T1, T2, T3, T4) { - do d.read_tup(5) { + do d.read_seq |len| { + fail_unless!(len == 5); ( - d.read_tup_elt(0, || Decodable::decode(d)), - d.read_tup_elt(1, || Decodable::decode(d)), - d.read_tup_elt(2, || Decodable::decode(d)), - d.read_tup_elt(3, || Decodable::decode(d)), - d.read_tup_elt(4, || Decodable::decode(d)) + d.read_seq_elt(0, || Decodable::decode(d)), + d.read_seq_elt(1, || Decodable::decode(d)), + d.read_seq_elt(2, || Decodable::decode(d)), + d.read_seq_elt(3, || Decodable::decode(d)), + d.read_seq_elt(4, || Decodable::decode(d)) ) } } @@ -547,9 +548,9 @@ pub trait EncoderHelpers { impl EncoderHelpers for S { fn emit_from_vec(&self, v: &[T], f: &fn(v: &T)) { - do self.emit_owned_vec(v.len()) { + do self.emit_seq(v.len()) { for v.eachi |i, e| { - do self.emit_vec_elt(i) { + do self.emit_seq_elt(i) { f(e) } } @@ -563,9 +564,9 @@ pub trait DecoderHelpers { impl DecoderHelpers for D { fn read_to_vec(&self, f: &fn() -> T) -> ~[T] { - do self.read_owned_vec |len| { + do self.read_seq |len| { do vec::from_fn(len) |i| { - self.read_vec_elt(i, || f()) + self.read_seq_elt(i, || f()) } } } diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index c9357c9d867..4b262df36a6 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -1255,17 +1255,10 @@ mod test { self.add_to_log(CallToEmitEnumVariantArg (idx)); f(); } - fn emit_borrowed_vec(&self, +_len: uint, f: &fn()) { + fn emit_seq(&self, +_len: uint, f: &fn()) { self.add_unknown_to_log(); f(); } - - fn emit_owned_vec(&self, +_len: uint, f: &fn()) { - self.add_unknown_to_log(); f(); - } - fn emit_managed_vec(&self, +_len: uint, f: &fn()) { - self.add_unknown_to_log(); f(); - } - fn emit_vec_elt(&self, +_idx: uint, f: &fn()) { + fn emit_seq_elt(&self, +_idx: uint, f: &fn()) { self.add_unknown_to_log(); f(); } -- cgit 1.4.1-3-g733a5 From e99cdcfba847ade51791afbc95478306c6c3e7c1 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 28 Mar 2013 21:47:17 -0700 Subject: std: remove Encoder::emit_tup{,_elt} and Decoder::read_tup{,_elt} --- src/libstd/ebml.rs | 13 ------------- src/libstd/json.rs | 31 ------------------------------- src/libstd/serialize.rs | 6 ------ src/libsyntax/ext/auto_encode.rs | 7 ------- 4 files changed, 57 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index 18cf807f4f1..12a4ea14909 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -378,16 +378,6 @@ pub mod reader { f() } - fn read_tup(&self, len: uint, f: &fn() -> T) -> T { - debug!("read_tup(len=%u)", len); - f() - } - - fn read_tup_elt(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_tup_elt(idx=%u)", idx); - f() - } - #[cfg(stage0)] fn read_option(&self, f: &fn(bool) -> T) -> T { debug!("read_option()"); @@ -661,9 +651,6 @@ pub mod writer { f() } - fn emit_tup(&self, _len: uint, f: &fn()) { f() } - fn emit_tup_elt(&self, _idx: uint, f: &fn()) { f() } - fn emit_option(&self, f: &fn()) { self.emit_enum("Option", f); } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index d2dd9bc3432..fe5b0cf88e6 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -158,13 +158,6 @@ impl serialize::Encoder for Encoder { f(); } - fn emit_tup(&self, len: uint, f: &fn()) { - self.emit_seq(len, f); - } - fn emit_tup_elt(&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(); } fn emit_option_some(&self, f: &fn()) { f(); } @@ -287,12 +280,6 @@ impl serialize::Encoder for PrettyEncoder { self.wr.write_str(": "); f(); } - fn emit_tup(&self, sz: uint, f: &fn()) { - self.emit_seq(sz, f); - } - fn emit_tup_elt(&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(); } @@ -905,24 +892,6 @@ impl<'self> serialize::Decoder for Decoder<'self> { } } - fn read_tup(&self, len: uint, f: &fn() -> T) -> T { - debug!("read_tup(len=%u)", len); - let value = f(); - self.pop(); - value - } - - fn read_tup_elt(&self, idx: uint, f: &fn() -> T) -> T { - debug!("read_tup_elt(idx=%u)", idx); - match *self.peek() { - List(ref list) => { - self.stack.push(&list[idx]); - f() - } - _ => fail!(~"not a list") - } - } - fn read_option(&self, f: &fn(bool) -> T) -> T { match *self.peek() { Null => { self.pop(); f(false) } diff --git a/src/libstd/serialize.rs b/src/libstd/serialize.rs index 512a12aace8..d0127bb0999 100644 --- a/src/libstd/serialize.rs +++ b/src/libstd/serialize.rs @@ -53,9 +53,6 @@ pub trait Encoder { fn emit_struct(&self, name: &str, _len: uint, f: &fn()); fn emit_field(&self, f_name: &str, f_idx: uint, f: &fn()); - fn emit_tup(&self, len: uint, f: &fn()); - fn emit_tup_elt(&self, idx: uint, f: &fn()); - // Specialized types: fn emit_option(&self, f: &fn()); fn emit_option_none(&self); @@ -99,9 +96,6 @@ pub trait Decoder { fn read_struct(&self, name: &str, _len: uint, f: &fn() -> T) -> T; fn read_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T; - fn read_tup(&self, sz: uint, f: &fn() -> T) -> T; - fn read_tup_elt(&self, idx: uint, f: &fn() -> T) -> T; - // Specialized types: fn read_option(&self, f: &fn(bool) -> T) -> T; } diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index 4b262df36a6..d4e1fa3abf7 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -1272,13 +1272,6 @@ mod test { self.add_to_log(CallToEmitField (name.to_str(),idx)); f(); } - fn emit_tup(&self, +_len: uint, f: &fn()) { - self.add_unknown_to_log(); f(); - } - fn emit_tup_elt(&self, +_idx: uint, f: &fn()) { - self.add_unknown_to_log(); f(); - } - fn emit_option(&self, f: &fn()) { self.add_to_log(CallToEmitOption); f(); -- cgit 1.4.1-3-g733a5 From 90b3658cd69f63d95f3bf568e6f23406c56f11a7 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 28 Mar 2013 22:19:43 -0700 Subject: std: remove Encoder::read_rec and Decoder::emit_rec --- src/librustc/middle/astencode.rs | 8 +++---- src/libstd/ebml.rs | 6 ----- src/libstd/json.rs | 51 ++++++++++++++-------------------------- src/libstd/serialize.rs | 2 -- src/libsyntax/ext/auto_encode.rs | 3 --- 5 files changed, 22 insertions(+), 48 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index c2692e9fa10..719ffe9bb87 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -558,7 +558,7 @@ trait read_method_map_entry_helper { fn encode_method_map_entry(ecx: @e::EncodeContext, ebml_w: writer::Encoder, mme: method_map_entry) { - do ebml_w.emit_rec { + do ebml_w.emit_struct("method_map_entry", 3) { do ebml_w.emit_field(~"self_arg", 0u) { ebml_w.emit_arg(ecx, mme.self_arg); } @@ -574,7 +574,7 @@ fn encode_method_map_entry(ecx: @e::EncodeContext, impl read_method_map_entry_helper for reader::Decoder { fn read_method_map_entry(&self, xcx: @ExtendedDecodeContext) -> method_map_entry { - do self.read_rec { + do self.read_struct("method_map_entry", 3) { method_map_entry { self_arg: self.read_field(~"self_arg", 0u, || { self.read_arg(xcx) @@ -817,7 +817,7 @@ impl ebml_writer_helpers for writer::Encoder { fn emit_tpbt(&self, ecx: @e::EncodeContext, tpbt: ty::ty_param_bounds_and_ty) { - do self.emit_rec { + do self.emit_struct("ty_param_bounds_and_ty", 3) { do self.emit_field(~"bounds", 0) { do self.emit_from_vec(*tpbt.bounds) |bs| { self.emit_bounds(ecx, *bs); @@ -1084,7 +1084,7 @@ impl ebml_decoder_decoder_helpers for reader::Decoder { fn read_ty_param_bounds_and_ty(&self, xcx: @ExtendedDecodeContext) -> ty::ty_param_bounds_and_ty { - do self.read_rec { + do self.read_struct("ty_param_bounds_and_ty", 3) { ty::ty_param_bounds_and_ty { bounds: self.read_field(~"bounds", 0u, || { @self.read_to_vec(|| self.read_bounds(xcx) ) diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index 12a4ea14909..6b216504e54 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -362,11 +362,6 @@ pub mod reader { self.push_doc(self.next_doc(EsVecElt), f) } - fn read_rec(&self, f: &fn() -> T) -> T { - debug!("read_rec()"); - f() - } - fn read_struct(&self, name: &str, _len: uint, f: &fn() -> T) -> T { debug!("read_struct(name=%s)", name); f() @@ -644,7 +639,6 @@ pub mod writer { self.wr_tag(EsVecElt as uint, f) } - fn emit_rec(&self, f: &fn()) { f() } fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) { f() } fn emit_field(&self, name: &str, _idx: uint, f: &fn()) { self._emit_label(name); diff --git a/src/libstd/json.rs b/src/libstd/json.rs index fe5b0cf88e6..ac7119d8ed8 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -141,11 +141,6 @@ impl serialize::Encoder for Encoder { f() } - fn emit_rec(&self, f: &fn()) { - self.wr.write_char('{'); - f(); - self.wr.write_char('}'); - } fn emit_struct(&self, _name: &str, _len: uint, f: &fn()) { self.wr.write_char('{'); f(); @@ -253,20 +248,17 @@ impl serialize::Encoder for PrettyEncoder { f() } - fn emit_rec(&self, f: &fn()) { - self.wr.write_char('{'); - self.indent += 2; - f(); - self.wr.write_char('\n'); - self.indent -= 2; - self.wr.write_str(spaces(self.indent)); - self.wr.write_char('}'); - } fn emit_struct(&self, _name: &str, len: uint, f: &fn()) { if len == 0 { self.wr.write_str("{}"); } else { - self.emit_rec(f) + self.wr.write_char('{'); + self.indent += 2; + f(); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char('}'); } } fn emit_field(&self, name: &str, idx: uint, f: &fn()) { @@ -286,25 +278,25 @@ impl serialize::Encoder for PrettyEncoder { fn emit_option_some(&self, f: &fn()) { f(); } } -impl serialize::Encodable for Json { - fn encode(&self, s: &S) { +impl serialize::Encodable for Json { + fn encode(&self, e: &E) { match *self { - Number(v) => v.encode(s), - String(ref v) => v.encode(s), - Boolean(v) => v.encode(s), - List(ref v) => v.encode(s), + 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) => { - do s.emit_rec || { + do e.emit_struct("Object", v.len())|| { let mut idx = 0; for v.each |&(key, value)| { - do s.emit_field(*key, idx) { - value.encode(s); + do e.emit_field(*key, idx) { + value.encode(e); } idx += 1; } } }, - Null => s.emit_nil(), + Null => e.emit_nil(), } } } @@ -855,13 +847,6 @@ impl<'self> serialize::Decoder for Decoder<'self> { } } - fn read_rec(&self, f: &fn() -> T) -> T { - debug!("read_rec()"); - let value = f(); - self.pop(); - value - } - fn read_struct(&self, _name: &str, _len: uint, f: &fn() -> T) -> T { debug!("read_struct()"); let value = f(); @@ -870,7 +855,7 @@ impl<'self> serialize::Decoder for Decoder<'self> { } fn read_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T { - debug!("read_rec_field(%s, idx=%u)", name, idx); + debug!("read_field(%s, idx=%u)", name, idx); let top = self.peek(); match *top { Object(ref obj) => { diff --git a/src/libstd/serialize.rs b/src/libstd/serialize.rs index d0127bb0999..83201653474 100644 --- a/src/libstd/serialize.rs +++ b/src/libstd/serialize.rs @@ -49,7 +49,6 @@ pub trait Encoder { fn emit_seq(&self, len: uint, f: &fn()); fn emit_seq_elt(&self, idx: uint, f: &fn()); - fn emit_rec(&self, f: &fn()); fn emit_struct(&self, name: &str, _len: uint, f: &fn()); fn emit_field(&self, f_name: &str, f_idx: uint, f: &fn()); @@ -92,7 +91,6 @@ pub trait Decoder { fn read_seq(&self, f: &fn(uint) -> T) -> T; fn read_seq_elt(&self, idx: uint, f: &fn() -> T) -> T; - fn read_rec(&self, f: &fn() -> T) -> T; fn read_struct(&self, name: &str, _len: uint, f: &fn() -> T) -> T; fn read_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T; diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index d4e1fa3abf7..3aceef9c73a 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -1262,9 +1262,6 @@ mod test { self.add_unknown_to_log(); f(); } - fn emit_rec(&self, f: &fn()) { - self.add_unknown_to_log(); f(); - } fn emit_struct(&self, name: &str, +len: uint, f: &fn()) { self.add_to_log(CallToEmitStruct (name.to_str(),len)); f(); } -- cgit 1.4.1-3-g733a5 From d1a83e6986470d2db81474bb7fd3eadada78b0a4 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 29 Mar 2013 09:04:35 -0700 Subject: std: add Encoder::emit_map and Decoder::read_map --- src/libstd/ebml.rs | 27 +++++ src/libstd/flatpipes.rs | 4 +- src/libstd/json.rs | 221 ++++++++++++++++++++++++--------------- src/libstd/serialize.rs | 63 ++++++++++- src/libstd/workcache.rs | 41 +++++--- src/libsyntax/ext/auto_encode.rs | 10 ++ 6 files changed, 263 insertions(+), 103 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index 6b216504e54..f7fe2f54c57 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -402,6 +402,21 @@ pub mod reader { } } } + + fn read_map(&self, _f: &fn(uint) -> T) -> T { + debug!("read_map()"); + fail!(~"read_map is unimplemented"); + } + + fn read_map_elt_key(&self, idx: uint, _f: &fn() -> T) -> T { + debug!("read_map_elt_key(idx=%u)", idx); + fail!(~"read_map_elt_val is unimplemented"); + } + + fn read_map_elt_val(&self, idx: uint, _f: &fn() -> T) -> T { + debug!("read_map_elt_val(idx=%u)", idx); + fail!(~"read_map_elt_val is unimplemented"); + } } } @@ -654,6 +669,18 @@ pub mod writer { fn emit_option_some(&self, f: &fn()) { self.emit_enum_variant("Some", 1, 1, f) } + + fn emit_map(&self, _len: uint, _f: &fn()) { + fail!(~"emit_map is unimplemented"); + } + + fn emit_map_elt_key(&self, _idx: uint, _f: &fn()) { + fail!(~"emit_map_elt_key is unimplemented"); + } + + fn emit_map_elt_val(&self, _idx: uint, _f: &fn()) { + fail!(~"emit_map_elt_val is unimplemented"); + } } } diff --git a/src/libstd/flatpipes.rs b/src/libstd/flatpipes.rs index 1a3a28f7492..f042e2226cd 100644 --- a/src/libstd/flatpipes.rs +++ b/src/libstd/flatpipes.rs @@ -466,8 +466,8 @@ pub mod flatteners { fn from_writer(w: @Writer) -> Self; } - impl<'self> FromReader for json::Decoder<'self> { - fn from_reader(r: @Reader) -> json::Decoder<'self> { + impl FromReader for json::Decoder { + fn from_reader(r: @Reader) -> json::Decoder { match json::from_reader(r) { Ok(json) => { json::Decoder(json) diff --git a/src/libstd/json.rs b/src/libstd/json.rs index ac7119d8ed8..f069547bc8c 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -156,6 +156,22 @@ impl serialize::Encoder for Encoder { fn emit_option(&self, f: &fn()) { f(); } fn emit_option_none(&self) { self.emit_nil(); } fn emit_option_some(&self, f: &fn()) { f(); } + + fn emit_map(&self, _len: uint, f: &fn()) { + self.wr.write_char('{'); + f(); + self.wr.write_char('}'); + } + + fn emit_map_elt_key(&self, idx: uint, f: &fn()) { + if idx != 0 { self.wr.write_char(','); } + f() + } + + fn emit_map_elt_val(&self, _idx: uint, f: &fn()) { + self.wr.write_char(':'); + f() + } } pub struct PrettyEncoder { @@ -276,6 +292,34 @@ impl serialize::Encoder for PrettyEncoder { fn emit_option(&self, f: &fn()) { f(); } fn emit_option_none(&self) { self.emit_nil(); } fn emit_option_some(&self, f: &fn()) { f(); } + + fn emit_map(&self, len: uint, f: &fn()) { + if len == 0 { + self.wr.write_str("{}"); + } else { + self.wr.write_char('{'); + self.indent += 2; + f(); + self.wr.write_char('\n'); + self.indent -= 2; + self.wr.write_str(spaces(self.indent)); + self.wr.write_char('}'); + } + } + fn emit_map_elt_key(&self, idx: uint, f: &fn()) { + if idx == 0 { + self.wr.write_char('\n'); + } else { + self.wr.write_str(",\n"); + } + self.wr.write_str(spaces(self.indent)); + f(); + } + + fn emit_map_elt_val(&self, _idx: uint, f: &fn()) { + self.wr.write_str(": "); + f(); + } } impl serialize::Encodable for Json { @@ -285,17 +329,7 @@ impl serialize::Encodable for Json { String(ref v) => v.encode(e), Boolean(v) => v.encode(e), List(ref v) => v.encode(e), - Object(ref v) => { - do e.emit_struct("Object", v.len())|| { - let mut idx = 0; - for v.each |&(key, value)| { - do e.emit_field(*key, idx) { - value.encode(e); - } - idx += 1; - } - } - }, + Object(ref v) => v.encode(e), Null => e.emit_nil(), } } @@ -702,37 +736,20 @@ pub fn from_str(s: &str) -> Result { } } -pub struct Decoder<'self> { - priv json: Json, - priv mut stack: ~[&'self Json], +pub struct Decoder { + priv mut stack: ~[Json], } pub fn Decoder(json: Json) -> Decoder { - Decoder { json: json, stack: ~[] } -} - -priv impl<'self> Decoder<'self> { - fn peek(&self) -> &'self Json { - if vec::uniq_len(&const self.stack) == 0 { - self.stack.push(&self.json); - } - self.stack[vec::uniq_len(&const self.stack) - 1] - } - - fn pop(&self) -> &'self Json { - if vec::uniq_len(&const self.stack) == 0 { - self.stack.push(&self.json); - } - self.stack.pop() - } + Decoder { stack: ~[json] } } -impl<'self> serialize::Decoder for Decoder<'self> { +impl serialize::Decoder for Decoder { fn read_nil(&self) -> () { debug!("read_nil"); - match *self.pop() { + match self.stack.pop() { Null => (), - _ => fail!(~"not a null") + value => fail!(fmt!("not a null: %?", value)) } } @@ -750,9 +767,9 @@ impl<'self> serialize::Decoder for Decoder<'self> { fn read_bool(&self) -> bool { debug!("read_bool"); - match *self.pop() { + match self.stack.pop() { Boolean(b) => b, - _ => fail!(~"not a boolean") + value => fail!(fmt!("not a boolean: %?", value)) } } @@ -760,9 +777,9 @@ impl<'self> serialize::Decoder for Decoder<'self> { fn read_f32(&self) -> f32 { self.read_float() as f32 } fn read_float(&self) -> float { debug!("read_float"); - match *self.pop() { + match self.stack.pop() { Number(f) => f, - _ => fail!(~"not a number") + value => fail!(fmt!("not a number: %?", value)) } } @@ -775,13 +792,12 @@ impl<'self> serialize::Decoder for Decoder<'self> { fn read_str(&self) -> ~str { debug!("read_str"); - match *self.pop() { - String(ref s) => copy *s, - ref json => fail!(fmt!("not a string: %?", *json)) + match self.stack.pop() { + String(s) => s, + json => fail!(fmt!("not a string: %?", json)) } } - fn read_enum(&self, name: &str, f: &fn() -> T) -> T { debug!("read_enum(%s)", name); f() @@ -790,9 +806,9 @@ impl<'self> serialize::Decoder for Decoder<'self> { #[cfg(stage0)] fn read_enum_variant(&self, f: &fn(uint) -> T) -> T { debug!("read_enum_variant()"); - let idx = match *self.peek() { - Null => 0, - _ => 1, + let idx = match self.stack.pop() { + Null => { self.stack.push(Null); 0 }, + value => { self.stack.push(value); 1 }, }; f(idx) } @@ -802,12 +818,20 @@ impl<'self> serialize::Decoder for Decoder<'self> { #[cfg(stage3)] fn read_enum_variant(&self, names: &[&str], f: &fn(uint) -> T) -> T { debug!("read_enum_variant(names=%?)", names); - let name = match *self.peek() { - String(ref s) => s, - List([String(ref s), .. _]) => s, + 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)) { + let idx = match vec::position(names, |n| str::eq_slice(*n, name)) { Some(idx) => idx, None => fail!(fmt!("Unknown variant name: %?", name)), }; @@ -816,73 +840,90 @@ impl<'self> serialize::Decoder for Decoder<'self> { fn read_enum_variant_arg(&self, idx: uint, f: &fn() -> T) -> T { debug!("read_enum_variant_arg(idx=%u)", idx); - match *self.peek() { - List(ref list) => { - self.stack.push(&list[idx + 1]); - f() - } - ref json => fail!(fmt!("not a list: %?", json)), - } + f() } fn read_seq(&self, f: &fn(uint) -> T) -> T { debug!("read_seq()"); - let len = match *self.peek() { - List(ref list) => list.len(), + 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"), }; let res = f(len); - self.pop(); + self.stack.pop(); res } fn read_seq_elt(&self, idx: uint, f: &fn() -> T) -> T { debug!("read_seq_elt(idx=%u)", idx); - match *self.peek() { - List(ref list) => { - self.stack.push(&list[idx]); - f() - } - _ => fail!(~"not a list"), - } + f() } - fn read_struct(&self, _name: &str, _len: uint, f: &fn() -> T) -> T { - debug!("read_struct()"); + fn read_struct(&self, name: &str, len: uint, f: &fn() -> T) -> T { + debug!("read_struct(name=%s, len=%u)", name, len); let value = f(); - self.pop(); + self.stack.pop(); value } fn read_field(&self, name: &str, idx: uint, f: &fn() -> T) -> T { debug!("read_field(%s, idx=%u)", name, idx); - let top = self.peek(); - match *top { - Object(ref obj) => { - match obj.find(&name.to_owned()) { + 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 } - Number(_) => fail!(~"num"), - String(_) => fail!(~"str"), - Boolean(_) => fail!(~"bool"), - List(_) => fail!(fmt!("list: %?", top)), - Null => fail!(~"null"), - - //_ => fail!(fmt!("not an object: %?", *top)) + value => fail!(fmt!("not an object: %?", value)) } } fn read_option(&self, f: &fn(bool) -> T) -> T { - match *self.peek() { - Null => { self.pop(); f(false) } - _ => f(true), + match self.stack.pop() { + Null => f(false), + value => { self.stack.push(value); f(true) } } } + + fn read_map(&self, f: &fn(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(len) + } + + fn read_map_elt_key(&self, idx: uint, f: &fn() -> T) -> T { + debug!("read_map_elt_key(idx=%u)", idx); + f() + } + + fn read_map_elt_val(&self, idx: uint, f: &fn() -> T) -> T { + debug!("read_map_elt_val(idx=%u)", idx); + f() + } } impl Eq for Json { @@ -1251,7 +1292,7 @@ mod tests { #[test] fn test_write_object_pretty() { - assert_eq!(to_pretty_str(&mk_object(~[])), ~"{\n}"); + assert_eq!(to_pretty_str(&mk_object(~[])), ~"{}"); assert_eq!( to_pretty_str(&mk_object(~[(~"a", Boolean(true))])), ~"\ @@ -1630,6 +1671,16 @@ mod tests { assert_eq!(value, Frog(~"Henry", 349)); } + #[test] + fn test_read_map() { + let s = ~"{\"a\": \"Dog\", \"b\": [\"Frog\", \"Henry\", 349]}"; + let decoder = Decoder(from_str(s).unwrap()); + let map: LinearMap<~str, Animal> = Decodable::decode(&decoder); + + assert_eq!(map.find(&~"a"), Some(Dog)); + assert_eq!(map.find(&~"b"), Some(Frog(~"Henry", 349))); + } + #[test] fn test_multiline_errors() { assert_eq!(from_str(~"{\n \"foo\":\n \"bar\""), diff --git a/src/libstd/serialize.rs b/src/libstd/serialize.rs index 83201653474..dd487218fe5 100644 --- a/src/libstd/serialize.rs +++ b/src/libstd/serialize.rs @@ -16,9 +16,8 @@ Core encoding and decoding interfaces. #[forbid(non_camel_case_types)]; -use core::at_vec; use core::prelude::*; -use core::vec; +use core::hashmap::linear::LinearMap; pub trait Encoder { // Primitive types: @@ -56,6 +55,10 @@ pub trait Encoder { fn emit_option(&self, f: &fn()); fn emit_option_none(&self); fn emit_option_some(&self, f: &fn()); + + fn emit_map(&self, len: uint, f: &fn()); + fn emit_map_elt_key(&self, idx: uint, f: &fn()); + fn emit_map_elt_val(&self, idx: uint, f: &fn()); } pub trait Decoder { @@ -96,6 +99,10 @@ pub trait Decoder { // Specialized types: fn read_option(&self, f: &fn(bool) -> T) -> T; + + fn read_map(&self, f: &fn(uint) -> T) -> T; + fn read_map_elt_key(&self, idx: uint, f: &fn() -> T) -> T; + fn read_map_elt_val(&self, idx: uint, f: &fn() -> T) -> T; } pub trait Encodable { @@ -529,6 +536,58 @@ impl< } } +impl< + E: Encoder, + K: Encodable + Hash + IterBytes + Eq, + V: Encodable +> Encodable for LinearMap { + fn encode(&self, e: &E) { + do e.emit_map(self.len()) { + 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)); + i += 1; + } + } + } +} + +impl< + D: Decoder, + K: Decodable + Hash + IterBytes + Eq, + V: Decodable +> Decodable for LinearMap { + #[cfg(stage0)] + fn decode(d: &D) -> LinearMap { + do d.read_map |len| { + let mut map = LinearMap::new(); + map.reserve_at_least(len); + 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)); + map.insert(key, val); + } + map + } + } + + #[cfg(stage1)] + #[cfg(stage2)] + #[cfg(stage3)] + fn decode(d: &D) -> LinearMap { + do d.read_map |len| { + let mut map = LinearMap::with_capacity(len); + 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)); + map.insert(key, val); + } + map + } + } +} + // ___________________________________________________________________________ // Helper routines // diff --git a/src/libstd/workcache.rs b/src/libstd/workcache.rs index f477a8c9f91..90f10ea5054 100644 --- a/src/libstd/workcache.rs +++ b/src/libstd/workcache.rs @@ -137,7 +137,11 @@ pub impl WorkKey { } } -type WorkMap = LinearMap; +struct WorkMap(LinearMap); + +impl WorkMap { + fn new() -> WorkMap { WorkMap(LinearMap::new()) } +} impl Encodable for WorkMap { fn encode(&self, s: &S) { @@ -153,7 +157,7 @@ impl Encodable for WorkMap { impl Decodable for WorkMap { fn decode(d: &D) -> WorkMap { let v : ~[(WorkKey,~str)] = Decodable::decode(d); - let mut w = LinearMap::new(); + let mut w = WorkMap::new(); for v.each |&(k, v)| { w.insert(copy k, copy v); } @@ -235,7 +239,7 @@ fn json_encode>(t: &T) -> ~str { } // FIXME(#5121) -fn json_decode>>(s: &str) -> T { +fn json_decode>(s: &str) -> T { do io::with_str_reader(s) |rdr| { let j = result::unwrap(json::from_reader(rdr)); Decodable::decode(&json::Decoder(j)) @@ -260,18 +264,25 @@ pub impl Context { fn new(db: @Mut, lg: @Mut, cfg: @json::Object) -> Context { - Context{db: db, logger: lg, cfg: cfg, freshness: LinearMap::new()} + Context { + db: db, + logger: lg, + cfg: cfg, + freshness: LinearMap::new() + } } fn prep + - Decodable>>( // FIXME(#5121) + Decodable>( // FIXME(#5121) @self, fn_name:&str, blk: &fn(@Mut)->Work) -> Work { - let p = @Mut(Prep {ctxt: self, - fn_name: fn_name.to_owned(), - declared_inputs: LinearMap::new()}); + let p = @Mut(Prep { + ctxt: self, + fn_name: fn_name.to_owned(), + declared_inputs: WorkMap::new() + }); blk(p) } } @@ -283,7 +294,7 @@ trait TPrep { fn all_fresh(&self, cat:&str, map:&WorkMap) -> bool; fn exec + - Decodable>>( // FIXME(#5121) + Decodable>( // FIXME(#5121) &self, blk: ~fn(&Exec) -> T) -> Work; } @@ -324,7 +335,7 @@ impl TPrep for @Mut { fn exec + - Decodable>>( // FIXME(#5121) + Decodable>( // FIXME(#5121) &self, blk: ~fn(&Exec) -> T) -> Work { let mut bo = Some(blk); @@ -349,8 +360,10 @@ impl TPrep for @Mut { let blk = blk.unwrap(); let chan = Cell(chan); do task::spawn || { - let exe = Exec{discovered_inputs: LinearMap::new(), - discovered_outputs: LinearMap::new()}; + let exe = Exec { + discovered_inputs: WorkMap::new(), + discovered_outputs: WorkMap::new(), + }; let chan = chan.take(); let v = blk(&exe); send_one(chan, (exe, v)); @@ -365,7 +378,7 @@ impl TPrep for @Mut { pub impl + - Decodable>> Work { // FIXME(#5121) + Decodable> Work { // FIXME(#5121) fn new(p: @Mut, e: Either>) -> Work { Work { prep: p, res: Some(e) } } @@ -374,7 +387,7 @@ pub impl + - Decodable>>( // FIXME(#5121) + Decodable>( // FIXME(#5121) w: Work) -> T { let mut ww = w; let mut s = None; diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index 3aceef9c73a..de01d258449 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -1280,6 +1280,16 @@ mod test { self.add_to_log(CallToEmitOptionSome); f(); } + + fn read_map(&self, f: &fn(uint) -> T) -> T { + self.add_unknown_to_log(); f(0); + } + fn read_map_elt_key(&self, idx: uint, f: &fn() -> T) -> T { + self.add_unknown_to_log(); f(); + } + fn read_map_elt_val(&self, idx: uint, f: &fn() -> T) -> T { + self.add_unknown_to_log(); f(); + } } -- cgit 1.4.1-3-g733a5 From f364cf54631fc22860d0f00fb60f6a1dea3d7fd2 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 30 Mar 2013 13:24:52 -0700 Subject: syntax: fix auto_encode test. --- src/libsyntax/ext/auto_encode.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index 52bb4110082..d25792355a7 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -1281,13 +1281,13 @@ mod test { f(); } - fn emit_map(&self, f: &fn(uint) -> T) -> T { - self.add_unknown_to_log(); f(0); + fn emit_map(&self, _len: uint, f: &fn()) { + self.add_unknown_to_log(); f(); } - fn emit_map_elt_key(&self, idx: uint, f: &fn() -> T) -> T { + fn emit_map_elt_key(&self, _idx: uint, f: &fn()) { self.add_unknown_to_log(); f(); } - fn emit_map_elt_val(&self, idx: uint, f: &fn() -> T) -> T { + fn emit_map_elt_val(&self, _idx: uint, f: &fn()) { self.add_unknown_to_log(); f(); } } -- cgit 1.4.1-3-g733a5