diff --git a/src/libcore/str.rs b/src/libcore/str.rs index c6e7116675f2..a228e02c8be2 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -349,7 +349,7 @@ pub pure fn to_bytes(s: &str) -> ~[u8] unsafe { #[inline(always)] pub pure fn byte_slice(s: &str, f: fn(v: &[u8]) -> T) -> T { do as_buf(s) |p,n| { - unsafe { vec::raw::form_slice(p, n-1u, f) } + unsafe { vec::raw::buf_as_slice(p, n-1u, f) } } } diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index eb58d17c5f11..7d0421c07301 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -226,8 +226,7 @@ pub pure fn to_str_bytes(neg: bool, num: T, radix: uint, *ptr::mut_offset(mp, i) = '-' as u8; } - vec::raw::form_slice(ptr::offset(p, i), - len - i, f) + vec::raw::buf_as_slice(ptr::offset(p, i), len - i, f) } } } diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index a8286eb4e082..757a9e4b00de 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1838,7 +1838,7 @@ mod raw { * not bytes). */ #[inline(always)] - unsafe fn form_slice(p: *T, len: uint, f: fn(v: &[T]) -> U) -> U { + unsafe fn buf_as_slice(p: *T, len: uint, f: fn(v: &[T]) -> U) -> U { let pair = (p, len * sys::size_of::()); let v : *(&blk/[T]) = ::cast::reinterpret_cast(&addr_of(&pair)); diff --git a/src/libstd/ebml2.rs b/src/libstd/ebml2.rs index 106537603682..3ed6426b829f 100644 --- a/src/libstd/ebml2.rs +++ b/src/libstd/ebml2.rs @@ -384,7 +384,25 @@ impl Serializer: serialization2::Serializer { fail ~"Unimplemented: serializing a float"; } - fn emit_str(&self, v: &str) { self.wr_tagged_str(EsStr as uint, v) } + fn emit_char(&self, _v: char) { + fail ~"Unimplemented: serializing a char"; + } + + fn emit_borrowed_str(&self, v: &str) { + self.wr_tagged_str(EsStr as uint, v) + } + + fn emit_owned_str(&self, v: &str) { + self.emit_borrowed_str(v) + } + + fn emit_managed_str(&self, v: &str) { + self.emit_borrowed_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()) { self._emit_label(name); @@ -397,25 +415,33 @@ impl Serializer: serialization2::Serializer { } fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) { f() } - fn emit_vec(&self, len: uint, f: fn()) { + fn emit_borrowed_vec(&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()) { self.wr_tag(EsVecElt as uint, f) } - fn emit_box(&self, f: fn()) { f() } - fn emit_uniq(&self, f: fn()) { f() } fn emit_rec(&self, f: fn()) { f() } - fn emit_rec_field(&self, f_name: &str, _f_idx: uint, f: fn()) { - self._emit_label(f_name); + fn emit_struct(&self, _name: &str, f: fn()) { f() } + fn emit_field(&self, name: &str, _idx: uint, f: fn()) { + self._emit_label(name); f() } - fn emit_tup(&self, _sz: uint, f: fn()) { f() } + + fn emit_tup(&self, _len: uint, f: fn()) { f() } fn emit_tup_elt(&self, _idx: uint, f: fn()) { f() } } @@ -525,9 +551,22 @@ impl Deserializer: serialization2::Deserializer { fn read_f32(&self) -> f32 { fail ~"read_f32()"; } fn read_float(&self) -> float { fail ~"read_float()"; } - fn read_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) } + fn read_char(&self) -> char { fail ~"read_char()"; } + + fn read_owned_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) } + fn read_managed_str(&self) -> @str { fail ~"read_managed_str()"; } // 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); @@ -548,8 +587,17 @@ impl Deserializer: serialization2::Deserializer { f() } - fn read_vec(&self, f: fn(uint) -> T) -> T { - debug!("read_vec()"); + fn read_owned_vec(&self, f: fn(uint) -> T) -> T { + debug!("read_owned_vec()"); + do self.push_doc(self.next_doc(EsVec)) { + let len = self._next_uint(EsVecLen); + debug!(" len=%u", len); + f(len) + } + } + + 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); @@ -562,30 +610,24 @@ impl Deserializer: serialization2::Deserializer { self.push_doc(self.next_doc(EsVecElt), f) } - fn read_box(&self, f: fn() -> T) -> T { - debug!("read_box()"); + fn read_rec(&self, f: fn() -> T) -> T { + debug!("read_rec()"); f() } - fn read_uniq(&self, f: fn() -> T) -> T { - debug!("read_uniq()"); + fn read_struct(&self, name: &str, f: fn() -> T) -> T { + debug!("read_struct(name=%s)", name); f() } - fn read_rec(&self, f: fn() -> T) -> T { - debug!("read_rec()"); - f() - } - - fn read_rec_field(&self, f_name: &str, f_idx: uint, - f: fn() -> T) -> T { - debug!("read_rec_field(%s, idx=%u)", f_name, f_idx); - self._check_label(f_name); + fn read_field(&self, name: &str, idx: uint, f: fn() -> T) -> T { + debug!("read_field(name=%s, idx=%u)", name, idx); + self._check_label(name); f() } - fn read_tup(&self, sz: uint, f: fn() -> T) -> T { - debug!("read_tup(sz=%u)", sz); + fn read_tup(&self, len: uint, f: fn() -> T) -> T { + debug!("read_tup(len=%u)", len); f() } @@ -595,7 +637,6 @@ impl Deserializer: serialization2::Deserializer { } } - // ___________________________________________________________________________ // Testing diff --git a/src/libstd/json.rs b/src/libstd/json.rs index c8d9e3c24632..d3713bdb29d5 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -6,10 +6,7 @@ //! json serialization use core::cmp::{Eq, Ord}; -use result::{Result, Ok, Err}; use io::{WriterUtil, ReaderUtil}; -use map::HashMap; -use map::Map; use send_map::linear; use sort::Sort; @@ -95,10 +92,15 @@ pub impl Serializer: serialization2::Serializer { self.wr.write_str(float::to_str(v, 6u)); } - fn emit_str(&self, v: &str) { - let s = escape_str(v); - self.wr.write_str(s); - } + fn emit_char(&self, v: char) { self.emit_borrowed_str(str::from_char(v)) } + + fn emit_borrowed_str(&self, v: &str) { self.wr.write_str(escape_str(v)) } + fn emit_owned_str(&self, v: &str) { self.emit_borrowed_str(v) } + fn emit_managed_str(&self, v: &str) { self.emit_borrowed_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()) { if name != "option" { fail ~"only supports option enum" } @@ -115,32 +117,41 @@ pub impl Serializer: serialization2::Serializer { f() } - fn emit_vec(&self, _len: uint, f: fn()) { + fn emit_borrowed_vec(&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()) { if idx != 0 { self.wr.write_char(','); } f() } - fn emit_box(&self, f: fn()) { f() } - fn emit_uniq(&self, f: fn()) { f() } fn emit_rec(&self, f: fn()) { self.wr.write_char('{'); f(); self.wr.write_char('}'); } - fn emit_rec_field(&self, name: &str, idx: uint, f: fn()) { + fn emit_struct(&self, _name: &str, f: fn()) { + self.wr.write_char('{'); + f(); + self.wr.write_char('}'); + } + fn emit_field(&self, name: &str, idx: uint, f: fn()) { if idx != 0 { self.wr.write_char(','); } self.wr.write_str(escape_str(name)); self.wr.write_char(':'); f(); } - fn emit_tup(&self, sz: uint, f: fn()) { - self.emit_vec(sz, f); + + fn emit_tup(&self, len: uint, f: fn()) { + self.emit_borrowed_vec(len, f); } fn emit_tup_elt(&self, idx: uint, f: fn()) { self.emit_vec_elt(idx, f) @@ -185,7 +196,15 @@ pub impl PrettySerializer: serialization2::Serializer { self.wr.write_str(float::to_str(v, 6u)); } - fn emit_str(&self, v: &str) { self.wr.write_str(escape_str(v)); } + fn emit_char(&self, v: char) { self.emit_borrowed_str(str::from_char(v)) } + + fn emit_borrowed_str(&self, v: &str) { self.wr.write_str(escape_str(v)); } + fn emit_owned_str(&self, v: &str) { self.emit_borrowed_str(v) } + fn emit_managed_str(&self, v: &str) { self.emit_borrowed_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()) { if name != "option" { fail ~"only supports option enum" } @@ -202,14 +221,19 @@ pub impl PrettySerializer: serialization2::Serializer { f() } - fn emit_vec(&self, _len: uint, f: fn()) { + fn emit_borrowed_vec(&self, _len: uint, f: fn()) { self.wr.write_char('['); self.indent += 2; f(); self.indent -= 2; 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()) { if idx == 0 { self.wr.write_char('\n'); @@ -220,8 +244,6 @@ pub impl PrettySerializer: serialization2::Serializer { f() } - fn emit_box(&self, f: fn()) { f() } - fn emit_uniq(&self, f: fn()) { f() } fn emit_rec(&self, f: fn()) { self.wr.write_char('{'); self.indent += 2; @@ -229,7 +251,10 @@ pub impl PrettySerializer: serialization2::Serializer { self.indent -= 2; self.wr.write_char('}'); } - fn emit_rec_field(&self, name: &str, idx: uint, f: fn()) { + fn emit_struct(&self, _name: &str, f: fn()) { + self.emit_rec(f) + } + fn emit_field(&self, name: &str, idx: uint, f: fn()) { if idx == 0 { self.wr.write_char('\n'); } else { @@ -241,43 +266,39 @@ pub impl PrettySerializer: serialization2::Serializer { f(); } fn emit_tup(&self, sz: uint, f: fn()) { - self.emit_vec(sz, f); + self.emit_borrowed_vec(sz, f); } fn emit_tup_elt(&self, idx: uint, f: fn()) { self.emit_vec_elt(idx, f) } } -pub fn to_serializer(ser: &S, json: &Json) { - match *json { - Number(f) => ser.emit_float(f), - String(ref s) => ser.emit_str(*s), - Boolean(b) => ser.emit_bool(b), - List(v) => { - do ser.emit_vec(v.len()) || { - for v.eachi |i, elt| { - ser.emit_vec_elt(i, || to_serializer(ser, elt)) - } - } - } - Object(ref o) => { - do ser.emit_rec || { - let mut idx = 0; - for o.each |key, value| { - do ser.emit_rec_field(*key, idx) { - to_serializer(ser, value); +pub impl Json: serialization2::Serializable { + fn serialize(&self, s: &S) { + match *self { + Number(v) => v.serialize(s), + String(ref v) => v.serialize(s), + Boolean(v) => v.serialize(s), + List(v) => v.serialize(s), + Object(ref v) => { + do s.emit_rec || { + let mut idx = 0; + for v.each |key, value| { + do s.emit_field(*key, idx) { + value.serialize(s); + } + idx += 1; } - idx += 1; } - } + }, + Null => s.emit_nil(), } - Null => ser.emit_nil(), } } /// Serializes a json value into a io::writer pub fn to_writer(wr: io::Writer, json: &Json) { - to_serializer(&Serializer(wr), json) + json.serialize(&Serializer(wr)) } /// Serializes a json value into a string @@ -287,7 +308,7 @@ pub fn to_str(json: &Json) -> ~str { /// Serializes a json value into a io::writer pub fn to_pretty_writer(wr: io::Writer, json: &Json) { - to_serializer(&PrettySerializer(wr), json) + json.serialize(&PrettySerializer(wr)) } /// Serializes a json value into a string @@ -739,14 +760,35 @@ pub impl Deserializer: serialization2::Deserializer { } } - fn read_str(&self) -> ~str { - debug!("read_str"); + fn read_char(&self) -> char { + let v = str::chars(self.read_owned_str()); + if v.len() != 1 { fail ~"string must have one character" } + v[0] + } + + fn read_owned_str(&self) -> ~str { + debug!("read_owned_str"); match *self.pop() { String(ref s) => copy *s, _ => fail ~"not a string" } } + fn read_managed_str(&self) -> @str { + // FIXME(#3604): There's no way to convert from a ~str to a @str. + fail ~"read_managed_str()"; + } + + 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); if name != ~"option" { fail ~"only supports the option enum" } @@ -768,8 +810,19 @@ pub impl Deserializer: serialization2::Deserializer { f() } - fn read_vec(&self, f: fn(uint) -> T) -> T { - debug!("read_vec()"); + fn read_owned_vec(&self, f: fn(uint) -> T) -> T { + debug!("read_owned_vec()"); + let len = match *self.peek() { + List(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()"); let len = match *self.peek() { List(ref list) => list.len(), _ => fail ~"not a list", @@ -793,16 +846,6 @@ pub impl Deserializer: serialization2::Deserializer { } } - fn read_box(&self, f: fn() -> T) -> T { - debug!("read_box()"); - f() - } - - fn read_uniq(&self, f: fn() -> T) -> T { - debug!("read_uniq()"); - f() - } - fn read_rec(&self, f: fn() -> T) -> T { debug!("read_rec()"); let value = f(); @@ -810,17 +853,23 @@ pub impl Deserializer: serialization2::Deserializer { value } - fn read_rec_field(&self, f_name: &str, f_idx: uint, - f: fn() -> T) -> T { - debug!("read_rec_field(%s, idx=%u)", f_name, f_idx); + fn read_struct(&self, _name: &str, f: fn() -> T) -> T { + debug!("read_struct()"); + let value = f(); + self.pop(); + value + } + + fn read_field(&self, name: &str, idx: uint, f: fn() -> T) -> T { + debug!("read_rec_field(%s, idx=%u)", name, idx); let top = self.peek(); match *top { Object(ref obj) => { // FIXME(#3148) This hint should not be necessary. let obj: &self/~Object = obj; - match obj.find_ref(&(f_name.to_unique())) { - None => fail fmt!("no such field: %s", f_name), + match obj.find_ref(&name.to_unique()) { + None => fail fmt!("no such field: %s", name), Some(json) => { self.stack.push(json); f() @@ -837,8 +886,8 @@ pub impl Deserializer: serialization2::Deserializer { } } - fn read_tup(&self, sz: uint, f: fn() -> T) -> T { - debug!("read_tup(sz=%u)", sz); + fn read_tup(&self, len: uint, f: fn() -> T) -> T { + debug!("read_tup(len=%u)", len); let value = f(); self.pop(); value diff --git a/src/libstd/prettyprint2.rs b/src/libstd/prettyprint2.rs index 68421a217ee0..87af519eb123 100644 --- a/src/libstd/prettyprint2.rs +++ b/src/libstd/prettyprint2.rs @@ -12,7 +12,7 @@ pub fn Serializer(wr: io::Writer) -> Serializer { Serializer { wr: wr } } -impl Serializer: serialization2::Serializer { +pub impl Serializer: serialization2::Serializer { fn emit_nil(&self) { self.wr.write_str(~"()") } @@ -73,10 +73,37 @@ impl Serializer: serialization2::Serializer { self.wr.write_str(fmt!("%?_f32", v)); } - fn emit_str(&self, v: &str) { + fn emit_char(&self, v: char) { self.wr.write_str(fmt!("%?", v)); } + fn emit_borrowed_str(&self, v: &str) { + self.wr.write_str(fmt!("&%?", v)); + } + + fn emit_owned_str(&self, v: &str) { + self.wr.write_str(fmt!("~%?", v)); + } + + fn emit_managed_str(&self, v: &str) { + self.wr.write_str(fmt!("@%?", v)); + } + + fn emit_borrowed(&self, f: fn()) { + self.wr.write_str(~"&"); + f(); + } + + fn emit_owned(&self, f: fn()) { + self.wr.write_str(~"~"); + f(); + } + + fn emit_managed(&self, f: fn()) { + self.wr.write_str(~"@"); + f(); + } + fn emit_enum(&self, _name: &str, f: fn()) { f(); } @@ -94,24 +121,26 @@ impl Serializer: serialization2::Serializer { f(); } - fn emit_vec(&self, _len: uint, f: fn()) { - self.wr.write_str(~"["); + fn emit_borrowed_vec(&self, _len: uint, f: fn()) { + self.wr.write_str(~"&["); f(); self.wr.write_str(~"]"); } - fn emit_vec_elt(&self, idx: uint, f: fn()) { - if idx > 0u { self.wr.write_str(~", "); } + fn emit_owned_vec(&self, _len: uint, f: fn()) { + self.wr.write_str(~"~["); f(); + self.wr.write_str(~"]"); } - fn emit_box(&self, f: fn()) { - self.wr.write_str(~"@"); + fn emit_managed_vec(&self, _len: uint, f: fn()) { + self.wr.write_str(~"@["); f(); + self.wr.write_str(~"]"); } - fn emit_uniq(&self, f: fn()) { - self.wr.write_str(~"~"); + fn emit_vec_elt(&self, idx: uint, f: fn()) { + if idx > 0u { self.wr.write_str(~", "); } f(); } @@ -121,14 +150,20 @@ impl Serializer: serialization2::Serializer { self.wr.write_str(~"}"); } - fn emit_rec_field(&self, f_name: &str, f_idx: uint, f: fn()) { - if f_idx > 0u { self.wr.write_str(~", "); } - self.wr.write_str(f_name); + fn emit_struct(&self, name: &str, f: fn()) { + self.wr.write_str(fmt!("%s {", name)); + f(); + self.wr.write_str(~"}"); + } + + fn emit_field(&self, name: &str, idx: uint, f: fn()) { + if idx > 0u { self.wr.write_str(~", "); } + self.wr.write_str(name); self.wr.write_str(~": "); f(); } - fn emit_tup(&self, _sz: uint, f: fn()) { + fn emit_tup(&self, _len: uint, f: fn()) { self.wr.write_str(~"("); f(); self.wr.write_str(~")"); diff --git a/src/libstd/serialization2.rs b/src/libstd/serialization2.rs index 9cceccf42c6f..5173ef163a22 100644 --- a/src/libstd/serialization2.rs +++ b/src/libstd/serialization2.rs @@ -24,19 +24,30 @@ pub trait Serializer { fn emit_float(&self, v: float); fn emit_f64(&self, v: f64); fn emit_f32(&self, v: f32); - fn emit_str(&self, v: &str); + fn emit_char(&self, v: char); + fn emit_borrowed_str(&self, v: &str); + fn emit_owned_str(&self, v: &str); + fn emit_managed_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()); fn emit_enum_variant_arg(&self, idx: uint, f: fn()); - fn emit_vec(&self, len: 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_box(&self, f: fn()); - fn emit_uniq(&self, f: fn()); + fn emit_rec(&self, f: fn()); - fn emit_rec_field(&self, f_name: &str, f_idx: uint, f: fn()); - fn emit_tup(&self, sz: uint, f: fn()); + fn emit_struct(&self, name: &str, 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()); } @@ -57,29 +68,43 @@ pub trait Deserializer { fn read_f64(&self) -> f64; fn read_f32(&self) -> f32; fn read_float(&self) -> float; - fn read_str(&self) -> ~str; + fn read_char(&self) -> char; + fn read_owned_str(&self) -> ~str; + fn read_managed_str(&self) -> @str; // Compound types: fn read_enum(&self, name: &str, f: fn() -> T) -> T; fn read_enum_variant(&self, f: fn(uint) -> T) -> T; fn read_enum_variant_arg(&self, idx: uint, f: fn() -> T) -> T; - fn read_vec(&self, f: fn(uint) -> 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; - fn read_box(&self, f: fn() -> T) -> T; - fn read_uniq(&self, f: fn() -> T) -> T; + fn read_rec(&self, f: fn() -> T) -> T; - fn read_rec_field(&self, f_name: &str, f_idx: uint, f: fn() -> T) -> T; + fn read_struct(&self, name: &str, 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; } pub trait Serializable { fn serialize(&self, s: &S); +} + +pub trait Deserializable { static fn deserialize(&self, d: &D) -> self; } pub impl uint: Serializable { fn serialize(&self, s: &S) { s.emit_uint(*self) } +} + +pub impl uint: Deserializable { static fn deserialize(&self, d: &D) -> uint { d.read_uint() } @@ -87,6 +112,9 @@ pub impl uint: Serializable { pub impl u8: Serializable { fn serialize(&self, s: &S) { s.emit_u8(*self) } +} + +pub impl u8: Deserializable { static fn deserialize(&self, d: &D) -> u8 { d.read_u8() } @@ -94,6 +122,9 @@ pub impl u8: Serializable { pub impl u16: Serializable { fn serialize(&self, s: &S) { s.emit_u16(*self) } +} + +pub impl u16: Deserializable { static fn deserialize(&self, d: &D) -> u16 { d.read_u16() } @@ -101,6 +132,9 @@ pub impl u16: Serializable { pub impl u32: Serializable { fn serialize(&self, s: &S) { s.emit_u32(*self) } +} + +pub impl u32: Deserializable { static fn deserialize(&self, d: &D) -> u32 { d.read_u32() } @@ -108,6 +142,9 @@ pub impl u32: Serializable { pub impl u64: Serializable { fn serialize(&self, s: &S) { s.emit_u64(*self) } +} + +pub impl u64: Deserializable { static fn deserialize(&self, d: &D) -> u64 { d.read_u64() } @@ -115,6 +152,9 @@ pub impl u64: Serializable { pub impl int: Serializable { fn serialize(&self, s: &S) { s.emit_int(*self) } +} + +pub impl int: Deserializable { static fn deserialize(&self, d: &D) -> int { d.read_int() } @@ -122,6 +162,9 @@ pub impl int: Serializable { pub impl i8: Serializable { fn serialize(&self, s: &S) { s.emit_i8(*self) } +} + +pub impl i8: Deserializable { static fn deserialize(&self, d: &D) -> i8 { d.read_i8() } @@ -129,6 +172,9 @@ pub impl i8: Serializable { pub impl i16: Serializable { fn serialize(&self, s: &S) { s.emit_i16(*self) } +} + +pub impl i16: Deserializable { static fn deserialize(&self, d: &D) -> i16 { d.read_i16() } @@ -136,6 +182,9 @@ pub impl i16: Serializable { pub impl i32: Serializable { fn serialize(&self, s: &S) { s.emit_i32(*self) } +} + +pub impl i32: Deserializable { static fn deserialize(&self, d: &D) -> i32 { d.read_i32() } @@ -143,20 +192,43 @@ pub impl i32: Serializable { pub impl i64: Serializable { fn serialize(&self, s: &S) { s.emit_i64(*self) } +} + +pub impl i64: Deserializable { static fn deserialize(&self, d: &D) -> i64 { d.read_i64() } } +pub impl &str: Serializable { + fn serialize(&self, s: &S) { s.emit_borrowed_str(*self) } +} + pub impl ~str: Serializable { - fn serialize(&self, s: &S) { s.emit_str(*self) } + fn serialize(&self, s: &S) { s.emit_owned_str(*self) } +} + +pub impl ~str: Deserializable { static fn deserialize(&self, d: &D) -> ~str { - d.read_str() + d.read_owned_str() + } +} + +pub impl @str: Serializable { + fn serialize(&self, s: &S) { s.emit_managed_str(*self) } +} + +pub impl @str: Deserializable { + static fn deserialize(&self, d: &D) -> @str { + d.read_managed_str() } } pub impl float: Serializable { fn serialize(&self, s: &S) { s.emit_float(*self) } +} + +pub impl float: Deserializable { static fn deserialize(&self, d: &D) -> float { d.read_float() } @@ -164,12 +236,18 @@ pub impl float: Serializable { pub impl f32: Serializable { fn serialize(&self, s: &S) { s.emit_f32(*self) } +} + +pub impl f32: Deserializable { static fn deserialize(&self, d: &D) -> f32 { d.read_f32() } } pub impl f64: Serializable { fn serialize(&self, s: &S) { s.emit_f64(*self) } +} + +pub impl f64: Deserializable { static fn deserialize(&self, d: &D) -> f64 { d.read_f64() } @@ -177,6 +255,9 @@ pub impl f64: Serializable { pub impl bool: Serializable { fn serialize(&self, s: &S) { s.emit_bool(*self) } +} + +pub impl bool: Deserializable { static fn deserialize(&self, d: &D) -> bool { d.read_bool() } @@ -184,42 +265,67 @@ pub impl bool: Serializable { pub impl (): Serializable { fn serialize(&self, s: &S) { s.emit_nil() } +} + +pub impl (): Deserializable { static fn deserialize(&self, d: &D) -> () { d.read_nil() } } -pub impl @T: Serializable { +pub impl &T: Serializable { fn serialize(&self, s: &S) { - s.emit_box(|| (*self).serialize(s)) - } - - static fn deserialize(&self, d: &D) -> @T { - d.read_box(|| @deserialize(d)) + s.emit_borrowed(|| (**self).serialize(s)) } } pub impl ~T: Serializable { fn serialize(&self, s: &S) { - s.emit_uniq(|| (*self).serialize(s)) + s.emit_owned(|| (**self).serialize(s)) } +} +pub impl ~T: Deserializable { static fn deserialize(&self, d: &D) -> ~T { - d.read_uniq(|| ~deserialize(d)) + d.read_owned(|| ~deserialize(d)) + } +} + +pub impl @T: Serializable { + fn serialize(&self, s: &S) { + s.emit_managed(|| (**self).serialize(s)) + } +} + +pub impl @T: Deserializable { + static fn deserialize(&self, d: &D) -> @T { + d.read_managed(|| @deserialize(d)) + } +} + +pub impl &[T]: Serializable { + fn serialize(&self, s: &S) { + do s.emit_borrowed_vec(self.len()) { + for self.eachi |i, e| { + s.emit_vec_elt(i, || e.serialize(s)) + } + } } } pub impl ~[T]: Serializable { fn serialize(&self, s: &S) { - do s.emit_vec(self.len()) { + do s.emit_owned_vec(self.len()) { for self.eachi |i, e| { s.emit_vec_elt(i, || e.serialize(s)) } } } +} +pub impl ~[T]: Deserializable { static fn deserialize(&self, d: &D) -> ~[T] { - do d.read_vec |len| { + do d.read_owned_vec |len| { do vec::from_fn(len) |i| { d.read_vec_elt(i, || deserialize(d)) } @@ -227,6 +333,26 @@ pub impl ~[T]: Serializable { } } +pub impl @[T]: Serializable { + fn serialize(&self, s: &S) { + do s.emit_managed_vec(self.len()) { + for self.eachi |i, e| { + s.emit_vec_elt(i, || e.serialize(s)) + } + } + } +} + +pub impl @[T]: Deserializable { + static fn deserialize(&self, d: &D) -> @[T] { + do d.read_managed_vec |len| { + do at_vec::from_fn(len) |i| { + d.read_vec_elt(i, || deserialize(d)) + } + } + } +} + pub impl Option: Serializable { fn serialize(&self, s: &S) { do s.emit_enum(~"option") { @@ -240,7 +366,9 @@ pub impl Option: Serializable { } } } +} +pub impl Option: Deserializable { static fn deserialize(&self, d: &D) -> Option { do d.read_enum(~"option") { do d.read_enum_variant |i| { @@ -268,7 +396,12 @@ pub impl< } } } +} +pub impl< + T0: Deserializable, + T1: Deserializable +> (T0, T1): Deserializable { static fn deserialize(&self, d: &D) -> (T0, T1) { do d.read_tup(2) { ( @@ -295,7 +428,13 @@ pub impl< } } } +} +pub impl< + T0: Deserializable, + T1: Deserializable, + T2: Deserializable +> (T0, T1, T2): Deserializable { static fn deserialize(&self, d: &D) -> (T0, T1, T2) { do d.read_tup(3) { ( @@ -325,7 +464,14 @@ pub impl< } } } +} +pub impl< + T0: Deserializable, + T1: Deserializable, + T2: Deserializable, + T3: Deserializable +> (T0, T1, T2, T3): Deserializable { static fn deserialize(&self, d: &D) -> (T0, T1, T2, T3) { do d.read_tup(4) { ( @@ -358,7 +504,15 @@ pub impl< } } } +} +pub impl< + T0: Deserializable, + T1: Deserializable, + T2: Deserializable, + T3: Deserializable, + T4: Deserializable +> (T0, T1, T2, T3, T4): Deserializable { static fn deserialize(&self, d: &D) -> (T0, T1, T2, T3, T4) { do d.read_tup(5) { @@ -379,12 +533,12 @@ pub impl< // In some cases, these should eventually be coded as traits. pub trait SerializerHelpers { - fn emit_from_vec(&self, v: ~[T], f: fn(v: &T)); + fn emit_from_vec(&self, v: &[T], f: fn(&T)); } pub impl S: SerializerHelpers { - fn emit_from_vec(&self, v: ~[T], f: fn(v: &T)) { - do self.emit_vec(v.len()) { + fn emit_from_vec(&self, v: &[T], f: fn(&T)) { + do self.emit_owned_vec(v.len()) { for v.eachi |i, e| { do self.emit_vec_elt(i) { f(e) @@ -400,7 +554,7 @@ pub trait DeserializerHelpers { pub impl D: DeserializerHelpers { fn read_to_vec(&self, f: fn() -> T) -> ~[T] { - do self.read_vec |len| { + do self.read_owned_vec |len| { do vec::from_fn(len) |i| { self.read_vec_elt(i, || f()) } diff --git a/src/libsyntax/ext/auto_serialize2.rs b/src/libsyntax/ext/auto_serialize2.rs index 099ba67713fc..99f837a4c844 100644 --- a/src/libsyntax/ext/auto_serialize2.rs +++ b/src/libsyntax/ext/auto_serialize2.rs @@ -1,46 +1,60 @@ /* -The compiler code necessary to implement the #[auto_serialize2] -extension. The idea here is that type-defining items may be tagged -with #[auto_serialize2], which will cause us to generate a little -companion module with the same name as the item. +The compiler code necessary to implement the #[auto_serialize2] and +#[auto_deserialize2] extension. The idea here is that type-defining items may +be tagged with #[auto_serialize2] and #[auto_deserialize2], which will cause +us to generate a little companion module with the same name as the item. For example, a type like: - type node_id = uint; + #[auto_serialize2] + #[auto_deserialize2] + struct Node {id: uint} -would generate two functions like: +would generate two implementations like: - impl node_id: Serializable { - fn serialize(s: S) { - s.emit_uint(self) + impl Node: Serializable { + fn serialize(s: &S) { + do s.emit_struct("Node") { + s.emit_field("id", 0, || s.emit_uint(self)) + } } + } - static fn deserialize(d: D) -> node_id { - d.read_uint() + impl node_id: Deserializable { + static fn deserialize(d: &D) -> Node { + do d.read_struct("Node") { + Node { + id: d.read_field(~"x", 0, || deserialize(d)) + } + } } } Other interesting scenarios are whe the item has type parameters or references other non-built-in types. A type definition like: + #[auto_serialize2] + #[auto_deserialize2] type spanned = {node: T, span: span}; would yield functions like: impl spanned: Serializable { - fn serialize(s: S) { + fn serialize(s: &S) { do s.emit_rec { - s.emit_rec_field("node", 0, self.node.serialize(s)); - s.emit_rec_field("span", 1, self.span.serialize(s)); + s.emit_field("node", 0, || self.node.serialize(s)); + s.emit_field("span", 1, || self.span.serialize(s)); } } + } - static fn deserialize(d: D) -> spanned { + impl spanned: Deserializable { + static fn deserialize(d: &D) -> spanned { do d.read_rec { { - node: d.read_rec_field(~"node", 0, || deserialize(d)), - span: d.read_rec_field(~"span", 1, || deserialize(d)), + node: d.read_field(~"node", 0, || deserialize(d)), + span: d.read_field(~"span", 1, || deserialize(d)), } } } @@ -63,7 +77,8 @@ use codemap::span; use std::map; use std::map::HashMap; -export expand; +export expand_auto_serialize; +export expand_auto_deserialize; // Transitional reexports so qquote can find the paths it is looking for mod syntax { @@ -71,45 +86,130 @@ mod syntax { pub use parse; } -fn expand(cx: ext_ctxt, - span: span, - _mitem: ast::meta_item, - in_items: ~[@ast::item]) -> ~[@ast::item] { - fn not_auto_serialize2(a: &ast::attribute) -> bool { - attr::get_attr_name(*a) != ~"auto_serialize2" +fn expand_auto_serialize( + cx: ext_ctxt, + span: span, + _mitem: ast::meta_item, + in_items: ~[@ast::item] +) -> ~[@ast::item] { + fn is_auto_serialize2(a: &ast::attribute) -> bool { + attr::get_attr_name(*a) == ~"auto_serialize2" } fn filter_attrs(item: @ast::item) -> @ast::item { - @{attrs: vec::filter(item.attrs, not_auto_serialize2), + @{attrs: vec::filter(item.attrs, |a| !is_auto_serialize2(a)), .. *item} } do vec::flat_map(in_items) |item| { - match item.node { - ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { - ~[ - filter_attrs(*item), - mk_rec_impl(cx, item.span, item.ident, fields, tps), - ] - }, - ast::item_class(@{ fields, _}, tps) => { - ~[ - filter_attrs(*item), - mk_struct_impl(cx, item.span, item.ident, fields, tps), - ] - }, - ast::item_enum(enum_def, tps) => { - ~[ - filter_attrs(*item), - mk_enum_impl(cx, item.span, item.ident, enum_def, tps), - ] - }, - _ => { - cx.span_err(span, ~"#[auto_serialize2] can only be applied \ - to structs, record types, and enum \ - definitions"); - ~[*item] + if item.attrs.any(is_auto_serialize2) { + match item.node { + ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { + let ser_impl = mk_rec_ser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + ast::item_class(@{ fields, _}, tps) => { + let ser_impl = mk_struct_ser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + ast::item_enum(enum_def, tps) => { + let ser_impl = mk_enum_ser_impl( + cx, + item.span, + item.ident, + enum_def, + tps + ); + + ~[filter_attrs(*item), ser_impl] + }, + _ => { + cx.span_err(span, ~"#[auto_serialize2] can only be \ + applied to structs, record types, \ + and enum definitions"); + ~[*item] + } + } + } else { + ~[*item] + } + } +} + +fn expand_auto_deserialize( + cx: ext_ctxt, + span: span, + _mitem: ast::meta_item, + in_items: ~[@ast::item] +) -> ~[@ast::item] { + fn is_auto_deserialize2(a: &ast::attribute) -> bool { + attr::get_attr_name(*a) == ~"auto_deserialize2" + } + + fn filter_attrs(item: @ast::item) -> @ast::item { + @{attrs: vec::filter(item.attrs, |a| !is_auto_deserialize2(a)), + .. *item} + } + + do vec::flat_map(in_items) |item| { + if item.attrs.any(is_auto_deserialize2) { + match item.node { + ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => { + let deser_impl = mk_rec_deser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + ast::item_class(@{ fields, _}, tps) => { + let deser_impl = mk_struct_deser_impl( + cx, + item.span, + item.ident, + fields, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + ast::item_enum(enum_def, tps) => { + let deser_impl = mk_enum_deser_impl( + cx, + item.span, + item.ident, + enum_def, + tps + ); + + ~[filter_attrs(*item), deser_impl] + }, + _ => { + cx.span_err(span, ~"#[auto_deserialize2] can only be \ + applied to structs, record types, \ + and enum definitions"); + ~[*item] + } } + } else { + ~[*item] } } } @@ -152,22 +252,11 @@ fn mk_impl( cx: ext_ctxt, span: span, ident: ast::ident, + path: @ast::path, tps: ~[ast::ty_param], - ser_body: @ast::expr, - deser_body: @ast::expr + f: fn(@ast::ty) -> @ast::method ) -> @ast::item { - // Make a path to the std::serialization2::Serializable trait. - let path = cx.path( - span, - ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialization2"), - cx.ident_of(~"Serializable"), - ] - ); - - // All the type parameters need to bound to - // std::serialization::Serializable. + // All the type parameters need to bound to the trait. let trait_tps = do tps.map |tp| { let t_bound = ast::bound_trait(@{ id: cx.next_id(), @@ -194,23 +283,72 @@ fn mk_impl( tps.map(|tp| cx.ty_path(span, ~[tp.ident], ~[])) ); - let methods = ~[ - mk_ser_method(cx, span, cx.expr_blk(ser_body)), - mk_deser_method(cx, span, ty, cx.expr_blk(deser_body)), - ]; - @{ // This is a new-style impl declaration. // XXX: clownshoes ident: ast::token::special_idents::clownshoes_extensions, attrs: ~[], id: cx.next_id(), - node: ast::item_impl(trait_tps, opt_trait, ty, methods), + node: ast::item_impl(trait_tps, opt_trait, ty, ~[f(ty)]), vis: ast::public, span: span, } } +fn mk_ser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + tps: ~[ast::ty_param], + body: @ast::expr +) -> @ast::item { + // Make a path to the std::serialization2::Serializable trait. + let path = cx.path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization2"), + cx.ident_of(~"Serializable"), + ] + ); + + mk_impl( + cx, + span, + ident, + path, + tps, + |_ty| mk_ser_method(cx, span, cx.expr_blk(body)) + ) +} + +fn mk_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + tps: ~[ast::ty_param], + body: @ast::expr +) -> @ast::item { + // Make a path to the std::serialization2::Deserializable trait. + let path = cx.path( + span, + ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialization2"), + cx.ident_of(~"Deserializable"), + ] + ); + + mk_impl( + cx, + span, + ident, + path, + tps, + |ty| mk_deser_method(cx, span, ty, cx.expr_blk(body)) + ) +} + fn mk_ser_method( cx: ext_ctxt, span: span, @@ -346,43 +484,137 @@ fn mk_deser_method( } } -fn mk_rec_impl( +fn mk_rec_ser_impl( cx: ext_ctxt, span: span, ident: ast::ident, fields: ~[ast::ty_field], tps: ~[ast::ty_param] ) -> @ast::item { - // Records and structs don't have the same fields types, but they share - // enough that if we extract the right subfields out we can share the - // serialization generator code. - let fields = do fields.map |field| { - { - span: field.span, - ident: field.node.ident, - mutbl: field.node.mt.mutbl, - } - }; + let fields = mk_ser_fields(cx, span, mk_rec_fields(fields)); - let ser_body = mk_ser_fields(cx, span, fields); - let deser_body = do mk_deser_fields(cx, span, fields) |fields| { - cx.expr(span, ast::expr_rec(fields, None)) - }; + // ast for `__s.emit_rec(|| $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_rec") + ), + ~[cx.lambda_stmts(span, fields)] + ); + + mk_ser_impl(cx, span, ident, tps, body) +} + +fn mk_rec_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[ast::ty_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_deser_fields(cx, span, mk_rec_fields(fields)); + + // ast for `read_rec(|| $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_rec") + ), + ~[ + cx.lambda_expr( + cx.expr( + span, + ast::expr_rec(fields, None) + ) + ) + ] + ); - mk_impl(cx, span, ident, tps, ser_body, deser_body) + mk_deser_impl(cx, span, ident, tps, body) } -fn mk_struct_impl( +fn mk_struct_ser_impl( cx: ext_ctxt, span: span, ident: ast::ident, fields: ~[@ast::struct_field], tps: ~[ast::ty_param] ) -> @ast::item { - // Records and structs don't have the same fields types, but they share - // enough that if we extract the right subfields out we can share the - // serialization generator code. - let fields = do fields.map |field| { + let fields = mk_ser_fields(cx, span, mk_struct_fields(fields)); + + // ast for `__s.emit_struct($(name), || $(fields))` + let ser_body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_struct") + ), + ~[ + cx.lit_str(span, @cx.str_of(ident)), + cx.lambda_stmts(span, fields), + ] + ); + + mk_ser_impl(cx, span, ident, tps, ser_body) +} + +fn mk_struct_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + fields: ~[@ast::struct_field], + tps: ~[ast::ty_param] +) -> @ast::item { + let fields = mk_deser_fields(cx, span, mk_struct_fields(fields)); + + // ast for `read_struct($(name), || $(fields))` + let body = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_struct") + ), + ~[ + cx.lit_str(span, @cx.str_of(ident)), + cx.lambda_expr( + cx.expr( + span, + ast::expr_struct( + cx.path(span, ~[ident]), + fields, + None + ) + ) + ), + ] + ); + + mk_deser_impl(cx, span, ident, tps, body) +} + +// Records and structs don't have the same fields types, but they share enough +// that if we extract the right subfields out we can share the serialization +// generator code. +type field = { span: span, ident: ast::ident, mutbl: ast::mutability }; + +fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] { + do fields.map |field| { + { + span: field.span, + ident: field.node.ident, + mutbl: field.node.mt.mutbl, + } + } +} + +fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] { + do fields.map |field| { let (ident, mutbl) = match field.node.kind { ast::named_field(ident, mutbl, _) => (ident, mutbl), _ => fail ~"[auto_serialize2] does not support \ @@ -397,22 +629,15 @@ fn mk_struct_impl( ast::class_immutable => ast::m_imm, }, } - }; - - let ser_body = mk_ser_fields(cx, span, fields); - let deser_body = do mk_deser_fields(cx, span, fields) |fields| { - cx.expr(span, ast::expr_struct(cx.path(span, ~[ident]), fields, None)) - }; - - mk_impl(cx, span, ident, tps, ser_body, deser_body) + } } fn mk_ser_fields( cx: ext_ctxt, span: span, - fields: ~[{ span: span, ident: ast::ident, mutbl: ast::mutability }] -) -> @ast::expr { - let stmts = do fields.mapi |idx, field| { + fields: ~[field] +) -> ~[@ast::stmt] { + do fields.mapi |idx, field| { // ast for `|| self.$(name).serialize(__s)` let expr_lambda = cx.lambda_expr( cx.expr_call( @@ -430,14 +655,14 @@ fn mk_ser_fields( ) ); - // ast for `__s.emit_rec_field($(name), $(idx), $(expr_lambda))` + // ast for `__s.emit_field($(name), $(idx), $(expr_lambda))` cx.stmt( cx.expr_call( span, cx.expr_field( span, cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_rec_field") + cx.ident_of(~"emit_field") ), ~[ cx.lit_str(span, @cx.str_of(field.ident)), @@ -446,27 +671,15 @@ fn mk_ser_fields( ] ) ) - }; - - // ast for `__s.emit_rec(|| $(stmts))` - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_rec") - ), - ~[cx.lambda_stmts(span, stmts)] - ) + } } fn mk_deser_fields( cx: ext_ctxt, span: span, - fields: ~[{ span: span, ident: ast::ident, mutbl: ast::mutability }], - f: fn(~[ast::field]) -> @ast::expr -) -> @ast::expr { - let fields = do fields.mapi |idx, field| { + fields: ~[{ span: span, ident: ast::ident, mutbl: ast::mutability }] +) -> ~[ast::field] { + do fields.mapi |idx, field| { // ast for `|| std::serialization2::deserialize(__d)` let expr_lambda = cx.lambda( cx.expr_blk( @@ -482,13 +695,13 @@ fn mk_deser_fields( ) ); - // ast for `__d.read_rec_field($(name), $(idx), $(expr_lambda))` + // ast for `__d.read_field($(name), $(idx), $(expr_lambda))` let expr: @ast::expr = cx.expr_call( span, cx.expr_field( span, cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_rec_field") + cx.ident_of(~"read_field") ), ~[ cx.lit_str(span, @cx.str_of(field.ident)), @@ -501,42 +714,41 @@ fn mk_deser_fields( node: { mutbl: field.mutbl, ident: field.ident, expr: expr }, span: span, } - }; - - // ast for `__d.read_rec(|| $(fields_expr))` - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_rec") - ), - ~[cx.lambda_expr(f(fields))] - ) + } } -fn mk_enum_impl( +fn mk_enum_ser_impl( cx: ext_ctxt, span: span, ident: ast::ident, enum_def: ast::enum_def, tps: ~[ast::ty_param] ) -> @ast::item { - let ser_body = mk_enum_ser_body( + let body = mk_enum_ser_body( cx, span, ident, enum_def.variants ); - let deser_body = mk_enum_deser_body( + mk_ser_impl(cx, span, ident, tps, body) +} + +fn mk_enum_deser_impl( + cx: ext_ctxt, + span: span, + ident: ast::ident, + enum_def: ast::enum_def, + tps: ~[ast::ty_param] +) -> @ast::item { + let body = mk_enum_deser_body( cx, span, ident, enum_def.variants ); - mk_impl(cx, span, ident, tps, ser_body, deser_body) + mk_deser_impl(cx, span, ident, tps, body) } fn ser_variant( diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 6c71fd8fcbc9..9a31cc1d8f67 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -82,8 +82,12 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> { syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext)); syntax_expanders.insert(~"auto_serialize", item_decorator(ext::auto_serialize::expand)); - syntax_expanders.insert(~"auto_serialize2", - item_decorator(ext::auto_serialize2::expand)); + syntax_expanders.insert( + ~"auto_serialize2", + item_decorator(ext::auto_serialize2::expand_auto_serialize)); + syntax_expanders.insert( + ~"auto_deserialize2", + item_decorator(ext::auto_serialize2::expand_auto_deserialize)); syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext)); syntax_expanders.insert(~"concat_idents", builtin(ext::concat_idents::expand_syntax_ext)); diff --git a/src/rustc/metadata/loader.rs b/src/rustc/metadata/loader.rs index 2ccaccf17a59..0a8354be71f7 100644 --- a/src/rustc/metadata/loader.rs +++ b/src/rustc/metadata/loader.rs @@ -193,7 +193,7 @@ fn get_metadata_section(os: os, vlen); let minsz = uint::min(vlen, csz); let mut version_ok = false; - do vec::raw::form_slice(cvbuf, minsz) |buf0| { + do vec::raw::buf_as_slice(cvbuf, minsz) |buf0| { version_ok = (buf0 == encoder::metadata_encoding_version); } @@ -202,7 +202,7 @@ fn get_metadata_section(os: os, let cvbuf1 = ptr::offset(cvbuf, vlen); debug!("inflating %u bytes of compressed metadata", csz - vlen); - do vec::raw::form_slice(cvbuf1, csz-vlen) |bytes| { + do vec::raw::buf_as_slice(cvbuf1, csz-vlen) |bytes| { let inflated = flate::inflate_bytes(bytes); found = move Some(@(move inflated)); } diff --git a/src/test/run-pass/auto_serialize2-box.rs b/src/test/run-pass/auto_serialize2-box.rs deleted file mode 100644 index e395b1bfe5de..000000000000 --- a/src/test/run-pass/auto_serialize2-box.rs +++ /dev/null @@ -1,73 +0,0 @@ -// xfail-test FIXME Blocked on (#3585) - -extern mod std; - -// These tests used to be separate files, but I wanted to refactor all -// the common code. - -use cmp::Eq; -use std::ebml2; -use io::Writer; -use std::serialization2::{Serializer, Serializable, deserialize}; -use std::prettyprint2; - -fn test_ser_and_deser( - a1: A, - expected: ~str -) { - // check the pretty printer: - let s = do io::with_str_writer |w| { - a1.serialize(&prettyprint2::Serializer(w)) - }; - debug!("s == %?", s); - assert s == expected; - - // check the EBML serializer: - let bytes = do io::with_bytes_writer |wr| { - let ebml_w = &ebml2::Serializer(wr); - a1.serialize(ebml_w) - }; - let d = ebml2::Doc(@bytes); - let a2: A = deserialize(&ebml2::Deserializer(d)); - assert a1 == a2; -} - -#[auto_serialize2] -enum Expr { - Val(uint), - Plus(@Expr, @Expr), - Minus(@Expr, @Expr) -} - -impl Expr : cmp::Eq { - pure fn eq(other: &Expr) -> bool { - match self { - Val(e0a) => { - match *other { - Val(e0b) => e0a == e0b, - _ => false - } - } - Plus(e0a, e1a) => { - match *other { - Plus(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - Minus(e0a, e1a) => { - match *other { - Minus(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - } - } - pure fn ne(other: &Expr) -> bool { !self.eq(other) } -} - -fn main() { - test_ser_and_deser(Plus(@Minus(@Val(3u), @Val(10u)), - @Plus(@Val(22u), @Val(5u))), - ~"Plus(@Minus(@Val(3u), @Val(10u)), \ - @Plus(@Val(22u), @Val(5u)))"); -} diff --git a/src/test/run-pass/auto_serialize2.rs b/src/test/run-pass/auto_serialize2.rs index ac526a8f0b6b..4503ea6c7e01 100644 --- a/src/test/run-pass/auto_serialize2.rs +++ b/src/test/run-pass/auto_serialize2.rs @@ -6,14 +6,13 @@ extern mod std; use cmp::Eq; use std::ebml2; use io::Writer; -use std::serialization2::{Serializer, Serializable, deserialize}; +use std::serialization2::{Serializable, Deserializable, deserialize}; use std::prettyprint2; -fn test_ser_and_deser( - a1: A, - expected: ~str +fn test_ser_and_deser( + a1: &A, + +expected: ~str ) { - // check the pretty printer: let s = do io::with_str_writer |w| { a1.serialize(&prettyprint2::Serializer(w)) @@ -28,7 +27,41 @@ fn test_ser_and_deser( }; let d = ebml2::Doc(@bytes); let a2: A = deserialize(&ebml2::Deserializer(d)); - assert a1 == a2; + assert *a1 == a2; +} + +#[auto_serialize2] +#[auto_deserialize2] +enum Expr { + Val(uint), + Plus(@Expr, @Expr), + Minus(@Expr, @Expr) +} + +impl Expr : cmp::Eq { + pure fn eq(other: &Expr) -> bool { + match self { + Val(e0a) => { + match *other { + Val(e0b) => e0a == e0b, + _ => false + } + } + Plus(e0a, e1a) => { + match *other { + Plus(e0b, e1b) => e0a == e0b && e1a == e1b, + _ => false + } + } + Minus(e0a, e1a) => { + match *other { + Minus(e0b, e1b) => e0a == e0b && e1a == e1b, + _ => false + } + } + } + } + pure fn ne(other: &Expr) -> bool { !self.eq(other) } } impl AnEnum : cmp::Eq { @@ -73,6 +106,7 @@ impl CLike : cmp::Eq { } #[auto_serialize2] +#[auto_deserialize2] type Spanned = {lo: uint, hi: uint, node: T}; impl Spanned : cmp::Eq { @@ -83,37 +117,47 @@ impl Spanned : cmp::Eq { } #[auto_serialize2] +#[auto_deserialize2] type SomeRec = {v: ~[uint]}; #[auto_serialize2] +#[auto_deserialize2] enum AnEnum = SomeRec; #[auto_serialize2] -type Point = {x: uint, y: uint}; +#[auto_deserialize2] +struct Point {x: uint, y: uint} #[auto_serialize2] +#[auto_deserialize2] enum Quark { Top(T), Bottom(T) } #[auto_serialize2] +#[auto_deserialize2] enum CLike { A, B, C } fn main() { - test_ser_and_deser({lo: 0u, hi: 5u, node: 22u}, + test_ser_and_deser(&Plus(@Minus(@Val(3u), @Val(10u)), + @Plus(@Val(22u), @Val(5u))), + ~"Plus(@Minus(@Val(3u), @Val(10u)), \ + @Plus(@Val(22u), @Val(5u)))"); + + test_ser_and_deser(&{lo: 0u, hi: 5u, node: 22u}, ~"{lo: 0u, hi: 5u, node: 22u}"); - test_ser_and_deser(AnEnum({v: ~[1u, 2u, 3u]}), - ~"AnEnum({v: [1u, 2u, 3u]})"); + test_ser_and_deser(&AnEnum({v: ~[1u, 2u, 3u]}), + ~"AnEnum({v: ~[1u, 2u, 3u]})"); - test_ser_and_deser({x: 3u, y: 5u}, ~"{x: 3u, y: 5u}"); + test_ser_and_deser(&Point {x: 3u, y: 5u}, ~"Point {x: 3u, y: 5u}"); - test_ser_and_deser(~[1u, 2u, 3u], ~"[1u, 2u, 3u]"); + test_ser_and_deser(&@[1u, 2u, 3u], ~"@[1u, 2u, 3u]"); - test_ser_and_deser(Top(22u), ~"Top(22u)"); - test_ser_and_deser(Bottom(222u), ~"Bottom(222u)"); + test_ser_and_deser(&Top(22u), ~"Top(22u)"); + test_ser_and_deser(&Bottom(222u), ~"Bottom(222u)"); - test_ser_and_deser(A, ~"A"); - test_ser_and_deser(B, ~"B"); + test_ser_and_deser(&A, ~"A"); + test_ser_and_deser(&B, ~"B"); }