Merge remote-tracking branch 'erickt/incoming' into snap-2012-10-05

This commit is contained in:
Tim Chevalier 2012-10-08 09:03:26 -07:00
commit e3f1d342e7
22 changed files with 2266 additions and 4210 deletions

View file

@ -1,21 +1,35 @@
#[forbid(deprecated_mode)]; #[forbid(deprecated_mode)];
use serialization;
// Simple Extensible Binary Markup Language (ebml) reader and writer on a // Simple Extensible Binary Markup Language (ebml) reader and writer on a
// cursor model. See the specification here: // cursor model. See the specification here:
// http://www.matroska.org/technical/specs/rfc/index.html // http://www.matroska.org/technical/specs/rfc/index.html
use core::Option;
use option::{Some, None};
type EbmlTag = {id: uint, size: uint}; struct EbmlTag {
id: uint,
size: uint,
}
type EbmlState = {ebml_tag: EbmlTag, tag_pos: uint, data_pos: uint}; struct EbmlState {
ebml_tag: EbmlTag,
tag_pos: uint,
data_pos: uint,
}
// FIXME (#2739): When we have module renaming, make "reader" and "writer" // FIXME (#2739): When we have module renaming, make "reader" and "writer"
// separate modules within this file. // separate modules within this file.
// ebml reading // ebml reading
pub type Doc = {data: @~[u8], start: uint, end: uint}; struct Doc {
data: @~[u8],
start: uint,
end: uint,
}
type TaggedDoc = {tag: uint, doc: Doc}; struct TaggedDoc {
tag: uint,
doc: Doc,
}
impl Doc: ops::Index<uint,Doc> { impl Doc: ops::Index<uint,Doc> {
pure fn index(tag: uint) -> Doc { pure fn index(tag: uint) -> Doc {
@ -49,15 +63,17 @@ fn vuint_at(data: &[u8], start: uint) -> {val: uint, next: uint} {
} }
pub fn Doc(data: @~[u8]) -> Doc { pub fn Doc(data: @~[u8]) -> Doc {
return {data: data, start: 0u, end: vec::len::<u8>(*data)}; Doc { data: data, start: 0u, end: vec::len::<u8>(*data) }
} }
pub fn doc_at(data: @~[u8], start: uint) -> TaggedDoc { pub fn doc_at(data: @~[u8], start: uint) -> TaggedDoc {
let elt_tag = vuint_at(*data, start); let elt_tag = vuint_at(*data, start);
let elt_size = vuint_at(*data, elt_tag.next); let elt_size = vuint_at(*data, elt_tag.next);
let end = elt_size.next + elt_size.val; let end = elt_size.next + elt_size.val;
return {tag: elt_tag.val, TaggedDoc {
doc: {data: data, start: elt_size.next, end: end}}; tag: elt_tag.val,
doc: Doc { data: data, start: elt_size.next, end: end }
}
} }
pub fn maybe_get_doc(d: Doc, tg: uint) -> Option<Doc> { pub fn maybe_get_doc(d: Doc, tg: uint) -> Option<Doc> {
@ -67,19 +83,15 @@ pub fn maybe_get_doc(d: Doc, tg: uint) -> Option<Doc> {
let elt_size = vuint_at(*d.data, elt_tag.next); let elt_size = vuint_at(*d.data, elt_tag.next);
pos = elt_size.next + elt_size.val; pos = elt_size.next + elt_size.val;
if elt_tag.val == tg { if elt_tag.val == tg {
return Some::<Doc>({ return Some(Doc { data: d.data, start: elt_size.next, end: pos });
data: d.data,
start: elt_size.next,
end: pos
});
} }
} }
return None::<Doc>; None
} }
pub fn get_doc(d: Doc, tg: uint) -> Doc { pub fn get_doc(d: Doc, tg: uint) -> Doc {
match maybe_get_doc(d, tg) { match maybe_get_doc(d, tg) {
Some(d) => return d, Some(d) => d,
None => { None => {
error!("failed to find block with tag %u", tg); error!("failed to find block with tag %u", tg);
fail; fail;
@ -93,7 +105,8 @@ pub fn docs(d: Doc, it: fn(uint, Doc) -> bool) {
let elt_tag = vuint_at(*d.data, pos); let elt_tag = vuint_at(*d.data, pos);
let elt_size = vuint_at(*d.data, elt_tag.next); let elt_size = vuint_at(*d.data, elt_tag.next);
pos = elt_size.next + elt_size.val; pos = elt_size.next + elt_size.val;
if !it(elt_tag.val, {data: d.data, start: elt_size.next, end: pos}) { let doc = Doc { data: d.data, start: elt_size.next, end: pos };
if !it(elt_tag.val, doc) {
break; break;
} }
} }
@ -106,7 +119,8 @@ pub fn tagged_docs(d: Doc, tg: uint, it: fn(Doc) -> bool) {
let elt_size = vuint_at(*d.data, elt_tag.next); let elt_size = vuint_at(*d.data, elt_tag.next);
pos = elt_size.next + elt_size.val; pos = elt_size.next + elt_size.val;
if elt_tag.val == tg { if elt_tag.val == tg {
if !it({data: d.data, start: elt_size.next, end: pos}) { let doc = Doc { data: d.data, start: elt_size.next, end: pos };
if !it(doc) {
break; break;
} }
} }
@ -116,29 +130,29 @@ pub fn tagged_docs(d: Doc, tg: uint, it: fn(Doc) -> bool) {
pub fn doc_data(d: Doc) -> ~[u8] { vec::slice::<u8>(*d.data, d.start, d.end) } pub fn doc_data(d: Doc) -> ~[u8] { vec::slice::<u8>(*d.data, d.start, d.end) }
pub fn with_doc_data<T>(d: Doc, f: fn(x: &[u8]) -> T) -> T { pub fn with_doc_data<T>(d: Doc, f: fn(x: &[u8]) -> T) -> T {
return f(vec::view(*d.data, d.start, d.end)); f(vec::view(*d.data, d.start, d.end))
} }
pub fn doc_as_str(d: Doc) -> ~str { return str::from_bytes(doc_data(d)); } pub fn doc_as_str(d: Doc) -> ~str { str::from_bytes(doc_data(d)) }
pub fn doc_as_u8(d: Doc) -> u8 { pub fn doc_as_u8(d: Doc) -> u8 {
assert d.end == d.start + 1u; assert d.end == d.start + 1u;
return (*d.data)[d.start]; (*d.data)[d.start]
} }
pub fn doc_as_u16(d: Doc) -> u16 { pub fn doc_as_u16(d: Doc) -> u16 {
assert d.end == d.start + 2u; assert d.end == d.start + 2u;
return io::u64_from_be_bytes(*d.data, d.start, 2u) as u16; io::u64_from_be_bytes(*d.data, d.start, 2u) as u16
} }
pub fn doc_as_u32(d: Doc) -> u32 { pub fn doc_as_u32(d: Doc) -> u32 {
assert d.end == d.start + 4u; assert d.end == d.start + 4u;
return io::u64_from_be_bytes(*d.data, d.start, 4u) as u32; io::u64_from_be_bytes(*d.data, d.start, 4u) as u32
} }
pub fn doc_as_u64(d: Doc) -> u64 { pub fn doc_as_u64(d: Doc) -> u64 {
assert d.end == d.start + 8u; assert d.end == d.start + 8u;
return io::u64_from_be_bytes(*d.data, d.start, 8u); io::u64_from_be_bytes(*d.data, d.start, 8u)
} }
pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 } pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
@ -147,10 +161,9 @@ pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 }
pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 } pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 }
// ebml writing // ebml writing
type Writer_ = {writer: io::Writer, mut size_positions: ~[uint]}; struct Serializer {
writer: io::Writer,
pub enum Writer { priv mut size_positions: ~[uint],
Writer_(Writer_)
} }
fn write_sized_vuint(w: io::Writer, n: uint, size: uint) { fn write_sized_vuint(w: io::Writer, n: uint, size: uint) {
@ -173,13 +186,13 @@ fn write_vuint(w: io::Writer, n: uint) {
fail fmt!("vint to write too big: %?", n); fail fmt!("vint to write too big: %?", n);
} }
pub fn Writer(w: io::Writer) -> Writer { pub fn Serializer(w: io::Writer) -> Serializer {
let size_positions: ~[uint] = ~[]; let size_positions: ~[uint] = ~[];
return Writer_({writer: w, mut size_positions: size_positions}); Serializer { writer: w, mut size_positions: size_positions }
} }
// FIXME (#2741): Provide a function to write the standard ebml header. // FIXME (#2741): Provide a function to write the standard ebml header.
impl Writer { impl Serializer {
fn start_tag(tag_id: uint) { fn start_tag(tag_id: uint) {
debug!("Start tag %u", tag_id); debug!("Start tag %u", tag_id);
@ -295,12 +308,7 @@ enum EbmlSerializerTag {
EsLabel // Used only when debugging EsLabel // Used only when debugging
} }
trait SerializerPriv { priv impl Serializer {
fn _emit_tagged_uint(t: EbmlSerializerTag, v: uint);
fn _emit_label(label: &str);
}
impl ebml::Writer: SerializerPriv {
// used internally to emit things like the vector length and so on // used internally to emit things like the vector length and so on
fn _emit_tagged_uint(t: EbmlSerializerTag, v: uint) { fn _emit_tagged_uint(t: EbmlSerializerTag, v: uint) {
assert v <= 0xFFFF_FFFF_u; assert v <= 0xFFFF_FFFF_u;
@ -318,89 +326,123 @@ impl ebml::Writer: SerializerPriv {
} }
} }
impl ebml::Writer { impl Serializer {
fn emit_opaque(f: fn()) { fn emit_opaque(&self, f: fn()) {
do self.wr_tag(EsOpaque as uint) { do self.wr_tag(EsOpaque as uint) {
f() f()
} }
} }
} }
impl ebml::Writer: serialization::Serializer { impl Serializer: serialization::Serializer {
fn emit_nil() {} fn emit_nil(&self) {}
fn emit_uint(v: uint) { self.wr_tagged_u64(EsUint as uint, v as u64); } fn emit_uint(&self, v: uint) {
fn emit_u64(v: u64) { self.wr_tagged_u64(EsU64 as uint, v); } self.wr_tagged_u64(EsUint as uint, v as u64);
fn emit_u32(v: u32) { self.wr_tagged_u32(EsU32 as uint, v); } }
fn emit_u16(v: u16) { self.wr_tagged_u16(EsU16 as uint, v); } fn emit_u64(&self, v: u64) { self.wr_tagged_u64(EsU64 as uint, v); }
fn emit_u8(v: u8) { self.wr_tagged_u8 (EsU8 as uint, v); } fn emit_u32(&self, v: u32) { self.wr_tagged_u32(EsU32 as uint, v); }
fn emit_u16(&self, v: u16) { self.wr_tagged_u16(EsU16 as uint, v); }
fn emit_u8(&self, v: u8) { self.wr_tagged_u8 (EsU8 as uint, v); }
fn emit_int(v: int) { self.wr_tagged_i64(EsInt as uint, v as i64); } fn emit_int(&self, v: int) {
fn emit_i64(v: i64) { self.wr_tagged_i64(EsI64 as uint, v); } self.wr_tagged_i64(EsInt as uint, v as i64);
fn emit_i32(v: i32) { self.wr_tagged_i32(EsI32 as uint, v); } }
fn emit_i16(v: i16) { self.wr_tagged_i16(EsI16 as uint, v); } fn emit_i64(&self, v: i64) { self.wr_tagged_i64(EsI64 as uint, v); }
fn emit_i8(v: i8) { self.wr_tagged_i8 (EsI8 as uint, v); } fn emit_i32(&self, v: i32) { self.wr_tagged_i32(EsI32 as uint, v); }
fn emit_i16(&self, v: i16) { self.wr_tagged_i16(EsI16 as uint, v); }
fn emit_i8(&self, v: i8) { self.wr_tagged_i8 (EsI8 as uint, v); }
fn emit_bool(v: bool) { self.wr_tagged_u8(EsBool as uint, v as u8) } fn emit_bool(&self, v: bool) {
self.wr_tagged_u8(EsBool as uint, v as u8)
}
// FIXME (#2742): implement these // FIXME (#2742): implement these
fn emit_f64(_v: f64) { fail ~"Unimplemented: serializing an f64"; } fn emit_f64(&self, _v: f64) { fail ~"Unimplemented: serializing an f64"; }
fn emit_f32(_v: f32) { fail ~"Unimplemented: serializing an f32"; } fn emit_f32(&self, _v: f32) { fail ~"Unimplemented: serializing an f32"; }
fn emit_float(_v: float) { fail ~"Unimplemented: serializing a float"; } fn emit_float(&self, _v: float) {
fail ~"Unimplemented: serializing a float";
}
fn emit_str(v: &str) { self.wr_tagged_str(EsStr as uint, v) } fn emit_char(&self, _v: char) {
fail ~"Unimplemented: serializing a char";
}
fn emit_enum(name: &str, f: fn()) { 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); self._emit_label(name);
self.wr_tag(EsEnum as uint, f) self.wr_tag(EsEnum as uint, f)
} }
fn emit_enum_variant(_v_name: &str, v_id: uint, _cnt: uint, f: fn()) { fn emit_enum_variant(&self, _v_name: &str, v_id: uint, _cnt: uint,
f: fn()) {
self._emit_tagged_uint(EsEnumVid, v_id); self._emit_tagged_uint(EsEnumVid, v_id);
self.wr_tag(EsEnumBody as uint, f) self.wr_tag(EsEnumBody as uint, f)
} }
fn emit_enum_variant_arg(_idx: uint, f: fn()) { f() } fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) { f() }
fn emit_vec(len: uint, f: fn()) { fn emit_borrowed_vec(&self, len: uint, f: fn()) {
do self.wr_tag(EsVec as uint) { do self.wr_tag(EsVec as uint) {
self._emit_tagged_uint(EsVecLen, len); self._emit_tagged_uint(EsVecLen, len);
f() f()
} }
} }
fn emit_vec_elt(_idx: uint, f: fn()) { 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) self.wr_tag(EsVecElt as uint, f)
} }
fn emit_box(f: fn()) { f() } fn emit_rec(&self, f: fn()) { f() }
fn emit_uniq(f: fn()) { f() } fn emit_struct(&self, _name: &str, f: fn()) { f() }
fn emit_rec(f: fn()) { f() } fn emit_field(&self, name: &str, _idx: uint, f: fn()) {
fn emit_rec_field(f_name: &str, _f_idx: uint, f: fn()) { self._emit_label(name);
self._emit_label(f_name);
f() f()
} }
fn emit_tup(_sz: uint, f: fn()) { f() }
fn emit_tup_elt(_idx: uint, f: fn()) { f() } fn emit_tup(&self, _len: uint, f: fn()) { f() }
fn emit_tup_elt(&self, _idx: uint, f: fn()) { f() }
} }
type EbmlDeserializer_ = {mut parent: ebml::Doc, struct Deserializer {
mut pos: uint}; priv mut parent: Doc,
priv mut pos: uint,
pub enum EbmlDeserializer {
EbmlDeserializer_(EbmlDeserializer_)
} }
pub fn ebml_deserializer(d: ebml::Doc) -> EbmlDeserializer { pub fn Deserializer(d: Doc) -> Deserializer {
EbmlDeserializer_({mut parent: d, mut pos: d.start}) Deserializer { mut parent: d, mut pos: d.start }
} }
priv impl EbmlDeserializer { priv impl Deserializer {
fn _check_label(lbl: &str) { fn _check_label(lbl: &str) {
if self.pos < self.parent.end { if self.pos < self.parent.end {
let {tag: r_tag, doc: r_doc} = let TaggedDoc { tag: r_tag, doc: r_doc } =
ebml::doc_at(self.parent.data, self.pos); doc_at(self.parent.data, self.pos);
if r_tag == (EsLabel as uint) { if r_tag == (EsLabel as uint) {
self.pos = r_doc.end; self.pos = r_doc.end;
let str = ebml::doc_as_str(r_doc); let str = doc_as_str(r_doc);
if lbl != str { if lbl != str {
fail fmt!("Expected label %s but found %s", lbl, str); fail fmt!("Expected label %s but found %s", lbl, str);
} }
@ -408,13 +450,13 @@ priv impl EbmlDeserializer {
} }
} }
fn next_doc(exp_tag: EbmlSerializerTag) -> ebml::Doc { fn next_doc(exp_tag: EbmlSerializerTag) -> Doc {
debug!(". next_doc(exp_tag=%?)", exp_tag); debug!(". next_doc(exp_tag=%?)", exp_tag);
if self.pos >= self.parent.end { if self.pos >= self.parent.end {
fail ~"no more documents in current node!"; fail ~"no more documents in current node!";
} }
let {tag: r_tag, doc: r_doc} = let TaggedDoc { tag: r_tag, doc: r_doc } =
ebml::doc_at(self.parent.data, self.pos); doc_at(self.parent.data, self.pos);
debug!("self.parent=%?-%? self.pos=%? r_tag=%? r_doc=%?-%?", debug!("self.parent=%?-%? self.pos=%? r_tag=%? r_doc=%?-%?",
copy self.parent.start, copy self.parent.end, copy self.parent.start, copy self.parent.end,
copy self.pos, r_tag, r_doc.start, r_doc.end); copy self.pos, r_tag, r_doc.start, r_doc.end);
@ -427,10 +469,10 @@ priv impl EbmlDeserializer {
r_doc.end, self.parent.end); r_doc.end, self.parent.end);
} }
self.pos = r_doc.end; self.pos = r_doc.end;
return r_doc; r_doc
} }
fn push_doc<T>(d: ebml::Doc, f: fn() -> T) -> T{ fn push_doc<T>(d: Doc, f: fn() -> T) -> T{
let old_parent = self.parent; let old_parent = self.parent;
let old_pos = self.pos; let old_pos = self.pos;
self.parent = d; self.parent = d;
@ -442,63 +484,76 @@ priv impl EbmlDeserializer {
} }
fn _next_uint(exp_tag: EbmlSerializerTag) -> uint { fn _next_uint(exp_tag: EbmlSerializerTag) -> uint {
let r = ebml::doc_as_u32(self.next_doc(exp_tag)); let r = doc_as_u32(self.next_doc(exp_tag));
debug!("_next_uint exp_tag=%? result=%?", exp_tag, r); debug!("_next_uint exp_tag=%? result=%?", exp_tag, r);
return r as uint; r as uint
} }
} }
impl EbmlDeserializer { impl Deserializer {
fn read_opaque<R>(op: fn(ebml::Doc) -> R) -> R { fn read_opaque<R>(&self, op: fn(Doc) -> R) -> R {
do self.push_doc(self.next_doc(EsOpaque)) { do self.push_doc(self.next_doc(EsOpaque)) {
op(copy self.parent) op(copy self.parent)
} }
} }
} }
impl EbmlDeserializer: serialization::Deserializer { impl Deserializer: serialization::Deserializer {
fn read_nil() -> () { () } fn read_nil(&self) -> () { () }
fn read_u64() -> u64 { ebml::doc_as_u64(self.next_doc(EsU64)) } fn read_u64(&self) -> u64 { doc_as_u64(self.next_doc(EsU64)) }
fn read_u32() -> u32 { ebml::doc_as_u32(self.next_doc(EsU32)) } fn read_u32(&self) -> u32 { doc_as_u32(self.next_doc(EsU32)) }
fn read_u16() -> u16 { ebml::doc_as_u16(self.next_doc(EsU16)) } fn read_u16(&self) -> u16 { doc_as_u16(self.next_doc(EsU16)) }
fn read_u8 () -> u8 { ebml::doc_as_u8 (self.next_doc(EsU8 )) } fn read_u8 (&self) -> u8 { doc_as_u8 (self.next_doc(EsU8 )) }
fn read_uint() -> uint { fn read_uint(&self) -> uint {
let v = ebml::doc_as_u64(self.next_doc(EsUint)); let v = doc_as_u64(self.next_doc(EsUint));
if v > (core::uint::max_value as u64) { if v > (core::uint::max_value as u64) {
fail fmt!("uint %? too large for this architecture", v); fail fmt!("uint %? too large for this architecture", v);
} }
return v as uint; v as uint
} }
fn read_i64() -> i64 { ebml::doc_as_u64(self.next_doc(EsI64)) as i64 } fn read_i64(&self) -> i64 { doc_as_u64(self.next_doc(EsI64)) as i64 }
fn read_i32() -> i32 { ebml::doc_as_u32(self.next_doc(EsI32)) as i32 } fn read_i32(&self) -> i32 { doc_as_u32(self.next_doc(EsI32)) as i32 }
fn read_i16() -> i16 { ebml::doc_as_u16(self.next_doc(EsI16)) as i16 } fn read_i16(&self) -> i16 { doc_as_u16(self.next_doc(EsI16)) as i16 }
fn read_i8 () -> i8 { ebml::doc_as_u8 (self.next_doc(EsI8 )) as i8 } fn read_i8 (&self) -> i8 { doc_as_u8 (self.next_doc(EsI8 )) as i8 }
fn read_int() -> int { fn read_int(&self) -> int {
let v = ebml::doc_as_u64(self.next_doc(EsInt)) as i64; let v = doc_as_u64(self.next_doc(EsInt)) as i64;
if v > (int::max_value as i64) || v < (int::min_value as i64) { if v > (int::max_value as i64) || v < (int::min_value as i64) {
fail fmt!("int %? out of range for this architecture", v); fail fmt!("int %? out of range for this architecture", v);
} }
return v as int; v as int
} }
fn read_bool() -> bool { ebml::doc_as_u8(self.next_doc(EsBool)) as bool } fn read_bool(&self) -> bool { doc_as_u8(self.next_doc(EsBool)) as bool }
fn read_f64() -> f64 { fail ~"read_f64()"; } fn read_f64(&self) -> f64 { fail ~"read_f64()"; }
fn read_f32() -> f32 { fail ~"read_f32()"; } fn read_f32(&self) -> f32 { fail ~"read_f32()"; }
fn read_float() -> float { fail ~"read_float()"; } fn read_float(&self) -> float { fail ~"read_float()"; }
fn read_str() -> ~str { ebml::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: // Compound types:
fn read_enum<T>(name: &str, f: fn() -> T) -> T { fn read_owned<T>(&self, f: fn() -> T) -> T {
debug!("read_owned()");
f()
}
fn read_managed<T>(&self, f: fn() -> T) -> T {
debug!("read_managed()");
f()
}
fn read_enum<T>(&self, name: &str, f: fn() -> T) -> T {
debug!("read_enum(%s)", name); debug!("read_enum(%s)", name);
self._check_label(name); self._check_label(name);
self.push_doc(self.next_doc(EsEnum), f) self.push_doc(self.next_doc(EsEnum), f)
} }
fn read_enum_variant<T>(f: fn(uint) -> T) -> T { fn read_enum_variant<T>(&self, f: fn(uint) -> T) -> T {
debug!("read_enum_variant()"); debug!("read_enum_variant()");
let idx = self._next_uint(EsEnumVid); let idx = self._next_uint(EsEnumVid);
debug!(" idx=%u", idx); debug!(" idx=%u", idx);
@ -507,13 +562,13 @@ impl EbmlDeserializer: serialization::Deserializer {
} }
} }
fn read_enum_variant_arg<T>(idx: uint, f: fn() -> T) -> T { fn read_enum_variant_arg<T>(&self, idx: uint, f: fn() -> T) -> T {
debug!("read_enum_variant_arg(idx=%u)", idx); debug!("read_enum_variant_arg(idx=%u)", idx);
f() f()
} }
fn read_vec<T>(f: fn(uint) -> T) -> T { fn read_owned_vec<T>(&self, f: fn(uint) -> T) -> T {
debug!("read_vec()"); debug!("read_owned_vec()");
do self.push_doc(self.next_doc(EsVec)) { do self.push_doc(self.next_doc(EsVec)) {
let len = self._next_uint(EsVecLen); let len = self._next_uint(EsVecLen);
debug!(" len=%u", len); debug!(" len=%u", len);
@ -521,99 +576,63 @@ impl EbmlDeserializer: serialization::Deserializer {
} }
} }
fn read_vec_elt<T>(idx: uint, f: fn() -> T) -> T { fn read_managed_vec<T>(&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<T>(&self, idx: uint, f: fn() -> T) -> T {
debug!("read_vec_elt(idx=%u)", idx); debug!("read_vec_elt(idx=%u)", idx);
self.push_doc(self.next_doc(EsVecElt), f) self.push_doc(self.next_doc(EsVecElt), f)
} }
fn read_box<T>(f: fn() -> T) -> T { fn read_rec<T>(&self, f: fn() -> T) -> T {
debug!("read_box()");
f()
}
fn read_uniq<T>(f: fn() -> T) -> T {
debug!("read_uniq()");
f()
}
fn read_rec<T>(f: fn() -> T) -> T {
debug!("read_rec()"); debug!("read_rec()");
f() f()
} }
fn read_rec_field<T>(f_name: &str, f_idx: uint, f: fn() -> T) -> T { fn read_struct<T>(&self, name: &str, f: fn() -> T) -> T {
debug!("read_rec_field(%s, idx=%u)", f_name, f_idx); debug!("read_struct(name=%s)", name);
self._check_label(f_name);
f() f()
} }
fn read_tup<T>(sz: uint, f: fn() -> T) -> T { fn read_field<T>(&self, name: &str, idx: uint, f: fn() -> T) -> T {
debug!("read_tup(sz=%u)", sz); debug!("read_field(name=%s, idx=%u)", name, idx);
self._check_label(name);
f() f()
} }
fn read_tup_elt<T>(idx: uint, f: fn() -> T) -> T { fn read_tup<T>(&self, len: uint, f: fn() -> T) -> T {
debug!("read_tup(len=%u)", len);
f()
}
fn read_tup_elt<T>(&self, idx: uint, f: fn() -> T) -> T {
debug!("read_tup_elt(idx=%u)", idx); debug!("read_tup_elt(idx=%u)", idx);
f() f()
} }
} }
// ___________________________________________________________________________ // ___________________________________________________________________________
// Testing // Testing
#[cfg(test)]
mod tests {
#[test] #[test]
fn test_option_int() { fn test_option_int() {
fn serialize_1<S: serialization::Serializer>(s: &S, v: int) {
s.emit_i64(v as i64);
}
fn serialize_0<S: serialization::Serializer>(s: &S, v: Option<int>) {
do s.emit_enum(~"core::option::t") {
match v {
None => s.emit_enum_variant(
~"core::option::None", 0u, 0u, || { } ),
Some(v0) => {
do s.emit_enum_variant(~"core::option::some", 1u, 1u) {
s.emit_enum_variant_arg(0u, || serialize_1(s, v0));
}
}
}
}
}
fn deserialize_1<S: serialization::Deserializer>(s: &S) -> int {
s.read_i64() as int
}
fn deserialize_0<S: serialization::Deserializer>(s: &S) -> Option<int> {
do s.read_enum(~"core::option::t") {
do s.read_enum_variant |i| {
match i {
0 => None,
1 => {
let v0 = do s.read_enum_variant_arg(0u) {
deserialize_1(s)
};
Some(v0)
}
_ => {
fail #fmt("deserialize_0: unexpected variant %u", i);
}
}
}
}
}
fn test_v(v: Option<int>) { fn test_v(v: Option<int>) {
debug!("v == %?", v); debug!("v == %?", v);
let bytes = do io::with_bytes_writer |wr| { let bytes = do io::with_bytes_writer |wr| {
let ebml_w = ebml::Writer(wr); let ebml_w = Serializer(wr);
serialize_0(&ebml_w, v); v.serialize(&ebml_w)
}; };
let ebml_doc = ebml::Doc(@bytes); let ebml_doc = Doc(@bytes);
let deser = ebml_deserializer(ebml_doc); let deser = Deserializer(ebml_doc);
let v1 = deserialize_0(&deser); let v1 = serialization::deserialize(&deser);
debug!("v1 == %?", v1); debug!("v1 == %?", v1);
assert v == v1; assert v == v1;
} }
@ -622,3 +641,4 @@ fn test_option_int() {
test_v(None); test_v(None);
test_v(Some(3)); test_v(Some(3));
} }
}

View file

@ -1,644 +1 @@
#[forbid(deprecated_mode)]; pub use ebml::*;
use serialization2;
// Simple Extensible Binary Markup Language (ebml) reader and writer on a
// cursor model. See the specification here:
// http://www.matroska.org/technical/specs/rfc/index.html
struct EbmlTag {
id: uint,
size: uint,
}
struct EbmlState {
ebml_tag: EbmlTag,
tag_pos: uint,
data_pos: uint,
}
// FIXME (#2739): When we have module renaming, make "reader" and "writer"
// separate modules within this file.
// ebml reading
struct Doc {
data: @~[u8],
start: uint,
end: uint,
}
struct TaggedDoc {
tag: uint,
doc: Doc,
}
impl Doc: ops::Index<uint,Doc> {
pure fn index(tag: uint) -> Doc {
unsafe {
get_doc(self, tag)
}
}
}
fn vuint_at(data: &[u8], start: uint) -> {val: uint, next: uint} {
let a = data[start];
if a & 0x80u8 != 0u8 {
return {val: (a & 0x7fu8) as uint, next: start + 1u};
}
if a & 0x40u8 != 0u8 {
return {val: ((a & 0x3fu8) as uint) << 8u |
(data[start + 1u] as uint),
next: start + 2u};
} else if a & 0x20u8 != 0u8 {
return {val: ((a & 0x1fu8) as uint) << 16u |
(data[start + 1u] as uint) << 8u |
(data[start + 2u] as uint),
next: start + 3u};
} else if a & 0x10u8 != 0u8 {
return {val: ((a & 0x0fu8) as uint) << 24u |
(data[start + 1u] as uint) << 16u |
(data[start + 2u] as uint) << 8u |
(data[start + 3u] as uint),
next: start + 4u};
} else { error!("vint too big"); fail; }
}
pub fn Doc(data: @~[u8]) -> Doc {
Doc { data: data, start: 0u, end: vec::len::<u8>(*data) }
}
pub fn doc_at(data: @~[u8], start: uint) -> TaggedDoc {
let elt_tag = vuint_at(*data, start);
let elt_size = vuint_at(*data, elt_tag.next);
let end = elt_size.next + elt_size.val;
TaggedDoc {
tag: elt_tag.val,
doc: Doc { data: data, start: elt_size.next, end: end }
}
}
pub fn maybe_get_doc(d: Doc, tg: uint) -> Option<Doc> {
let mut pos = d.start;
while pos < d.end {
let elt_tag = vuint_at(*d.data, pos);
let elt_size = vuint_at(*d.data, elt_tag.next);
pos = elt_size.next + elt_size.val;
if elt_tag.val == tg {
return Some(Doc { data: d.data, start: elt_size.next, end: pos });
}
}
None
}
pub fn get_doc(d: Doc, tg: uint) -> Doc {
match maybe_get_doc(d, tg) {
Some(d) => d,
None => {
error!("failed to find block with tag %u", tg);
fail;
}
}
}
pub fn docs(d: Doc, it: fn(uint, Doc) -> bool) {
let mut pos = d.start;
while pos < d.end {
let elt_tag = vuint_at(*d.data, pos);
let elt_size = vuint_at(*d.data, elt_tag.next);
pos = elt_size.next + elt_size.val;
let doc = Doc { data: d.data, start: elt_size.next, end: pos };
if !it(elt_tag.val, doc) {
break;
}
}
}
pub fn tagged_docs(d: Doc, tg: uint, it: fn(Doc) -> bool) {
let mut pos = d.start;
while pos < d.end {
let elt_tag = vuint_at(*d.data, pos);
let elt_size = vuint_at(*d.data, elt_tag.next);
pos = elt_size.next + elt_size.val;
if elt_tag.val == tg {
let doc = Doc { data: d.data, start: elt_size.next, end: pos };
if !it(doc) {
break;
}
}
}
}
pub fn doc_data(d: Doc) -> ~[u8] { vec::slice::<u8>(*d.data, d.start, d.end) }
pub fn with_doc_data<T>(d: Doc, f: fn(x: &[u8]) -> T) -> T {
f(vec::view(*d.data, d.start, d.end))
}
pub fn doc_as_str(d: Doc) -> ~str { str::from_bytes(doc_data(d)) }
pub fn doc_as_u8(d: Doc) -> u8 {
assert d.end == d.start + 1u;
(*d.data)[d.start]
}
pub fn doc_as_u16(d: Doc) -> u16 {
assert d.end == d.start + 2u;
io::u64_from_be_bytes(*d.data, d.start, 2u) as u16
}
pub fn doc_as_u32(d: Doc) -> u32 {
assert d.end == d.start + 4u;
io::u64_from_be_bytes(*d.data, d.start, 4u) as u32
}
pub fn doc_as_u64(d: Doc) -> u64 {
assert d.end == d.start + 8u;
io::u64_from_be_bytes(*d.data, d.start, 8u)
}
pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
pub fn doc_as_i16(d: Doc) -> i16 { doc_as_u16(d) as i16 }
pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 }
pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 }
// ebml writing
struct Serializer {
writer: io::Writer,
priv mut size_positions: ~[uint],
}
fn write_sized_vuint(w: io::Writer, n: uint, size: uint) {
match size {
1u => w.write(&[0x80u8 | (n as u8)]),
2u => w.write(&[0x40u8 | ((n >> 8_u) as u8), n as u8]),
3u => w.write(&[0x20u8 | ((n >> 16_u) as u8), (n >> 8_u) as u8,
n as u8]),
4u => w.write(&[0x10u8 | ((n >> 24_u) as u8), (n >> 16_u) as u8,
(n >> 8_u) as u8, n as u8]),
_ => fail fmt!("vint to write too big: %?", n)
};
}
fn write_vuint(w: io::Writer, n: uint) {
if n < 0x7f_u { write_sized_vuint(w, n, 1u); return; }
if n < 0x4000_u { write_sized_vuint(w, n, 2u); return; }
if n < 0x200000_u { write_sized_vuint(w, n, 3u); return; }
if n < 0x10000000_u { write_sized_vuint(w, n, 4u); return; }
fail fmt!("vint to write too big: %?", n);
}
pub fn Serializer(w: io::Writer) -> Serializer {
let size_positions: ~[uint] = ~[];
Serializer { writer: w, mut size_positions: size_positions }
}
// FIXME (#2741): Provide a function to write the standard ebml header.
impl Serializer {
fn start_tag(tag_id: uint) {
debug!("Start tag %u", tag_id);
// Write the enum ID:
write_vuint(self.writer, tag_id);
// Write a placeholder four-byte size.
self.size_positions.push(self.writer.tell());
let zeroes: &[u8] = &[0u8, 0u8, 0u8, 0u8];
self.writer.write(zeroes);
}
fn end_tag() {
let last_size_pos = self.size_positions.pop();
let cur_pos = self.writer.tell();
self.writer.seek(last_size_pos as int, io::SeekSet);
let size = (cur_pos - last_size_pos - 4u);
write_sized_vuint(self.writer, size, 4u);
self.writer.seek(cur_pos as int, io::SeekSet);
debug!("End tag (size = %u)", size);
}
fn wr_tag(tag_id: uint, blk: fn()) {
self.start_tag(tag_id);
blk();
self.end_tag();
}
fn wr_tagged_bytes(tag_id: uint, b: &[u8]) {
write_vuint(self.writer, tag_id);
write_vuint(self.writer, vec::len(b));
self.writer.write(b);
}
fn wr_tagged_u64(tag_id: uint, v: u64) {
do io::u64_to_be_bytes(v, 8u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
fn wr_tagged_u32(tag_id: uint, v: u32) {
do io::u64_to_be_bytes(v as u64, 4u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
fn wr_tagged_u16(tag_id: uint, v: u16) {
do io::u64_to_be_bytes(v as u64, 2u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
fn wr_tagged_u8(tag_id: uint, v: u8) {
self.wr_tagged_bytes(tag_id, &[v]);
}
fn wr_tagged_i64(tag_id: uint, v: i64) {
do io::u64_to_be_bytes(v as u64, 8u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
fn wr_tagged_i32(tag_id: uint, v: i32) {
do io::u64_to_be_bytes(v as u64, 4u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
fn wr_tagged_i16(tag_id: uint, v: i16) {
do io::u64_to_be_bytes(v as u64, 2u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
fn wr_tagged_i8(tag_id: uint, v: i8) {
self.wr_tagged_bytes(tag_id, &[v as u8]);
}
fn wr_tagged_str(tag_id: uint, v: &str) {
str::byte_slice(v, |b| self.wr_tagged_bytes(tag_id, b));
}
fn wr_bytes(b: &[u8]) {
debug!("Write %u bytes", vec::len(b));
self.writer.write(b);
}
fn wr_str(s: &str) {
debug!("Write str: %?", s);
self.writer.write(str::to_bytes(s));
}
}
// FIXME (#2743): optionally perform "relaxations" on end_tag to more
// efficiently encode sizes; this is a fixed point iteration
// Set to true to generate more debugging in EBML serialization.
// Totally lame approach.
const debug: bool = false;
enum EbmlSerializerTag {
EsUint, EsU64, EsU32, EsU16, EsU8,
EsInt, EsI64, EsI32, EsI16, EsI8,
EsBool,
EsStr,
EsF64, EsF32, EsFloat,
EsEnum, EsEnumVid, EsEnumBody,
EsVec, EsVecLen, EsVecElt,
EsOpaque,
EsLabel // Used only when debugging
}
priv impl Serializer {
// used internally to emit things like the vector length and so on
fn _emit_tagged_uint(t: EbmlSerializerTag, v: uint) {
assert v <= 0xFFFF_FFFF_u;
self.wr_tagged_u32(t as uint, v as u32);
}
fn _emit_label(label: &str) {
// There are various strings that we have access to, such as
// the name of a record field, which do not actually appear in
// the serialized EBML (normally). This is just for
// efficiency. When debugging, though, we can emit such
// labels and then they will be checked by deserializer to
// try and check failures more quickly.
if debug { self.wr_tagged_str(EsLabel as uint, label) }
}
}
impl Serializer {
fn emit_opaque(&self, f: fn()) {
do self.wr_tag(EsOpaque as uint) {
f()
}
}
}
impl Serializer: serialization2::Serializer {
fn emit_nil(&self) {}
fn emit_uint(&self, v: uint) {
self.wr_tagged_u64(EsUint as uint, v as u64);
}
fn emit_u64(&self, v: u64) { self.wr_tagged_u64(EsU64 as uint, v); }
fn emit_u32(&self, v: u32) { self.wr_tagged_u32(EsU32 as uint, v); }
fn emit_u16(&self, v: u16) { self.wr_tagged_u16(EsU16 as uint, v); }
fn emit_u8(&self, v: u8) { self.wr_tagged_u8 (EsU8 as uint, v); }
fn emit_int(&self, v: int) {
self.wr_tagged_i64(EsInt as uint, v as i64);
}
fn emit_i64(&self, v: i64) { self.wr_tagged_i64(EsI64 as uint, v); }
fn emit_i32(&self, v: i32) { self.wr_tagged_i32(EsI32 as uint, v); }
fn emit_i16(&self, v: i16) { self.wr_tagged_i16(EsI16 as uint, v); }
fn emit_i8(&self, v: i8) { self.wr_tagged_i8 (EsI8 as uint, v); }
fn emit_bool(&self, v: bool) {
self.wr_tagged_u8(EsBool as uint, v as u8)
}
// FIXME (#2742): implement these
fn emit_f64(&self, _v: f64) { fail ~"Unimplemented: serializing an f64"; }
fn emit_f32(&self, _v: f32) { fail ~"Unimplemented: serializing an f32"; }
fn emit_float(&self, _v: float) {
fail ~"Unimplemented: serializing a float";
}
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);
self.wr_tag(EsEnum as uint, f)
}
fn emit_enum_variant(&self, _v_name: &str, v_id: uint, _cnt: uint,
f: fn()) {
self._emit_tagged_uint(EsEnumVid, v_id);
self.wr_tag(EsEnumBody as uint, f)
}
fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) { f() }
fn emit_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_rec(&self, f: fn()) { f() }
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, _len: uint, f: fn()) { f() }
fn emit_tup_elt(&self, _idx: uint, f: fn()) { f() }
}
struct Deserializer {
priv mut parent: Doc,
priv mut pos: uint,
}
pub fn Deserializer(d: Doc) -> Deserializer {
Deserializer { mut parent: d, mut pos: d.start }
}
priv impl Deserializer {
fn _check_label(lbl: &str) {
if self.pos < self.parent.end {
let TaggedDoc { tag: r_tag, doc: r_doc } =
doc_at(self.parent.data, self.pos);
if r_tag == (EsLabel as uint) {
self.pos = r_doc.end;
let str = doc_as_str(r_doc);
if lbl != str {
fail fmt!("Expected label %s but found %s", lbl, str);
}
}
}
}
fn next_doc(exp_tag: EbmlSerializerTag) -> Doc {
debug!(". next_doc(exp_tag=%?)", exp_tag);
if self.pos >= self.parent.end {
fail ~"no more documents in current node!";
}
let TaggedDoc { tag: r_tag, doc: r_doc } =
doc_at(self.parent.data, self.pos);
debug!("self.parent=%?-%? self.pos=%? r_tag=%? r_doc=%?-%?",
copy self.parent.start, copy self.parent.end,
copy self.pos, r_tag, r_doc.start, r_doc.end);
if r_tag != (exp_tag as uint) {
fail fmt!("expected EMBL doc with tag %? but found tag %?",
exp_tag, r_tag);
}
if r_doc.end > self.parent.end {
fail fmt!("invalid EBML, child extends to 0x%x, parent to 0x%x",
r_doc.end, self.parent.end);
}
self.pos = r_doc.end;
r_doc
}
fn push_doc<T>(d: Doc, f: fn() -> T) -> T{
let old_parent = self.parent;
let old_pos = self.pos;
self.parent = d;
self.pos = d.start;
let r = f();
self.parent = old_parent;
self.pos = old_pos;
move r
}
fn _next_uint(exp_tag: EbmlSerializerTag) -> uint {
let r = doc_as_u32(self.next_doc(exp_tag));
debug!("_next_uint exp_tag=%? result=%?", exp_tag, r);
r as uint
}
}
impl Deserializer {
fn read_opaque<R>(&self, op: fn(Doc) -> R) -> R {
do self.push_doc(self.next_doc(EsOpaque)) {
op(copy self.parent)
}
}
}
impl Deserializer: serialization2::Deserializer {
fn read_nil(&self) -> () { () }
fn read_u64(&self) -> u64 { doc_as_u64(self.next_doc(EsU64)) }
fn read_u32(&self) -> u32 { doc_as_u32(self.next_doc(EsU32)) }
fn read_u16(&self) -> u16 { doc_as_u16(self.next_doc(EsU16)) }
fn read_u8 (&self) -> u8 { doc_as_u8 (self.next_doc(EsU8 )) }
fn read_uint(&self) -> uint {
let v = doc_as_u64(self.next_doc(EsUint));
if v > (core::uint::max_value as u64) {
fail fmt!("uint %? too large for this architecture", v);
}
v as uint
}
fn read_i64(&self) -> i64 { doc_as_u64(self.next_doc(EsI64)) as i64 }
fn read_i32(&self) -> i32 { doc_as_u32(self.next_doc(EsI32)) as i32 }
fn read_i16(&self) -> i16 { doc_as_u16(self.next_doc(EsI16)) as i16 }
fn read_i8 (&self) -> i8 { doc_as_u8 (self.next_doc(EsI8 )) as i8 }
fn read_int(&self) -> int {
let v = doc_as_u64(self.next_doc(EsInt)) as i64;
if v > (int::max_value as i64) || v < (int::min_value as i64) {
fail fmt!("int %? out of range for this architecture", v);
}
v as int
}
fn read_bool(&self) -> bool { doc_as_u8(self.next_doc(EsBool)) as bool }
fn read_f64(&self) -> f64 { fail ~"read_f64()"; }
fn read_f32(&self) -> f32 { fail ~"read_f32()"; }
fn read_float(&self) -> float { fail ~"read_float()"; }
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<T>(&self, f: fn() -> T) -> T {
debug!("read_owned()");
f()
}
fn read_managed<T>(&self, f: fn() -> T) -> T {
debug!("read_managed()");
f()
}
fn read_enum<T>(&self, name: &str, f: fn() -> T) -> T {
debug!("read_enum(%s)", name);
self._check_label(name);
self.push_doc(self.next_doc(EsEnum), f)
}
fn read_enum_variant<T>(&self, f: fn(uint) -> T) -> T {
debug!("read_enum_variant()");
let idx = self._next_uint(EsEnumVid);
debug!(" idx=%u", idx);
do self.push_doc(self.next_doc(EsEnumBody)) {
f(idx)
}
}
fn read_enum_variant_arg<T>(&self, idx: uint, f: fn() -> T) -> T {
debug!("read_enum_variant_arg(idx=%u)", idx);
f()
}
fn read_owned_vec<T>(&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<T>(&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<T>(&self, idx: uint, f: fn() -> T) -> T {
debug!("read_vec_elt(idx=%u)", idx);
self.push_doc(self.next_doc(EsVecElt), f)
}
fn read_rec<T>(&self, f: fn() -> T) -> T {
debug!("read_rec()");
f()
}
fn read_struct<T>(&self, name: &str, f: fn() -> T) -> T {
debug!("read_struct(name=%s)", name);
f()
}
fn read_field<T>(&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<T>(&self, len: uint, f: fn() -> T) -> T {
debug!("read_tup(len=%u)", len);
f()
}
fn read_tup_elt<T>(&self, idx: uint, f: fn() -> T) -> T {
debug!("read_tup_elt(idx=%u)", idx);
f()
}
}
// ___________________________________________________________________________
// Testing
#[cfg(test)]
mod tests {
#[test]
fn test_option_int() {
fn test_v(v: Option<int>) {
debug!("v == %?", v);
let bytes = do io::with_bytes_writer |wr| {
let ebml_w = Serializer(wr);
v.serialize(&ebml_w)
};
let ebml_doc = Doc(@bytes);
let deser = Deserializer(ebml_doc);
let v1 = serialization2::deserialize(&deser);
debug!("v1 == %?", v1);
assert v == v1;
}
test_v(Some(22));
test_v(None);
test_v(Some(3));
}
}

View file

@ -63,7 +63,7 @@ pub fn Serializer(wr: io::Writer) -> Serializer {
Serializer { wr: wr } Serializer { wr: wr }
} }
pub impl Serializer: serialization2::Serializer { pub impl Serializer: serialization::Serializer {
fn emit_nil(&self) { self.wr.write_str("null") } fn emit_nil(&self) { self.wr.write_str("null") }
fn emit_uint(&self, v: uint) { self.emit_float(v as float); } fn emit_uint(&self, v: uint) { self.emit_float(v as float); }
@ -167,7 +167,7 @@ pub fn PrettySerializer(wr: io::Writer) -> PrettySerializer {
PrettySerializer { wr: wr, indent: 0 } PrettySerializer { wr: wr, indent: 0 }
} }
pub impl PrettySerializer: serialization2::Serializer { pub impl PrettySerializer: serialization::Serializer {
fn emit_nil(&self) { self.wr.write_str("null") } fn emit_nil(&self) { self.wr.write_str("null") }
fn emit_uint(&self, v: uint) { self.emit_float(v as float); } fn emit_uint(&self, v: uint) { self.emit_float(v as float); }
@ -273,8 +273,8 @@ pub impl PrettySerializer: serialization2::Serializer {
} }
} }
pub impl Json: serialization2::Serializable { pub impl Json: serialization::Serializable {
fn serialize<S: serialization2::Serializer>(&self, s: &S) { fn serialize<S: serialization::Serializer>(&self, s: &S) {
match *self { match *self {
Number(v) => v.serialize(s), Number(v) => v.serialize(s),
String(ref v) => v.serialize(s), String(ref v) => v.serialize(s),
@ -721,7 +721,7 @@ priv impl Deserializer {
} }
} }
pub impl Deserializer: serialization2::Deserializer { pub impl Deserializer: serialization::Deserializer {
fn read_nil(&self) -> () { fn read_nil(&self) -> () {
debug!("read_nil"); debug!("read_nil");
match *self.pop() { match *self.pop() {

View file

@ -2,131 +2,175 @@
use io::Writer; use io::Writer;
use io::WriterUtil; use io::WriterUtil;
use serialization::Serializer; use serialization;
impl Writer: Serializer { pub struct Serializer {
fn emit_nil() { wr: io::Writer,
self.write_str(~"()")
} }
fn emit_uint(v: uint) { pub fn Serializer(wr: io::Writer) -> Serializer {
self.write_str(fmt!("%?u", v)); Serializer { wr: wr }
} }
fn emit_u64(v: u64) { pub impl Serializer: serialization::Serializer {
self.write_str(fmt!("%?_u64", v)); fn emit_nil(&self) {
self.wr.write_str(~"()")
} }
fn emit_u32(v: u32) { fn emit_uint(&self, v: uint) {
self.write_str(fmt!("%?_u32", v)); self.wr.write_str(fmt!("%?u", v));
} }
fn emit_u16(v: u16) { fn emit_u64(&self, v: u64) {
self.write_str(fmt!("%?_u16", v)); self.wr.write_str(fmt!("%?_u64", v));
} }
fn emit_u8(v: u8) { fn emit_u32(&self, v: u32) {
self.write_str(fmt!("%?_u8", v)); self.wr.write_str(fmt!("%?_u32", v));
} }
fn emit_int(v: int) { fn emit_u16(&self, v: u16) {
self.write_str(fmt!("%?", v)); self.wr.write_str(fmt!("%?_u16", v));
} }
fn emit_i64(v: i64) { fn emit_u8(&self, v: u8) {
self.write_str(fmt!("%?_i64", v)); self.wr.write_str(fmt!("%?_u8", v));
} }
fn emit_i32(v: i32) { fn emit_int(&self, v: int) {
self.write_str(fmt!("%?_i32", v)); self.wr.write_str(fmt!("%?", v));
} }
fn emit_i16(v: i16) { fn emit_i64(&self, v: i64) {
self.write_str(fmt!("%?_i16", v)); self.wr.write_str(fmt!("%?_i64", v));
} }
fn emit_i8(v: i8) { fn emit_i32(&self, v: i32) {
self.write_str(fmt!("%?_i8", v)); self.wr.write_str(fmt!("%?_i32", v));
} }
fn emit_bool(v: bool) { fn emit_i16(&self, v: i16) {
self.write_str(fmt!("%b", v)); self.wr.write_str(fmt!("%?_i16", v));
} }
fn emit_float(v: float) { fn emit_i8(&self, v: i8) {
self.write_str(fmt!("%?_f", v)); self.wr.write_str(fmt!("%?_i8", v));
} }
fn emit_f64(v: f64) { fn emit_bool(&self, v: bool) {
self.write_str(fmt!("%?_f64", v)); self.wr.write_str(fmt!("%b", v));
} }
fn emit_f32(v: f32) { fn emit_float(&self, v: float) {
self.write_str(fmt!("%?_f32", v)); self.wr.write_str(fmt!("%?_f", v));
} }
fn emit_str(v: &str) { fn emit_f64(&self, v: f64) {
self.write_str(fmt!("%?", v)); self.wr.write_str(fmt!("%?_f64", v));
} }
fn emit_enum(_name: &str, f: fn()) { fn emit_f32(&self, v: f32) {
self.wr.write_str(fmt!("%?_f32", v));
}
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(); f();
} }
fn emit_enum_variant(v_name: &str, _v_id: uint, sz: uint, f: fn()) { fn emit_owned(&self, f: fn()) {
self.write_str(v_name); self.wr.write_str(~"~");
if sz > 0u { self.write_str(~"("); }
f();
if sz > 0u { self.write_str(~")"); }
}
fn emit_enum_variant_arg(idx: uint, f: fn()) {
if idx > 0u { self.write_str(~", "); }
f(); f();
} }
fn emit_vec(_len: uint, f: fn()) { fn emit_managed(&self, f: fn()) {
self.write_str(~"["); self.wr.write_str(~"@");
f();
self.write_str(~"]");
}
fn emit_vec_elt(idx: uint, f: fn()) {
if idx > 0u { self.write_str(~", "); }
f(); f();
} }
fn emit_box(f: fn()) { fn emit_enum(&self, _name: &str, f: fn()) {
self.write_str(~"@");
f(); f();
} }
fn emit_uniq(f: fn()) { fn emit_enum_variant(&self, v_name: &str, _v_id: uint, sz: uint,
self.write_str(~"~"); f: fn()) {
self.wr.write_str(v_name);
if sz > 0u { self.wr.write_str(~"("); }
f();
if sz > 0u { self.wr.write_str(~")"); }
}
fn emit_enum_variant_arg(&self, idx: uint, f: fn()) {
if idx > 0u { self.wr.write_str(~", "); }
f(); f();
} }
fn emit_rec(f: fn()) { fn emit_borrowed_vec(&self, _len: uint, f: fn()) {
self.write_str(~"{"); self.wr.write_str(~"&[");
f(); f();
self.write_str(~"}"); self.wr.write_str(~"]");
} }
fn emit_rec_field(f_name: &str, f_idx: uint, f: fn()) { fn emit_owned_vec(&self, _len: uint, f: fn()) {
if f_idx > 0u { self.write_str(~", "); } self.wr.write_str(~"~[");
self.write_str(f_name); f();
self.write_str(~": "); self.wr.write_str(~"]");
}
fn emit_managed_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(~", "); }
f(); f();
} }
fn emit_tup(_sz: uint, f: fn()) { fn emit_rec(&self, f: fn()) {
self.write_str(~"("); self.wr.write_str(~"{");
f(); f();
self.write_str(~")"); self.wr.write_str(~"}");
} }
fn emit_tup_elt(idx: uint, f: fn()) { fn emit_struct(&self, name: &str, f: fn()) {
if idx > 0u { self.write_str(~", "); } 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, _len: uint, f: fn()) {
self.wr.write_str(~"(");
f();
self.wr.write_str(~")");
}
fn emit_tup_elt(&self, idx: uint, f: fn()) {
if idx > 0u { self.wr.write_str(~", "); }
f(); f();
} }
} }

View file

@ -1,176 +1 @@
#[forbid(deprecated_mode)]; pub use prettyprint::*;
use io::Writer;
use io::WriterUtil;
use serialization2;
pub struct Serializer {
wr: io::Writer,
}
pub fn Serializer(wr: io::Writer) -> Serializer {
Serializer { wr: wr }
}
pub impl Serializer: serialization2::Serializer {
fn emit_nil(&self) {
self.wr.write_str(~"()")
}
fn emit_uint(&self, v: uint) {
self.wr.write_str(fmt!("%?u", v));
}
fn emit_u64(&self, v: u64) {
self.wr.write_str(fmt!("%?_u64", v));
}
fn emit_u32(&self, v: u32) {
self.wr.write_str(fmt!("%?_u32", v));
}
fn emit_u16(&self, v: u16) {
self.wr.write_str(fmt!("%?_u16", v));
}
fn emit_u8(&self, v: u8) {
self.wr.write_str(fmt!("%?_u8", v));
}
fn emit_int(&self, v: int) {
self.wr.write_str(fmt!("%?", v));
}
fn emit_i64(&self, v: i64) {
self.wr.write_str(fmt!("%?_i64", v));
}
fn emit_i32(&self, v: i32) {
self.wr.write_str(fmt!("%?_i32", v));
}
fn emit_i16(&self, v: i16) {
self.wr.write_str(fmt!("%?_i16", v));
}
fn emit_i8(&self, v: i8) {
self.wr.write_str(fmt!("%?_i8", v));
}
fn emit_bool(&self, v: bool) {
self.wr.write_str(fmt!("%b", v));
}
fn emit_float(&self, v: float) {
self.wr.write_str(fmt!("%?_f", v));
}
fn emit_f64(&self, v: f64) {
self.wr.write_str(fmt!("%?_f64", v));
}
fn emit_f32(&self, v: f32) {
self.wr.write_str(fmt!("%?_f32", v));
}
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();
}
fn emit_enum_variant(&self, v_name: &str, _v_id: uint, sz: uint,
f: fn()) {
self.wr.write_str(v_name);
if sz > 0u { self.wr.write_str(~"("); }
f();
if sz > 0u { self.wr.write_str(~")"); }
}
fn emit_enum_variant_arg(&self, idx: uint, f: fn()) {
if idx > 0u { self.wr.write_str(~", "); }
f();
}
fn emit_borrowed_vec(&self, _len: uint, f: fn()) {
self.wr.write_str(~"&[");
f();
self.wr.write_str(~"]");
}
fn emit_owned_vec(&self, _len: uint, f: fn()) {
self.wr.write_str(~"~[");
f();
self.wr.write_str(~"]");
}
fn emit_managed_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(~", "); }
f();
}
fn emit_rec(&self, f: fn()) {
self.wr.write_str(~"{");
f();
self.wr.write_str(~"}");
}
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, _len: uint, f: fn()) {
self.wr.write_str(~"(");
f();
self.wr.write_str(~")");
}
fn emit_tup_elt(&self, idx: uint, f: fn()) {
if idx > 0u { self.wr.write_str(~", "); }
f();
}
}

View file

@ -1,81 +1,530 @@
//! Support code for serialization. //! Support code for serialization.
#[allow(deprecated_mode)];
/* /*
Core serialization interfaces. Core serialization interfaces.
*/ */
#[forbid(deprecated_mode)];
#[forbid(non_camel_case_types)];
pub trait Serializer { pub trait Serializer {
// Primitive types: // Primitive types:
fn emit_nil(); fn emit_nil(&self);
fn emit_uint(v: uint); fn emit_uint(&self, v: uint);
fn emit_u64(v: u64); fn emit_u64(&self, v: u64);
fn emit_u32(v: u32); fn emit_u32(&self, v: u32);
fn emit_u16(v: u16); fn emit_u16(&self, v: u16);
fn emit_u8(v: u8); fn emit_u8(&self, v: u8);
fn emit_int(v: int); fn emit_int(&self, v: int);
fn emit_i64(v: i64); fn emit_i64(&self, v: i64);
fn emit_i32(v: i32); fn emit_i32(&self, v: i32);
fn emit_i16(v: i16); fn emit_i16(&self, v: i16);
fn emit_i8(v: i8); fn emit_i8(&self, v: i8);
fn emit_bool(v: bool); fn emit_bool(&self, v: bool);
fn emit_float(v: float); fn emit_float(&self, v: float);
fn emit_f64(v: f64); fn emit_f64(&self, v: f64);
fn emit_f32(v: f32); fn emit_f32(&self, v: f32);
fn emit_str(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: // Compound types:
fn emit_enum(name: &str, f: fn()); fn emit_borrowed(&self, f: fn());
fn emit_enum_variant(v_name: &str, v_id: uint, sz: uint, f: fn()); fn emit_owned(&self, f: fn());
fn emit_enum_variant_arg(idx: uint, f: fn()); fn emit_managed(&self, f: fn());
fn emit_vec(len: uint, f: fn());
fn emit_vec_elt(idx: uint, f: fn()); fn emit_enum(&self, name: &str, f: fn());
fn emit_box(f: fn()); fn emit_enum_variant(&self, v_name: &str, v_id: uint, sz: uint, f: fn());
fn emit_uniq(f: fn()); fn emit_enum_variant_arg(&self, idx: uint, f: fn());
fn emit_rec(f: fn());
fn emit_rec_field(f_name: &str, f_idx: uint, f: fn()); fn emit_borrowed_vec(&self, len: uint, f: fn());
fn emit_tup(sz: uint, f: fn()); fn emit_owned_vec(&self, len: uint, f: fn());
fn emit_tup_elt(idx: uint, f: fn()); fn emit_managed_vec(&self, len: uint, f: fn());
fn emit_vec_elt(&self, idx: uint, f: fn());
fn emit_rec(&self, 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());
} }
pub trait Deserializer { pub trait Deserializer {
// Primitive types: // Primitive types:
fn read_nil() -> (); fn read_nil(&self) -> ();
fn read_uint(&self) -> uint;
fn read_uint() -> uint; fn read_u64(&self) -> u64;
fn read_u64() -> u64; fn read_u32(&self) -> u32;
fn read_u32() -> u32; fn read_u16(&self) -> u16;
fn read_u16() -> u16; fn read_u8(&self) -> u8;
fn read_u8() -> u8; fn read_int(&self) -> int;
fn read_i64(&self) -> i64;
fn read_int() -> int; fn read_i32(&self) -> i32;
fn read_i64() -> i64; fn read_i16(&self) -> i16;
fn read_i32() -> i32; fn read_i8(&self) -> i8;
fn read_i16() -> i16; fn read_bool(&self) -> bool;
fn read_i8() -> i8; fn read_f64(&self) -> f64;
fn read_f32(&self) -> f32;
fn read_float(&self) -> float;
fn read_bool() -> bool; fn read_char(&self) -> char;
fn read_owned_str(&self) -> ~str;
fn read_str() -> ~str; fn read_managed_str(&self) -> @str;
fn read_f64() -> f64;
fn read_f32() -> f32;
fn read_float() -> float;
// Compound types: // Compound types:
fn read_enum<T>(name: &str, f: fn() -> T) -> T; fn read_enum<T>(&self, name: &str, f: fn() -> T) -> T;
fn read_enum_variant<T>(f: fn(uint) -> T) -> T; fn read_enum_variant<T>(&self, f: fn(uint) -> T) -> T;
fn read_enum_variant_arg<T>(idx: uint, f: fn() -> T) -> T; fn read_enum_variant_arg<T>(&self, idx: uint, f: fn() -> T) -> T;
fn read_vec<T>(f: fn(uint) -> T) -> T;
fn read_vec_elt<T>(idx: uint, f: fn() -> T) -> T; fn read_owned<T>(&self, f: fn() -> T) -> T;
fn read_box<T>(f: fn() -> T) -> T; fn read_managed<T>(&self, f: fn() -> T) -> T;
fn read_uniq<T>(f: fn() -> T) -> T;
fn read_rec<T>(f: fn() -> T) -> T; fn read_owned_vec<T>(&self, f: fn(uint) -> T) -> T;
fn read_rec_field<T>(f_name: &str, f_idx: uint, f: fn() -> T) -> T; fn read_managed_vec<T>(&self, f: fn(uint) -> T) -> T;
fn read_tup<T>(sz: uint, f: fn() -> T) -> T; fn read_vec_elt<T>(&self, idx: uint, f: fn() -> T) -> T;
fn read_tup_elt<T>(idx: uint, f: fn() -> T) -> T;
fn read_rec<T>(&self, f: fn() -> T) -> T;
fn read_struct<T>(&self, name: &str, f: fn() -> T) -> T;
fn read_field<T>(&self, name: &str, idx: uint, f: fn() -> T) -> T;
fn read_tup<T>(&self, sz: uint, f: fn() -> T) -> T;
fn read_tup_elt<T>(&self, idx: uint, f: fn() -> T) -> T;
}
pub trait Serializable {
fn serialize<S: Serializer>(&self, s: &S);
}
pub trait Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> self;
}
pub impl uint: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_uint(*self) }
}
pub impl uint: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> uint {
d.read_uint()
}
}
pub impl u8: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u8(*self) }
}
pub impl u8: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> u8 {
d.read_u8()
}
}
pub impl u16: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u16(*self) }
}
pub impl u16: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> u16 {
d.read_u16()
}
}
pub impl u32: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u32(*self) }
}
pub impl u32: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> u32 {
d.read_u32()
}
}
pub impl u64: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u64(*self) }
}
pub impl u64: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> u64 {
d.read_u64()
}
}
pub impl int: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_int(*self) }
}
pub impl int: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> int {
d.read_int()
}
}
pub impl i8: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i8(*self) }
}
pub impl i8: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> i8 {
d.read_i8()
}
}
pub impl i16: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i16(*self) }
}
pub impl i16: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> i16 {
d.read_i16()
}
}
pub impl i32: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i32(*self) }
}
pub impl i32: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> i32 {
d.read_i32()
}
}
pub impl i64: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i64(*self) }
}
pub impl i64: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> i64 {
d.read_i64()
}
}
pub impl &str: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_borrowed_str(*self) }
}
pub impl ~str: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_owned_str(*self) }
}
pub impl ~str: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~str {
d.read_owned_str()
}
}
pub impl @str: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_managed_str(*self) }
}
pub impl @str: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> @str {
d.read_managed_str()
}
}
pub impl float: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_float(*self) }
}
pub impl float: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> float {
d.read_float()
}
}
pub impl f32: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_f32(*self) }
}
pub impl f32: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> f32 {
d.read_f32() }
}
pub impl f64: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_f64(*self) }
}
pub impl f64: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> f64 {
d.read_f64()
}
}
pub impl bool: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_bool(*self) }
}
pub impl bool: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> bool {
d.read_bool()
}
}
pub impl (): Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_nil() }
}
pub impl (): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> () {
d.read_nil()
}
}
pub impl<T: Serializable> &T: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
s.emit_borrowed(|| (**self).serialize(s))
}
}
pub impl<T: Serializable> ~T: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
s.emit_owned(|| (**self).serialize(s))
}
}
pub impl<T: Deserializable> ~T: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~T {
d.read_owned(|| ~deserialize(d))
}
}
pub impl<T: Serializable> @T: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
s.emit_managed(|| (**self).serialize(s))
}
}
pub impl<T: Deserializable> @T: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> @T {
d.read_managed(|| @deserialize(d))
}
}
pub impl<T: Serializable> &[T]: Serializable {
fn serialize<S: Serializer>(&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> ~[T]: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
do s.emit_owned_vec(self.len()) {
for self.eachi |i, e| {
s.emit_vec_elt(i, || e.serialize(s))
}
}
}
}
pub impl<T: Deserializable> ~[T]: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~[T] {
do d.read_owned_vec |len| {
do vec::from_fn(len) |i| {
d.read_vec_elt(i, || deserialize(d))
}
}
}
}
pub impl<T: Serializable> @[T]: Serializable {
fn serialize<S: Serializer>(&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> @[T]: Deserializable {
static fn deserialize<D: Deserializer>(&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<T: Serializable> Option<T>: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
do s.emit_enum(~"option") {
match *self {
None => do s.emit_enum_variant(~"none", 0u, 0u) {
},
Some(ref v) => do s.emit_enum_variant(~"some", 1u, 1u) {
s.emit_enum_variant_arg(0u, || v.serialize(s))
}
}
}
}
}
pub impl<T: Deserializable> Option<T>: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> Option<T> {
do d.read_enum(~"option") {
do d.read_enum_variant |i| {
match i {
0 => None,
1 => Some(d.read_enum_variant_arg(0u, || deserialize(d))),
_ => fail(#fmt("Bad variant for option: %u", i))
}
}
}
}
}
pub impl<
T0: Serializable,
T1: Serializable
> (T0, T1): Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
match *self {
(ref t0, ref t1) => {
do s.emit_tup(2) {
s.emit_tup_elt(0, || t0.serialize(s));
s.emit_tup_elt(1, || t1.serialize(s));
}
}
}
}
}
pub impl<
T0: Deserializable,
T1: Deserializable
> (T0, T1): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1) {
do d.read_tup(2) {
(
d.read_tup_elt(0, || deserialize(d)),
d.read_tup_elt(1, || deserialize(d))
)
}
}
}
pub impl<
T0: Serializable,
T1: Serializable,
T2: Serializable
> (T0, T1, T2): Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
match *self {
(ref t0, ref t1, ref t2) => {
do s.emit_tup(3) {
s.emit_tup_elt(0, || t0.serialize(s));
s.emit_tup_elt(1, || t1.serialize(s));
s.emit_tup_elt(2, || t2.serialize(s));
}
}
}
}
}
pub impl<
T0: Deserializable,
T1: Deserializable,
T2: Deserializable
> (T0, T1, T2): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1, T2) {
do d.read_tup(3) {
(
d.read_tup_elt(0, || deserialize(d)),
d.read_tup_elt(1, || deserialize(d)),
d.read_tup_elt(2, || deserialize(d))
)
}
}
}
pub impl<
T0: Serializable,
T1: Serializable,
T2: Serializable,
T3: Serializable
> (T0, T1, T2, T3): Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
match *self {
(ref t0, ref t1, ref t2, ref t3) => {
do s.emit_tup(4) {
s.emit_tup_elt(0, || t0.serialize(s));
s.emit_tup_elt(1, || t1.serialize(s));
s.emit_tup_elt(2, || t2.serialize(s));
s.emit_tup_elt(3, || t3.serialize(s));
}
}
}
}
}
pub impl<
T0: Deserializable,
T1: Deserializable,
T2: Deserializable,
T3: Deserializable
> (T0, T1, T2, T3): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1, T2, T3) {
do d.read_tup(4) {
(
d.read_tup_elt(0, || deserialize(d)),
d.read_tup_elt(1, || deserialize(d)),
d.read_tup_elt(2, || deserialize(d)),
d.read_tup_elt(3, || deserialize(d))
)
}
}
}
pub impl<
T0: Serializable,
T1: Serializable,
T2: Serializable,
T3: Serializable,
T4: Serializable
> (T0, T1, T2, T3, T4): Serializable {
fn serialize<S: Serializer>(&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.serialize(s));
s.emit_tup_elt(1, || t1.serialize(s));
s.emit_tup_elt(2, || t2.serialize(s));
s.emit_tup_elt(3, || t3.serialize(s));
s.emit_tup_elt(4, || t4.serialize(s));
}
}
}
}
}
pub impl<
T0: Deserializable,
T1: Deserializable,
T2: Deserializable,
T3: Deserializable,
T4: Deserializable
> (T0, T1, T2, T3, T4): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D)
-> (T0, T1, T2, T3, T4) {
do d.read_tup(5) {
(
d.read_tup_elt(0, || deserialize(d)),
d.read_tup_elt(1, || deserialize(d)),
d.read_tup_elt(2, || deserialize(d)),
d.read_tup_elt(3, || deserialize(d)),
d.read_tup_elt(4, || deserialize(d))
)
}
}
} }
// ___________________________________________________________________________ // ___________________________________________________________________________
@ -83,188 +532,31 @@ pub trait Deserializer {
// //
// In some cases, these should eventually be coded as traits. // In some cases, these should eventually be coded as traits.
pub fn emit_from_vec<S: Serializer, T>(&&s: S, &&v: ~[T], f: fn(&&x: T)) {
do s.emit_vec(vec::len(v)) {
for vec::eachi(v) |i,e| {
do s.emit_vec_elt(i) {
f(*e)
}
}
}
}
pub fn read_to_vec<D: Deserializer, T: Copy>(&&d: D, f: fn() -> T) -> ~[T] {
do d.read_vec |len| {
do vec::from_fn(len) |i| {
d.read_vec_elt(i, || f())
}
}
}
pub trait SerializerHelpers { pub trait SerializerHelpers {
fn emit_from_vec<T>(&&v: ~[T], f: fn(&&x: T)); fn emit_from_vec<T>(&self, v: &[T], f: fn(&T));
} }
impl<S: Serializer> S: SerializerHelpers { pub impl<S: Serializer> S: SerializerHelpers {
fn emit_from_vec<T>(&&v: ~[T], f: fn(&&x: T)) { fn emit_from_vec<T>(&self, v: &[T], f: fn(&T)) {
emit_from_vec(self, v, f) do self.emit_owned_vec(v.len()) {
for v.eachi |i, e| {
do self.emit_vec_elt(i) {
f(e)
}
}
}
} }
} }
pub trait DeserializerHelpers { pub trait DeserializerHelpers {
fn read_to_vec<T: Copy>(f: fn() -> T) -> ~[T]; fn read_to_vec<T>(&self, f: fn() -> T) -> ~[T];
} }
impl<D: Deserializer> D: DeserializerHelpers { pub impl<D: Deserializer> D: DeserializerHelpers {
fn read_to_vec<T: Copy>(f: fn() -> T) -> ~[T] { fn read_to_vec<T>(&self, f: fn() -> T) -> ~[T] {
read_to_vec(self, f) do self.read_owned_vec |len| {
} do vec::from_fn(len) |i| {
} self.read_vec_elt(i, || f())
pub fn serialize_uint<S: Serializer>(&&s: S, v: uint) {
s.emit_uint(v);
}
pub fn deserialize_uint<D: Deserializer>(&&d: D) -> uint {
d.read_uint()
}
pub fn serialize_u8<S: Serializer>(&&s: S, v: u8) {
s.emit_u8(v);
}
pub fn deserialize_u8<D: Deserializer>(&&d: D) -> u8 {
d.read_u8()
}
pub fn serialize_u16<S: Serializer>(&&s: S, v: u16) {
s.emit_u16(v);
}
pub fn deserialize_u16<D: Deserializer>(&&d: D) -> u16 {
d.read_u16()
}
pub fn serialize_u32<S: Serializer>(&&s: S, v: u32) {
s.emit_u32(v);
}
pub fn deserialize_u32<D: Deserializer>(&&d: D) -> u32 {
d.read_u32()
}
pub fn serialize_u64<S: Serializer>(&&s: S, v: u64) {
s.emit_u64(v);
}
pub fn deserialize_u64<D: Deserializer>(&&d: D) -> u64 {
d.read_u64()
}
pub fn serialize_int<S: Serializer>(&&s: S, v: int) {
s.emit_int(v);
}
pub fn deserialize_int<D: Deserializer>(&&d: D) -> int {
d.read_int()
}
pub fn serialize_i8<S: Serializer>(&&s: S, v: i8) {
s.emit_i8(v);
}
pub fn deserialize_i8<D: Deserializer>(&&d: D) -> i8 {
d.read_i8()
}
pub fn serialize_i16<S: Serializer>(&&s: S, v: i16) {
s.emit_i16(v);
}
pub fn deserialize_i16<D: Deserializer>(&&d: D) -> i16 {
d.read_i16()
}
pub fn serialize_i32<S: Serializer>(&&s: S, v: i32) {
s.emit_i32(v);
}
pub fn deserialize_i32<D: Deserializer>(&&d: D) -> i32 {
d.read_i32()
}
pub fn serialize_i64<S: Serializer>(&&s: S, v: i64) {
s.emit_i64(v);
}
pub fn deserialize_i64<D: Deserializer>(&&d: D) -> i64 {
d.read_i64()
}
pub fn serialize_str<S: Serializer>(&&s: S, v: &str) {
s.emit_str(v);
}
pub fn deserialize_str<D: Deserializer>(&&d: D) -> ~str {
d.read_str()
}
pub fn serialize_float<S: Serializer>(&&s: S, v: float) {
s.emit_float(v);
}
pub fn deserialize_float<D: Deserializer>(&&d: D) -> float {
d.read_float()
}
pub fn serialize_f32<S: Serializer>(&&s: S, v: f32) {
s.emit_f32(v);
}
pub fn deserialize_f32<D: Deserializer>(&&d: D) -> f32 {
d.read_f32()
}
pub fn serialize_f64<S: Serializer>(&&s: S, v: f64) {
s.emit_f64(v);
}
pub fn deserialize_f64<D: Deserializer>(&&d: D) -> f64 {
d.read_f64()
}
pub fn serialize_bool<S: Serializer>(&&s: S, v: bool) {
s.emit_bool(v);
}
pub fn deserialize_bool<D: Deserializer>(&&d: D) -> bool {
d.read_bool()
}
pub fn serialize_Option<S: Serializer,T>(&&s: S, &&v: Option<T>,
st: fn(&&x: T)) {
do s.emit_enum(~"option") {
match v {
None => do s.emit_enum_variant(~"none", 0u, 0u) {
},
Some(ref v) => do s.emit_enum_variant(~"some", 1u, 1u) {
do s.emit_enum_variant_arg(0u) {
st(*v)
}
}
}
}
}
pub fn deserialize_Option<D: Deserializer,T: Copy>(&&d: D, st: fn() -> T)
-> Option<T> {
do d.read_enum(~"option") {
do d.read_enum_variant |i| {
match i {
0 => None,
1 => Some(d.read_enum_variant_arg(0u, || st() )),
_ => fail(#fmt("Bad variant for option: %u", i))
} }
} }
} }

View file

@ -4,560 +4,4 @@
Core serialization interfaces. Core serialization interfaces.
*/ */
#[forbid(deprecated_mode)]; pub use serialization::*;
#[forbid(non_camel_case_types)];
pub trait Serializer {
// Primitive types:
fn emit_nil(&self);
fn emit_uint(&self, v: uint);
fn emit_u64(&self, v: u64);
fn emit_u32(&self, v: u32);
fn emit_u16(&self, v: u16);
fn emit_u8(&self, v: u8);
fn emit_int(&self, v: int);
fn emit_i64(&self, v: i64);
fn emit_i32(&self, v: i32);
fn emit_i16(&self, v: i16);
fn emit_i8(&self, v: i8);
fn emit_bool(&self, v: bool);
fn emit_float(&self, v: float);
fn emit_f64(&self, v: f64);
fn emit_f32(&self, v: f32);
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_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_rec(&self, 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());
}
pub trait Deserializer {
// Primitive types:
fn read_nil(&self) -> ();
fn read_uint(&self) -> uint;
fn read_u64(&self) -> u64;
fn read_u32(&self) -> u32;
fn read_u16(&self) -> u16;
fn read_u8(&self) -> u8;
fn read_int(&self) -> int;
fn read_i64(&self) -> i64;
fn read_i32(&self) -> i32;
fn read_i16(&self) -> i16;
fn read_i8(&self) -> i8;
fn read_bool(&self) -> bool;
fn read_f64(&self) -> f64;
fn read_f32(&self) -> f32;
fn read_float(&self) -> float;
fn read_char(&self) -> char;
fn read_owned_str(&self) -> ~str;
fn read_managed_str(&self) -> @str;
// Compound types:
fn read_enum<T>(&self, name: &str, f: fn() -> T) -> T;
fn read_enum_variant<T>(&self, f: fn(uint) -> T) -> T;
fn read_enum_variant_arg<T>(&self, idx: uint, f: fn() -> T) -> T;
fn read_owned<T>(&self, f: fn() -> T) -> T;
fn read_managed<T>(&self, f: fn() -> T) -> T;
fn read_owned_vec<T>(&self, f: fn(uint) -> T) -> T;
fn read_managed_vec<T>(&self, f: fn(uint) -> T) -> T;
fn read_vec_elt<T>(&self, idx: uint, f: fn() -> T) -> T;
fn read_rec<T>(&self, f: fn() -> T) -> T;
fn read_struct<T>(&self, name: &str, f: fn() -> T) -> T;
fn read_field<T>(&self, name: &str, idx: uint, f: fn() -> T) -> T;
fn read_tup<T>(&self, sz: uint, f: fn() -> T) -> T;
fn read_tup_elt<T>(&self, idx: uint, f: fn() -> T) -> T;
}
pub trait Serializable {
fn serialize<S: Serializer>(&self, s: &S);
}
pub trait Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> self;
}
pub impl uint: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_uint(*self) }
}
pub impl uint: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> uint {
d.read_uint()
}
}
pub impl u8: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u8(*self) }
}
pub impl u8: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> u8 {
d.read_u8()
}
}
pub impl u16: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u16(*self) }
}
pub impl u16: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> u16 {
d.read_u16()
}
}
pub impl u32: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u32(*self) }
}
pub impl u32: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> u32 {
d.read_u32()
}
}
pub impl u64: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u64(*self) }
}
pub impl u64: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> u64 {
d.read_u64()
}
}
pub impl int: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_int(*self) }
}
pub impl int: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> int {
d.read_int()
}
}
pub impl i8: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i8(*self) }
}
pub impl i8: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> i8 {
d.read_i8()
}
}
pub impl i16: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i16(*self) }
}
pub impl i16: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> i16 {
d.read_i16()
}
}
pub impl i32: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i32(*self) }
}
pub impl i32: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> i32 {
d.read_i32()
}
}
pub impl i64: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i64(*self) }
}
pub impl i64: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> i64 {
d.read_i64()
}
}
pub impl &str: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_borrowed_str(*self) }
}
pub impl ~str: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_owned_str(*self) }
}
pub impl ~str: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~str {
d.read_owned_str()
}
}
pub impl @str: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_managed_str(*self) }
}
pub impl @str: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> @str {
d.read_managed_str()
}
}
pub impl float: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_float(*self) }
}
pub impl float: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> float {
d.read_float()
}
}
pub impl f32: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_f32(*self) }
}
pub impl f32: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> f32 {
d.read_f32() }
}
pub impl f64: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_f64(*self) }
}
pub impl f64: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> f64 {
d.read_f64()
}
}
pub impl bool: Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_bool(*self) }
}
pub impl bool: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> bool {
d.read_bool()
}
}
pub impl (): Serializable {
fn serialize<S: Serializer>(&self, s: &S) { s.emit_nil() }
}
pub impl (): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> () {
d.read_nil()
}
}
pub impl<T: Serializable> &T: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
s.emit_borrowed(|| (**self).serialize(s))
}
}
pub impl<T: Serializable> ~T: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
s.emit_owned(|| (**self).serialize(s))
}
}
pub impl<T: Deserializable> ~T: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~T {
d.read_owned(|| ~deserialize(d))
}
}
pub impl<T: Serializable> @T: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
s.emit_managed(|| (**self).serialize(s))
}
}
pub impl<T: Deserializable> @T: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> @T {
d.read_managed(|| @deserialize(d))
}
}
pub impl<T: Serializable> &[T]: Serializable {
fn serialize<S: Serializer>(&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> ~[T]: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
do s.emit_owned_vec(self.len()) {
for self.eachi |i, e| {
s.emit_vec_elt(i, || e.serialize(s))
}
}
}
}
pub impl<T: Deserializable> ~[T]: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~[T] {
do d.read_owned_vec |len| {
do vec::from_fn(len) |i| {
d.read_vec_elt(i, || deserialize(d))
}
}
}
}
pub impl<T: Serializable> @[T]: Serializable {
fn serialize<S: Serializer>(&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> @[T]: Deserializable {
static fn deserialize<D: Deserializer>(&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<T: Serializable> Option<T>: Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
do s.emit_enum(~"option") {
match *self {
None => do s.emit_enum_variant(~"none", 0u, 0u) {
},
Some(ref v) => do s.emit_enum_variant(~"some", 1u, 1u) {
s.emit_enum_variant_arg(0u, || v.serialize(s))
}
}
}
}
}
pub impl<T: Deserializable> Option<T>: Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> Option<T> {
do d.read_enum(~"option") {
do d.read_enum_variant |i| {
match i {
0 => None,
1 => Some(d.read_enum_variant_arg(0u, || deserialize(d))),
_ => fail(#fmt("Bad variant for option: %u", i))
}
}
}
}
}
pub impl<
T0: Serializable,
T1: Serializable
> (T0, T1): Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
match *self {
(ref t0, ref t1) => {
do s.emit_tup(2) {
s.emit_tup_elt(0, || t0.serialize(s));
s.emit_tup_elt(1, || t1.serialize(s));
}
}
}
}
}
pub impl<
T0: Deserializable,
T1: Deserializable
> (T0, T1): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1) {
do d.read_tup(2) {
(
d.read_tup_elt(0, || deserialize(d)),
d.read_tup_elt(1, || deserialize(d))
)
}
}
}
pub impl<
T0: Serializable,
T1: Serializable,
T2: Serializable
> (T0, T1, T2): Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
match *self {
(ref t0, ref t1, ref t2) => {
do s.emit_tup(3) {
s.emit_tup_elt(0, || t0.serialize(s));
s.emit_tup_elt(1, || t1.serialize(s));
s.emit_tup_elt(2, || t2.serialize(s));
}
}
}
}
}
pub impl<
T0: Deserializable,
T1: Deserializable,
T2: Deserializable
> (T0, T1, T2): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1, T2) {
do d.read_tup(3) {
(
d.read_tup_elt(0, || deserialize(d)),
d.read_tup_elt(1, || deserialize(d)),
d.read_tup_elt(2, || deserialize(d))
)
}
}
}
pub impl<
T0: Serializable,
T1: Serializable,
T2: Serializable,
T3: Serializable
> (T0, T1, T2, T3): Serializable {
fn serialize<S: Serializer>(&self, s: &S) {
match *self {
(ref t0, ref t1, ref t2, ref t3) => {
do s.emit_tup(4) {
s.emit_tup_elt(0, || t0.serialize(s));
s.emit_tup_elt(1, || t1.serialize(s));
s.emit_tup_elt(2, || t2.serialize(s));
s.emit_tup_elt(3, || t3.serialize(s));
}
}
}
}
}
pub impl<
T0: Deserializable,
T1: Deserializable,
T2: Deserializable,
T3: Deserializable
> (T0, T1, T2, T3): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1, T2, T3) {
do d.read_tup(4) {
(
d.read_tup_elt(0, || deserialize(d)),
d.read_tup_elt(1, || deserialize(d)),
d.read_tup_elt(2, || deserialize(d)),
d.read_tup_elt(3, || deserialize(d))
)
}
}
}
pub impl<
T0: Serializable,
T1: Serializable,
T2: Serializable,
T3: Serializable,
T4: Serializable
> (T0, T1, T2, T3, T4): Serializable {
fn serialize<S: Serializer>(&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.serialize(s));
s.emit_tup_elt(1, || t1.serialize(s));
s.emit_tup_elt(2, || t2.serialize(s));
s.emit_tup_elt(3, || t3.serialize(s));
s.emit_tup_elt(4, || t4.serialize(s));
}
}
}
}
}
pub impl<
T0: Deserializable,
T1: Deserializable,
T2: Deserializable,
T3: Deserializable,
T4: Deserializable
> (T0, T1, T2, T3, T4): Deserializable {
static fn deserialize<D: Deserializer>(&self, d: &D)
-> (T0, T1, T2, T3, T4) {
do d.read_tup(5) {
(
d.read_tup_elt(0, || deserialize(d)),
d.read_tup_elt(1, || deserialize(d)),
d.read_tup_elt(2, || deserialize(d)),
d.read_tup_elt(3, || deserialize(d)),
d.read_tup_elt(4, || deserialize(d))
)
}
}
}
// ___________________________________________________________________________
// Helper routines
//
// In some cases, these should eventually be coded as traits.
pub trait SerializerHelpers {
fn emit_from_vec<T>(&self, v: &[T], f: fn(&T));
}
pub impl<S: Serializer> S: SerializerHelpers {
fn emit_from_vec<T>(&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)
}
}
}
}
}
pub trait DeserializerHelpers {
fn read_to_vec<T>(&self, f: fn() -> T) -> ~[T];
}
pub impl<D: Deserializer> D: DeserializerHelpers {
fn read_to_vec<T>(&self, f: fn() -> T) -> ~[T] {
do self.read_owned_vec |len| {
do vec::from_fn(len) |i| {
self.read_vec_elt(i, || f())
}
}
}
}

View file

@ -1,6 +1,6 @@
// The Rust abstract syntax tree. // The Rust abstract syntax tree.
use std::serialization2::{Serializable, use std::serialization::{Serializable,
Deserializable, Deserializable,
Serializer, Serializer,
Deserializer}; Deserializer};
@ -18,8 +18,8 @@ impl span: Deserializable {
} }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type spanned<T> = {node: T, span: span}; type spanned<T> = {node: T, span: span};
@ -74,8 +74,8 @@ impl ident: to_bytes::IterBytes {
// Functions may or may not have names. // Functions may or may not have names.
type fn_ident = Option<ident>; type fn_ident = Option<ident>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type path = {span: span, type path = {span: span,
global: bool, global: bool,
idents: ~[ident], idents: ~[ident],
@ -86,8 +86,8 @@ type crate_num = int;
type node_id = int; type node_id = int;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type def_id = {crate: crate_num, node: node_id}; type def_id = {crate: crate_num, node: node_id};
impl def_id : cmp::Eq { impl def_id : cmp::Eq {
@ -100,8 +100,8 @@ impl def_id : cmp::Eq {
const local_crate: crate_num = 0; const local_crate: crate_num = 0;
const crate_node_id: node_id = 0; const crate_node_id: node_id = 0;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum ty_param_bound { enum ty_param_bound {
bound_copy, bound_copy,
bound_send, bound_send,
@ -110,12 +110,12 @@ enum ty_param_bound {
bound_trait(@ty), bound_trait(@ty),
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type ty_param = {ident: ident, id: node_id, bounds: @~[ty_param_bound]}; type ty_param = {ident: ident, id: node_id, bounds: @~[ty_param_bound]};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum def { enum def {
def_fn(def_id, purity), def_fn(def_id, purity),
def_static_method(def_id, purity), def_static_method(def_id, purity),
@ -294,8 +294,8 @@ type crate_directive = spanned<crate_directive_>;
type meta_item = spanned<meta_item_>; type meta_item = spanned<meta_item_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum meta_item_ { enum meta_item_ {
meta_word(~str), meta_word(~str),
meta_list(~str, ~[@meta_item]), meta_list(~str, ~[@meta_item]),
@ -304,24 +304,24 @@ enum meta_item_ {
type blk = spanned<blk_>; type blk = spanned<blk_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type blk_ = {view_items: ~[@view_item], type blk_ = {view_items: ~[@view_item],
stmts: ~[@stmt], stmts: ~[@stmt],
expr: Option<@expr>, expr: Option<@expr>,
id: node_id, id: node_id,
rules: blk_check_mode}; rules: blk_check_mode};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type pat = {id: node_id, node: pat_, span: span}; type pat = {id: node_id, node: pat_, span: span};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type field_pat = {ident: ident, pat: @pat}; type field_pat = {ident: ident, pat: @pat};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum binding_mode { enum binding_mode {
bind_by_value, bind_by_value,
bind_by_move, bind_by_move,
@ -377,8 +377,8 @@ impl binding_mode : cmp::Eq {
pure fn ne(other: &binding_mode) -> bool { !self.eq(other) } pure fn ne(other: &binding_mode) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum pat_ { enum pat_ {
pat_wild, pat_wild,
// A pat_ident may either be a new bound variable, // A pat_ident may either be a new bound variable,
@ -401,8 +401,8 @@ enum pat_ {
pat_range(@expr, @expr), pat_range(@expr, @expr),
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum mutability { m_mutbl, m_imm, m_const, } enum mutability { m_mutbl, m_imm, m_const, }
impl mutability : to_bytes::IterBytes { impl mutability : to_bytes::IterBytes {
@ -418,8 +418,8 @@ impl mutability : cmp::Eq {
pure fn ne(other: &mutability) -> bool { !self.eq(other) } pure fn ne(other: &mutability) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum proto { enum proto {
proto_bare, // foreign fn proto_bare, // foreign fn
proto_uniq, // fn~ proto_uniq, // fn~
@ -434,8 +434,8 @@ impl proto : cmp::Eq {
pure fn ne(other: &proto) -> bool { !self.eq(other) } pure fn ne(other: &proto) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum vstore { enum vstore {
// FIXME (#2112): Change uint to @expr (actually only constant exprs) // FIXME (#2112): Change uint to @expr (actually only constant exprs)
vstore_fixed(Option<uint>), // [1,2,3,4]/_ or 4 vstore_fixed(Option<uint>), // [1,2,3,4]/_ or 4
@ -444,8 +444,8 @@ enum vstore {
vstore_slice(@region) // &[1,2,3,4](foo)? vstore_slice(@region) // &[1,2,3,4](foo)?
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum expr_vstore { enum expr_vstore {
// FIXME (#2112): Change uint to @expr (actually only constant exprs) // FIXME (#2112): Change uint to @expr (actually only constant exprs)
expr_vstore_fixed(Option<uint>), // [1,2,3,4]/_ or 4 expr_vstore_fixed(Option<uint>), // [1,2,3,4]/_ or 4
@ -461,8 +461,8 @@ pure fn is_blockish(p: ast::proto) -> bool {
} }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum binop { enum binop {
add, add,
subtract, subtract,
@ -491,8 +491,8 @@ impl binop : cmp::Eq {
pure fn ne(other: &binop) -> bool { !self.eq(other) } pure fn ne(other: &binop) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum unop { enum unop {
box(mutability), box(mutability),
uniq(mutability), uniq(mutability),
@ -543,8 +543,8 @@ impl unop : cmp::Eq {
// Generally, after typeck you can get the inferred value // Generally, after typeck you can get the inferred value
// using ty::resolved_T(...). // using ty::resolved_T(...).
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum inferable<T> { enum inferable<T> {
expl(T), expl(T),
infer(node_id) infer(node_id)
@ -583,8 +583,8 @@ impl<T:cmp::Eq> inferable<T> : cmp::Eq {
} }
// "resolved" mode: the real modes. // "resolved" mode: the real modes.
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum rmode { by_ref, by_val, by_move, by_copy } enum rmode { by_ref, by_val, by_move, by_copy }
impl rmode : to_bytes::IterBytes { impl rmode : to_bytes::IterBytes {
@ -606,8 +606,8 @@ type mode = inferable<rmode>;
type stmt = spanned<stmt_>; type stmt = spanned<stmt_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum stmt_ { enum stmt_ {
stmt_decl(@decl, node_id), stmt_decl(@decl, node_id),
@ -618,8 +618,8 @@ enum stmt_ {
stmt_semi(@expr, node_id), stmt_semi(@expr, node_id),
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum init_op { init_assign, init_move, } enum init_op { init_assign, init_move, }
impl init_op : cmp::Eq { impl init_op : cmp::Eq {
@ -642,14 +642,14 @@ impl init_op : cmp::Eq {
pure fn ne(other: &init_op) -> bool { !self.eq(other) } pure fn ne(other: &init_op) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type initializer = {op: init_op, expr: @expr}; type initializer = {op: init_op, expr: @expr};
// FIXME (pending discussion of #1697, #2178...): local should really be // FIXME (pending discussion of #1697, #2178...): local should really be
// a refinement on pat. // a refinement on pat.
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type local_ = {is_mutbl: bool, ty: @ty, pat: @pat, type local_ = {is_mutbl: bool, ty: @ty, pat: @pat,
init: Option<initializer>, id: node_id}; init: Option<initializer>, id: node_id};
@ -657,22 +657,22 @@ type local = spanned<local_>;
type decl = spanned<decl_>; type decl = spanned<decl_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum decl_ { decl_local(~[@local]), decl_item(@item), } enum decl_ { decl_local(~[@local]), decl_item(@item), }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type arm = {pats: ~[@pat], guard: Option<@expr>, body: blk}; type arm = {pats: ~[@pat], guard: Option<@expr>, body: blk};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type field_ = {mutbl: mutability, ident: ident, expr: @expr}; type field_ = {mutbl: mutability, ident: ident, expr: @expr};
type field = spanned<field_>; type field = spanned<field_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum blk_check_mode { default_blk, unsafe_blk, } enum blk_check_mode { default_blk, unsafe_blk, }
impl blk_check_mode : cmp::Eq { impl blk_check_mode : cmp::Eq {
@ -687,22 +687,22 @@ impl blk_check_mode : cmp::Eq {
pure fn ne(other: &blk_check_mode) -> bool { !self.eq(other) } pure fn ne(other: &blk_check_mode) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type expr = {id: node_id, callee_id: node_id, node: expr_, span: span}; type expr = {id: node_id, callee_id: node_id, node: expr_, span: span};
// Extra node ID is only used for index, assign_op, unary, binary // Extra node ID is only used for index, assign_op, unary, binary
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum log_level { error, debug, other } enum log_level { error, debug, other }
// 0 = error, 1 = debug, 2 = other // 0 = error, 1 = debug, 2 = other
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum alt_mode { alt_check, alt_exhaustive, } enum alt_mode { alt_check, alt_exhaustive, }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum expr_ { enum expr_ {
expr_vstore(@expr, expr_vstore), expr_vstore(@expr, expr_vstore),
expr_vec(~[@expr], mutability), expr_vec(~[@expr], mutability),
@ -758,8 +758,8 @@ enum expr_ {
expr_repeat(@expr /* element */, @expr /* count */, mutability) expr_repeat(@expr /* element */, @expr /* count */, mutability)
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type capture_item_ = { type capture_item_ = {
id: int, id: int,
is_move: bool, is_move: bool,
@ -787,8 +787,8 @@ type capture_clause = @~[capture_item];
// else knows what to do with them, so you'll probably get a syntax // else knows what to do with them, so you'll probably get a syntax
// error. // error.
// //
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
#[doc="For macro invocations; parsing is delegated to the macro"] #[doc="For macro invocations; parsing is delegated to the macro"]
enum token_tree { enum token_tree {
tt_tok(span, token::token), tt_tok(span, token::token),
@ -852,8 +852,8 @@ enum token_tree {
// //
type matcher = spanned<matcher_>; type matcher = spanned<matcher_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum matcher_ { enum matcher_ {
// match one token // match one token
match_tok(token::token), match_tok(token::token),
@ -868,14 +868,14 @@ type mac = spanned<mac_>;
type mac_arg = Option<@expr>; type mac_arg = Option<@expr>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type mac_body_ = {span: span}; type mac_body_ = {span: span};
type mac_body = Option<mac_body_>; type mac_body = Option<mac_body_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum mac_ { enum mac_ {
mac_invoc(@path, mac_arg, mac_body), // old macro-invocation mac_invoc(@path, mac_arg, mac_body), // old macro-invocation
mac_invoc_tt(@path,~[token_tree]), // new macro-invocation mac_invoc_tt(@path,~[token_tree]), // new macro-invocation
@ -888,8 +888,8 @@ enum mac_ {
type lit = spanned<lit_>; type lit = spanned<lit_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum lit_ { enum lit_ {
lit_str(@~str), lit_str(@~str),
lit_int(i64, int_ty), lit_int(i64, int_ty),
@ -930,24 +930,24 @@ impl ast::lit_: cmp::Eq {
// NB: If you change this, you'll probably want to change the corresponding // NB: If you change this, you'll probably want to change the corresponding
// type structure in middle/ty.rs as well. // type structure in middle/ty.rs as well.
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type mt = {ty: @ty, mutbl: mutability}; type mt = {ty: @ty, mutbl: mutability};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type ty_field_ = {ident: ident, mt: mt}; type ty_field_ = {ident: ident, mt: mt};
type ty_field = spanned<ty_field_>; type ty_field = spanned<ty_field_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type ty_method = {ident: ident, attrs: ~[attribute], purity: purity, type ty_method = {ident: ident, attrs: ~[attribute], purity: purity,
decl: fn_decl, tps: ~[ty_param], self_ty: self_ty, decl: fn_decl, tps: ~[ty_param], self_ty: self_ty,
id: node_id, span: span}; id: node_id, span: span};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
// A trait method is either required (meaning it doesn't have an // A trait method is either required (meaning it doesn't have an
// implementation, just a signature) or provided (meaning it has a default // implementation, just a signature) or provided (meaning it has a default
// implementation). // implementation).
@ -956,8 +956,8 @@ enum trait_method {
provided(@method), provided(@method),
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, }
impl int_ty : to_bytes::IterBytes { impl int_ty : to_bytes::IterBytes {
@ -986,8 +986,8 @@ impl int_ty : cmp::Eq {
pure fn ne(other: &int_ty) -> bool { !self.eq(other) } pure fn ne(other: &int_ty) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, }
impl uint_ty : to_bytes::IterBytes { impl uint_ty : to_bytes::IterBytes {
@ -1014,8 +1014,8 @@ impl uint_ty : cmp::Eq {
pure fn ne(other: &uint_ty) -> bool { !self.eq(other) } pure fn ne(other: &uint_ty) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum float_ty { ty_f, ty_f32, ty_f64, } enum float_ty { ty_f, ty_f32, ty_f64, }
impl float_ty : to_bytes::IterBytes { impl float_ty : to_bytes::IterBytes {
@ -1033,13 +1033,13 @@ impl float_ty : cmp::Eq {
pure fn ne(other: &float_ty) -> bool { !self.eq(other) } pure fn ne(other: &float_ty) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type ty = {id: node_id, node: ty_, span: span}; type ty = {id: node_id, node: ty_, span: span};
// Not represented directly in the AST, referred to by name through a ty_path. // Not represented directly in the AST, referred to by name through a ty_path.
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum prim_ty { enum prim_ty {
ty_int(int_ty), ty_int(int_ty),
ty_uint(uint_ty), ty_uint(uint_ty),
@ -1086,12 +1086,12 @@ impl prim_ty : cmp::Eq {
pure fn ne(other: &prim_ty) -> bool { !self.eq(other) } pure fn ne(other: &prim_ty) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type region = {id: node_id, node: region_}; type region = {id: node_id, node: region_};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum region_ { enum region_ {
re_anon, re_anon,
re_static, re_static,
@ -1099,8 +1099,8 @@ enum region_ {
re_named(ident) re_named(ident)
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum ty_ { enum ty_ {
ty_nil, ty_nil,
ty_bot, /* bottom type */ ty_bot, /* bottom type */
@ -1139,19 +1139,19 @@ impl ty : to_bytes::IterBytes {
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type arg = {mode: mode, ty: @ty, ident: ident, id: node_id}; type arg = {mode: mode, ty: @ty, ident: ident, id: node_id};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type fn_decl = type fn_decl =
{inputs: ~[arg], {inputs: ~[arg],
output: @ty, output: @ty,
cf: ret_style}; cf: ret_style};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum purity { enum purity {
pure_fn, // declared with "pure fn" pure_fn, // declared with "pure fn"
unsafe_fn, // declared with "unsafe fn" unsafe_fn, // declared with "unsafe fn"
@ -1172,8 +1172,8 @@ impl purity : cmp::Eq {
pure fn ne(other: &purity) -> bool { !self.eq(other) } pure fn ne(other: &purity) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum ret_style { enum ret_style {
noreturn, // functions with return type _|_ that always noreturn, // functions with return type _|_ that always
// raise an error or exit (i.e. never return to the caller) // raise an error or exit (i.e. never return to the caller)
@ -1198,8 +1198,8 @@ impl ret_style : cmp::Eq {
pure fn ne(other: &ret_style) -> bool { !self.eq(other) } pure fn ne(other: &ret_style) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum self_ty_ { enum self_ty_ {
sty_static, // no self: static method sty_static, // no self: static method
sty_by_ref, // old by-reference self: `` sty_by_ref, // old by-reference self: ``
@ -1255,20 +1255,20 @@ impl self_ty_ : cmp::Eq {
type self_ty = spanned<self_ty_>; type self_ty = spanned<self_ty_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type method = {ident: ident, attrs: ~[attribute], type method = {ident: ident, attrs: ~[attribute],
tps: ~[ty_param], self_ty: self_ty, tps: ~[ty_param], self_ty: self_ty,
purity: purity, decl: fn_decl, body: blk, purity: purity, decl: fn_decl, body: blk,
id: node_id, span: span, self_id: node_id, id: node_id, span: span, self_id: node_id,
vis: visibility}; vis: visibility};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type _mod = {view_items: ~[@view_item], items: ~[@item]}; type _mod = {view_items: ~[@view_item], items: ~[@item]};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum foreign_abi { enum foreign_abi {
foreign_abi_rust_intrinsic, foreign_abi_rust_intrinsic,
foreign_abi_cdecl, foreign_abi_cdecl,
@ -1276,8 +1276,8 @@ enum foreign_abi {
} }
// Foreign mods can be named or anonymous // Foreign mods can be named or anonymous
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum foreign_mod_sort { named, anonymous } enum foreign_mod_sort { named, anonymous }
impl foreign_mod_sort : cmp::Eq { impl foreign_mod_sort : cmp::Eq {
@ -1301,48 +1301,48 @@ impl foreign_abi : cmp::Eq {
pure fn ne(other: &foreign_abi) -> bool { !self.eq(other) } pure fn ne(other: &foreign_abi) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type foreign_mod = type foreign_mod =
{sort: foreign_mod_sort, {sort: foreign_mod_sort,
view_items: ~[@view_item], view_items: ~[@view_item],
items: ~[@foreign_item]}; items: ~[@foreign_item]};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type variant_arg = {ty: @ty, id: node_id}; type variant_arg = {ty: @ty, id: node_id};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum variant_kind { enum variant_kind {
tuple_variant_kind(~[variant_arg]), tuple_variant_kind(~[variant_arg]),
struct_variant_kind(@struct_def), struct_variant_kind(@struct_def),
enum_variant_kind(enum_def) enum_variant_kind(enum_def)
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type enum_def_ = { variants: ~[variant], common: Option<@struct_def> }; type enum_def_ = { variants: ~[variant], common: Option<@struct_def> };
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum enum_def = enum_def_; enum enum_def = enum_def_;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type variant_ = {name: ident, attrs: ~[attribute], kind: variant_kind, type variant_ = {name: ident, attrs: ~[attribute], kind: variant_kind,
id: node_id, disr_expr: Option<@expr>, vis: visibility}; id: node_id, disr_expr: Option<@expr>, vis: visibility};
type variant = spanned<variant_>; type variant = spanned<variant_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type path_list_ident_ = {name: ident, id: node_id}; type path_list_ident_ = {name: ident, id: node_id};
type path_list_ident = spanned<path_list_ident_>; type path_list_ident = spanned<path_list_ident_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum namespace { module_ns, type_value_ns } enum namespace { module_ns, type_value_ns }
impl namespace : cmp::Eq { impl namespace : cmp::Eq {
@ -1354,8 +1354,8 @@ impl namespace : cmp::Eq {
type view_path = spanned<view_path_>; type view_path = spanned<view_path_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum view_path_ { enum view_path_ {
// quux = foo::bar::baz // quux = foo::bar::baz
@ -1372,13 +1372,13 @@ enum view_path_ {
view_path_list(@path, ~[path_list_ident], node_id) view_path_list(@path, ~[path_list_ident], node_id)
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type view_item = {node: view_item_, attrs: ~[attribute], type view_item = {node: view_item_, attrs: ~[attribute],
vis: visibility, span: span}; vis: visibility, span: span};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum view_item_ { enum view_item_ {
view_item_use(ident, ~[@meta_item], node_id), view_item_use(ident, ~[@meta_item], node_id),
view_item_import(~[@view_path]), view_item_import(~[@view_path]),
@ -1391,8 +1391,8 @@ type attribute = spanned<attribute_>;
// Distinguishes between attributes that decorate items and attributes that // Distinguishes between attributes that decorate items and attributes that
// are contained as statements within items. These two cases need to be // are contained as statements within items. These two cases need to be
// distinguished for pretty-printing. // distinguished for pretty-printing.
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum attr_style { attr_outer, attr_inner, } enum attr_style { attr_outer, attr_inner, }
impl attr_style : cmp::Eq { impl attr_style : cmp::Eq {
@ -1403,8 +1403,8 @@ impl attr_style : cmp::Eq {
} }
// doc-comments are promoted to attributes that have is_sugared_doc = true // doc-comments are promoted to attributes that have is_sugared_doc = true
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool}; type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool};
/* /*
@ -1416,12 +1416,12 @@ type attribute_ = {style: attr_style, value: meta_item, is_sugared_doc: bool};
conceptually, the impl_id stands in for the pair of (this class, this conceptually, the impl_id stands in for the pair of (this class, this
trait) trait)
*/ */
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type trait_ref = {path: @path, ref_id: node_id, impl_id: node_id}; type trait_ref = {path: @path, ref_id: node_id, impl_id: node_id};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum visibility { public, private, inherited } enum visibility { public, private, inherited }
impl visibility : cmp::Eq { impl visibility : cmp::Eq {
@ -1438,8 +1438,8 @@ impl visibility : cmp::Eq {
pure fn ne(other: &visibility) -> bool { !self.eq(other) } pure fn ne(other: &visibility) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type struct_field_ = { type struct_field_ = {
kind: struct_field_kind, kind: struct_field_kind,
id: node_id, id: node_id,
@ -1448,15 +1448,15 @@ type struct_field_ = {
type struct_field = spanned<struct_field_>; type struct_field = spanned<struct_field_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum struct_field_kind { enum struct_field_kind {
named_field(ident, class_mutability, visibility), named_field(ident, class_mutability, visibility),
unnamed_field // element of a tuple-like struct unnamed_field // element of a tuple-like struct
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type struct_def = { type struct_def = {
traits: ~[@trait_ref], /* traits this struct implements */ traits: ~[@trait_ref], /* traits this struct implements */
fields: ~[@struct_field], /* fields */ fields: ~[@struct_field], /* fields */
@ -1472,14 +1472,14 @@ type struct_def = {
FIXME (#3300): Should allow items to be anonymous. Right now FIXME (#3300): Should allow items to be anonymous. Right now
we just use dummy names for anon items. we just use dummy names for anon items.
*/ */
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type item = {ident: ident, attrs: ~[attribute], type item = {ident: ident, attrs: ~[attribute],
id: node_id, node: item_, id: node_id, node: item_,
vis: visibility, span: span}; vis: visibility, span: span};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum item_ { enum item_ {
item_const(@ty, @expr), item_const(@ty, @expr),
item_fn(fn_decl, purity, ~[ty_param], blk), item_fn(fn_decl, purity, ~[ty_param], blk),
@ -1496,8 +1496,8 @@ enum item_ {
item_mac(mac), item_mac(mac),
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum class_mutability { class_mutable, class_immutable } enum class_mutability { class_mutable, class_immutable }
impl class_mutability : to_bytes::IterBytes { impl class_mutability : to_bytes::IterBytes {
@ -1520,8 +1520,8 @@ impl class_mutability : cmp::Eq {
type class_ctor = spanned<class_ctor_>; type class_ctor = spanned<class_ctor_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type class_ctor_ = {id: node_id, type class_ctor_ = {id: node_id,
attrs: ~[attribute], attrs: ~[attribute],
self_id: node_id, self_id: node_id,
@ -1530,15 +1530,15 @@ type class_ctor_ = {id: node_id,
type class_dtor = spanned<class_dtor_>; type class_dtor = spanned<class_dtor_>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type class_dtor_ = {id: node_id, type class_dtor_ = {id: node_id,
attrs: ~[attribute], attrs: ~[attribute],
self_id: node_id, self_id: node_id,
body: blk}; body: blk};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type foreign_item = type foreign_item =
{ident: ident, {ident: ident,
attrs: ~[attribute], attrs: ~[attribute],
@ -1547,8 +1547,8 @@ type foreign_item =
span: span, span: span,
vis: visibility}; vis: visibility};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum foreign_item_ { enum foreign_item_ {
foreign_item_fn(fn_decl, purity, ~[ty_param]), foreign_item_fn(fn_decl, purity, ~[ty_param]),
foreign_item_const(@ty) foreign_item_const(@ty)
@ -1557,8 +1557,8 @@ enum foreign_item_ {
// The data we save and restore about an inlined item or method. This is not // The data we save and restore about an inlined item or method. This is not
// part of the AST that we parse from a file, but it becomes part of the tree // part of the AST that we parse from a file, but it becomes part of the tree
// that we trans. // that we trans.
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum inlined_item { enum inlined_item {
ii_item(@item), ii_item(@item),
ii_method(def_id /* impl id */, @method), ii_method(def_id /* impl id */, @method),

View file

@ -406,8 +406,8 @@ fn dtor_dec() -> fn_decl {
// ______________________________________________________________________ // ______________________________________________________________________
// Enumerating the IDs which appear in an AST // Enumerating the IDs which appear in an AST
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type id_range = {min: node_id, max: node_id}; type id_range = {min: node_id, max: node_id};
fn empty(range: id_range) -> bool { fn empty(range: id_range) -> bool {

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -54,7 +54,7 @@ enum syntax_extension {
// macro_defining() is obsolete, remove when #old_macros go away. // macro_defining() is obsolete, remove when #old_macros go away.
macro_defining(macro_definer), macro_defining(macro_definer),
// #[auto_serialize2] and such. will probably survive death of #old_macros // #[auto_serialize] and such. will probably survive death of #old_macros
item_decorator(item_decorator), item_decorator(item_decorator),
// Token-tree expanders // Token-tree expanders
@ -64,67 +64,6 @@ enum syntax_extension {
// A temporary hard-coded map of methods for expanding syntax extension // A temporary hard-coded map of methods for expanding syntax extension
// AST nodes into full ASTs // AST nodes into full ASTs
#[cfg(stage0)]
fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
fn builtin(f: syntax_expander_) -> syntax_extension
{normal({expander: f, span: None})}
fn builtin_expr_tt(f: syntax_expander_tt_) -> syntax_extension {
expr_tt({expander: f, span: None})
}
fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension {
item_tt({expander: f, span: None})
}
let syntax_expanders = HashMap();
syntax_expanders.insert(~"macro",
macro_defining(ext::simplext::add_new_extension));
syntax_expanders.insert(~"macro_rules",
builtin_item_tt(
ext::tt::macro_rules::add_new_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_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));
syntax_expanders.insert(~"ident_to_str",
builtin(ext::ident_to_str::expand_syntax_ext));
syntax_expanders.insert(~"log_syntax",
builtin_expr_tt(
ext::log_syntax::expand_syntax_ext));
syntax_expanders.insert(~"ast",
builtin(ext::qquote::expand_ast));
syntax_expanders.insert(~"line",
builtin(ext::source_util::expand_line));
syntax_expanders.insert(~"col",
builtin(ext::source_util::expand_col));
syntax_expanders.insert(~"file",
builtin(ext::source_util::expand_file));
syntax_expanders.insert(~"stringify",
builtin(ext::source_util::expand_stringify));
syntax_expanders.insert(~"include",
builtin(ext::source_util::expand_include));
syntax_expanders.insert(~"include_str",
builtin(ext::source_util::expand_include_str));
syntax_expanders.insert(~"include_bin",
builtin(ext::source_util::expand_include_bin));
syntax_expanders.insert(~"module_path",
builtin(ext::source_util::expand_mod));
syntax_expanders.insert(~"proto",
builtin_item_tt(ext::pipes::expand_proto));
syntax_expanders.insert(
~"trace_macros",
builtin_expr_tt(ext::trace_macros::expand_trace_macros));
return syntax_expanders;
}
#[cfg(stage1)]
#[cfg(stage2)]
fn syntax_expander_table() -> HashMap<~str, syntax_extension> { fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
fn builtin(f: syntax_expander_) -> syntax_extension fn builtin(f: syntax_expander_) -> syntax_extension
{normal({expander: f, span: None})} {normal({expander: f, span: None})}
@ -143,16 +82,10 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext)); syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext));
syntax_expanders.insert( syntax_expanders.insert(
~"auto_serialize", ~"auto_serialize",
item_decorator(ext::auto_serialize2::expand_auto_serialize)); item_decorator(ext::auto_serialize::expand_auto_serialize));
syntax_expanders.insert( syntax_expanders.insert(
~"auto_deserialize", ~"auto_deserialize",
item_decorator(ext::auto_serialize2::expand_auto_deserialize)); item_decorator(ext::auto_serialize::expand_auto_deserialize));
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(~"env", builtin(ext::env::expand_syntax_ext));
syntax_expanders.insert(~"concat_idents", syntax_expanders.insert(~"concat_idents",
builtin(ext::concat_idents::expand_syntax_ext)); builtin(ext::concat_idents::expand_syntax_ext));

View file

@ -124,7 +124,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
} }
// This is a secondary mechanism for invoking syntax extensions on items: // This is a secondary mechanism for invoking syntax extensions on items:
// "decorator" attributes, such as #[auto_serialize2]. These are invoked by an // "decorator" attributes, such as #[auto_serialize]. These are invoked by an
// attribute prefixing an item, and are interpreted by feeding the item // attribute prefixing an item, and are interpreted by feeding the item
// through the named attribute _as a syntax extension_ and splicing in the // through the named attribute _as a syntax extension_ and splicing in the
// resulting item vec into place in favour of the decorator. Note that // resulting item vec into place in favour of the decorator. Note that

View file

@ -2,8 +2,8 @@ use util::interner;
use util::interner::interner; use util::interner::interner;
use std::map::HashMap; use std::map::HashMap;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum binop { enum binop {
PLUS, PLUS,
MINUS, MINUS,
@ -17,8 +17,8 @@ enum binop {
SHR, SHR,
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum token { enum token {
/* Expression-operator symbols. */ /* Expression-operator symbols. */
EQ, EQ,
@ -75,8 +75,8 @@ enum token {
EOF, EOF,
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
/// For interpolation during macro expansion. /// For interpolation during macro expansion.
enum nonterminal { enum nonterminal {
nt_item(@ast::item), nt_item(@ast::item),

View file

@ -128,12 +128,9 @@ mod ext {
mod ident_to_str; mod ident_to_str;
#[legacy_exports] #[legacy_exports]
mod log_syntax; mod log_syntax;
#[cfg(stage0)]
#[legacy_exports] #[legacy_exports]
mod auto_serialize; mod auto_serialize;
#[legacy_exports] #[legacy_exports]
mod auto_serialize2;
#[legacy_exports]
mod source_util; mod source_util;
mod pipes { mod pipes {

View file

@ -1,8 +1,9 @@
// Decoding metadata from a single crate's metadata // Decoding metadata from a single crate's metadata
use std::{ebml2, map}; use std::ebml;
use std::map;
use std::map::HashMap; use std::map::HashMap;
use std::serialization2::deserialize; use std::serialization::deserialize;
use io::WriterUtil; use io::WriterUtil;
use dvec::DVec; use dvec::DVec;
use syntax::{ast, ast_util}; use syntax::{ast, ast_util};
@ -64,25 +65,25 @@ export translate_def_id;
// what crate that's in and give us a def_id that makes sense for the current // what crate that's in and give us a def_id that makes sense for the current
// build. // build.
fn lookup_hash(d: ebml2::Doc, eq_fn: fn(x:&[u8]) -> bool, hash: uint) -> fn lookup_hash(d: ebml::Doc, eq_fn: fn(x:&[u8]) -> bool, hash: uint) ->
Option<ebml2::Doc> { Option<ebml::Doc> {
let index = ebml2::get_doc(d, tag_index); let index = ebml::get_doc(d, tag_index);
let table = ebml2::get_doc(index, tag_index_table); let table = ebml::get_doc(index, tag_index_table);
let hash_pos = table.start + hash % 256u * 4u; let hash_pos = table.start + hash % 256u * 4u;
let pos = io::u64_from_be_bytes(*d.data, hash_pos, 4u) as uint; let pos = io::u64_from_be_bytes(*d.data, hash_pos, 4u) as uint;
let tagged_doc = ebml2::doc_at(d.data, pos); let tagged_doc = ebml::doc_at(d.data, pos);
let belt = tag_index_buckets_bucket_elt; let belt = tag_index_buckets_bucket_elt;
for ebml2::tagged_docs(tagged_doc.doc, belt) |elt| { for ebml::tagged_docs(tagged_doc.doc, belt) |elt| {
let pos = io::u64_from_be_bytes(*elt.data, elt.start, 4u) as uint; let pos = io::u64_from_be_bytes(*elt.data, elt.start, 4u) as uint;
if eq_fn(vec::view(*elt.data, elt.start + 4u, elt.end)) { if eq_fn(vec::view(*elt.data, elt.start + 4u, elt.end)) {
return Some(ebml2::doc_at(d.data, pos).doc); return Some(ebml::doc_at(d.data, pos).doc);
} }
}; };
None None
} }
fn maybe_find_item(item_id: int, items: ebml2::Doc) -> Option<ebml2::Doc> { fn maybe_find_item(item_id: int, items: ebml::Doc) -> Option<ebml::Doc> {
fn eq_item(bytes: &[u8], item_id: int) -> bool { fn eq_item(bytes: &[u8], item_id: int) -> bool {
return io::u64_from_be_bytes(vec::view(bytes, 0u, 4u), 0u, 4u) as int return io::u64_from_be_bytes(vec::view(bytes, 0u, 4u), 0u, 4u) as int
== item_id; == item_id;
@ -92,14 +93,14 @@ fn maybe_find_item(item_id: int, items: ebml2::Doc) -> Option<ebml2::Doc> {
item_id.hash() as uint) item_id.hash() as uint)
} }
fn find_item(item_id: int, items: ebml2::Doc) -> ebml2::Doc { fn find_item(item_id: int, items: ebml::Doc) -> ebml::Doc {
return maybe_find_item(item_id, items).get(); return maybe_find_item(item_id, items).get();
} }
// Looks up an item in the given metadata and returns an ebml2 doc pointing // Looks up an item in the given metadata and returns an ebml doc pointing
// to the item data. // to the item data.
fn lookup_item(item_id: int, data: @~[u8]) -> ebml2::Doc { fn lookup_item(item_id: int, data: @~[u8]) -> ebml::Doc {
let items = ebml2::get_doc(ebml2::Doc(data), tag_items); let items = ebml::get_doc(ebml::Doc(data), tag_items);
match maybe_find_item(item_id, items) { match maybe_find_item(item_id, items) {
None => fail(fmt!("lookup_item: id not found: %d", item_id)), None => fail(fmt!("lookup_item: id not found: %d", item_id)),
Some(d) => d Some(d) => d
@ -137,9 +138,9 @@ impl Family : cmp::Eq {
pure fn ne(other: &Family) -> bool { !self.eq(other) } pure fn ne(other: &Family) -> bool { !self.eq(other) }
} }
fn item_family(item: ebml2::Doc) -> Family { fn item_family(item: ebml::Doc) -> Family {
let fam = ebml2::get_doc(item, tag_items_data_item_family); let fam = ebml::get_doc(item, tag_items_data_item_family);
match ebml2::doc_as_u8(fam) as char { match ebml::doc_as_u8(fam) as char {
'c' => Const, 'c' => Const,
'f' => Fn, 'f' => Fn,
'u' => UnsafeFn, 'u' => UnsafeFn,
@ -165,59 +166,59 @@ fn item_family(item: ebml2::Doc) -> Family {
} }
} }
fn item_symbol(item: ebml2::Doc) -> ~str { fn item_symbol(item: ebml::Doc) -> ~str {
let sym = ebml2::get_doc(item, tag_items_data_item_symbol); let sym = ebml::get_doc(item, tag_items_data_item_symbol);
return str::from_bytes(ebml2::doc_data(sym)); return str::from_bytes(ebml::doc_data(sym));
} }
fn item_parent_item(d: ebml2::Doc) -> Option<ast::def_id> { fn item_parent_item(d: ebml::Doc) -> Option<ast::def_id> {
for ebml2::tagged_docs(d, tag_items_data_parent_item) |did| { for ebml::tagged_docs(d, tag_items_data_parent_item) |did| {
return Some(ebml2::with_doc_data(did, |d| parse_def_id(d))); return Some(ebml::with_doc_data(did, |d| parse_def_id(d)));
} }
None None
} }
fn item_def_id(d: ebml2::Doc, cdata: cmd) -> ast::def_id { fn item_def_id(d: ebml::Doc, cdata: cmd) -> ast::def_id {
let tagdoc = ebml2::get_doc(d, tag_def_id); let tagdoc = ebml::get_doc(d, tag_def_id);
return translate_def_id(cdata, ebml2::with_doc_data(tagdoc, return translate_def_id(cdata, ebml::with_doc_data(tagdoc,
|d| parse_def_id(d))); |d| parse_def_id(d)));
} }
fn each_reexport(d: ebml2::Doc, f: fn(ebml2::Doc) -> bool) { fn each_reexport(d: ebml::Doc, f: fn(ebml::Doc) -> bool) {
for ebml2::tagged_docs(d, tag_items_data_item_reexport) |reexport_doc| { for ebml::tagged_docs(d, tag_items_data_item_reexport) |reexport_doc| {
if !f(reexport_doc) { if !f(reexport_doc) {
return; return;
} }
} }
} }
fn field_mutability(d: ebml2::Doc) -> ast::class_mutability { fn field_mutability(d: ebml::Doc) -> ast::class_mutability {
// Use maybe_get_doc in case it's a method // Use maybe_get_doc in case it's a method
option::map_default( option::map_default(
&ebml2::maybe_get_doc(d, tag_class_mut), &ebml::maybe_get_doc(d, tag_class_mut),
ast::class_immutable, ast::class_immutable,
|d| { |d| {
match ebml2::doc_as_u8(*d) as char { match ebml::doc_as_u8(*d) as char {
'm' => ast::class_mutable, 'm' => ast::class_mutable,
_ => ast::class_immutable _ => ast::class_immutable
} }
}) })
} }
fn variant_disr_val(d: ebml2::Doc) -> Option<int> { fn variant_disr_val(d: ebml::Doc) -> Option<int> {
do option::chain(ebml2::maybe_get_doc(d, tag_disr_val)) |val_doc| { do option::chain(ebml::maybe_get_doc(d, tag_disr_val)) |val_doc| {
int::parse_bytes(ebml2::doc_data(val_doc), 10u) int::parse_bytes(ebml::doc_data(val_doc), 10u)
} }
} }
fn doc_type(doc: ebml2::Doc, tcx: ty::ctxt, cdata: cmd) -> ty::t { fn doc_type(doc: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ty::t {
let tp = ebml2::get_doc(doc, tag_items_data_item_type); let tp = ebml::get_doc(doc, tag_items_data_item_type);
parse_ty_data(tp.data, cdata.cnum, tp.start, tcx, |did| { parse_ty_data(tp.data, cdata.cnum, tp.start, tcx, |did| {
translate_def_id(cdata, did) translate_def_id(cdata, did)
}) })
} }
fn item_type(item_id: ast::def_id, item: ebml2::Doc, fn item_type(item_id: ast::def_id, item: ebml::Doc,
tcx: ty::ctxt, cdata: cmd) -> ty::t { tcx: ty::ctxt, cdata: cmd) -> ty::t {
let t = doc_type(item, tcx, cdata); let t = doc_type(item, tcx, cdata);
if family_names_type(item_family(item)) { if family_names_type(item_family(item)) {
@ -225,18 +226,18 @@ fn item_type(item_id: ast::def_id, item: ebml2::Doc,
} else { t } } else { t }
} }
fn item_impl_traits(item: ebml2::Doc, tcx: ty::ctxt, cdata: cmd) -> ~[ty::t] { fn item_impl_traits(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd) -> ~[ty::t] {
let mut results = ~[]; let mut results = ~[];
for ebml2::tagged_docs(item, tag_impl_trait) |ity| { for ebml::tagged_docs(item, tag_impl_trait) |ity| {
results.push(doc_type(ity, tcx, cdata)); results.push(doc_type(ity, tcx, cdata));
}; };
results results
} }
fn item_ty_param_bounds(item: ebml2::Doc, tcx: ty::ctxt, cdata: cmd) fn item_ty_param_bounds(item: ebml::Doc, tcx: ty::ctxt, cdata: cmd)
-> @~[ty::param_bounds] { -> @~[ty::param_bounds] {
let mut bounds = ~[]; let mut bounds = ~[];
for ebml2::tagged_docs(item, tag_items_data_item_ty_param_bounds) |p| { for ebml::tagged_docs(item, tag_items_data_item_ty_param_bounds) |p| {
let bd = parse_bounds_data(p.data, p.start, cdata.cnum, tcx, |did| { let bd = parse_bounds_data(p.data, p.start, cdata.cnum, tcx, |did| {
translate_def_id(cdata, did) translate_def_id(cdata, did)
}); });
@ -245,42 +246,42 @@ fn item_ty_param_bounds(item: ebml2::Doc, tcx: ty::ctxt, cdata: cmd)
@bounds @bounds
} }
fn item_ty_region_param(item: ebml2::Doc) -> Option<ty::region_variance> { fn item_ty_region_param(item: ebml::Doc) -> Option<ty::region_variance> {
ebml2::maybe_get_doc(item, tag_region_param).map(|doc| { ebml::maybe_get_doc(item, tag_region_param).map(|doc| {
deserialize(&ebml2::Deserializer(*doc)) deserialize(&ebml::Deserializer(*doc))
}) })
} }
fn item_ty_param_count(item: ebml2::Doc) -> uint { fn item_ty_param_count(item: ebml::Doc) -> uint {
let mut n = 0u; let mut n = 0u;
ebml2::tagged_docs(item, tag_items_data_item_ty_param_bounds, ebml::tagged_docs(item, tag_items_data_item_ty_param_bounds,
|_p| { n += 1u; true } ); |_p| { n += 1u; true } );
n n
} }
fn enum_variant_ids(item: ebml2::Doc, cdata: cmd) -> ~[ast::def_id] { fn enum_variant_ids(item: ebml::Doc, cdata: cmd) -> ~[ast::def_id] {
let mut ids: ~[ast::def_id] = ~[]; let mut ids: ~[ast::def_id] = ~[];
let v = tag_items_data_item_variant; let v = tag_items_data_item_variant;
for ebml2::tagged_docs(item, v) |p| { for ebml::tagged_docs(item, v) |p| {
let ext = ebml2::with_doc_data(p, |d| parse_def_id(d)); let ext = ebml::with_doc_data(p, |d| parse_def_id(d));
ids.push({crate: cdata.cnum, node: ext.node}); ids.push({crate: cdata.cnum, node: ext.node});
}; };
return ids; return ids;
} }
fn item_path(intr: @ident_interner, item_doc: ebml2::Doc) -> ast_map::path { fn item_path(intr: @ident_interner, item_doc: ebml::Doc) -> ast_map::path {
let path_doc = ebml2::get_doc(item_doc, tag_path); let path_doc = ebml::get_doc(item_doc, tag_path);
let len_doc = ebml2::get_doc(path_doc, tag_path_len); let len_doc = ebml::get_doc(path_doc, tag_path_len);
let len = ebml2::doc_as_u32(len_doc) as uint; let len = ebml::doc_as_u32(len_doc) as uint;
let mut result = vec::with_capacity(len); let mut result = vec::with_capacity(len);
for ebml2::docs(path_doc) |tag, elt_doc| { for ebml::docs(path_doc) |tag, elt_doc| {
if tag == tag_path_elt_mod { if tag == tag_path_elt_mod {
let str = ebml2::doc_as_str(elt_doc); let str = ebml::doc_as_str(elt_doc);
result.push(ast_map::path_mod(intr.intern(@str))); result.push(ast_map::path_mod(intr.intern(@str)));
} else if tag == tag_path_elt_name { } else if tag == tag_path_elt_name {
let str = ebml2::doc_as_str(elt_doc); let str = ebml::doc_as_str(elt_doc);
result.push(ast_map::path_name(intr.intern(@str))); result.push(ast_map::path_name(intr.intern(@str)));
} else { } else {
// ignore tag_path_len element // ignore tag_path_len element
@ -290,12 +291,12 @@ fn item_path(intr: @ident_interner, item_doc: ebml2::Doc) -> ast_map::path {
return result; return result;
} }
fn item_name(intr: @ident_interner, item: ebml2::Doc) -> ast::ident { fn item_name(intr: @ident_interner, item: ebml::Doc) -> ast::ident {
let name = ebml2::get_doc(item, tag_paths_data_name); let name = ebml::get_doc(item, tag_paths_data_name);
intr.intern(@str::from_bytes(ebml2::doc_data(name))) intr.intern(@str::from_bytes(ebml::doc_data(name)))
} }
fn item_to_def_like(item: ebml2::Doc, did: ast::def_id, cnum: ast::crate_num) fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::crate_num)
-> def_like { -> def_like {
let fam = item_family(item); let fam = item_family(item);
match fam { match fam {
@ -367,10 +368,10 @@ fn get_impl_traits(cdata: cmd, id: ast::node_id, tcx: ty::ctxt) -> ~[ty::t] {
fn get_impl_method(intr: @ident_interner, cdata: cmd, id: ast::node_id, fn get_impl_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,
name: ast::ident) -> ast::def_id { name: ast::ident) -> ast::def_id {
let items = ebml2::get_doc(ebml2::Doc(cdata.data), tag_items); let items = ebml::get_doc(ebml::Doc(cdata.data), tag_items);
let mut found = None; let mut found = None;
for ebml2::tagged_docs(find_item(id, items), tag_item_impl_method) |mid| { for ebml::tagged_docs(find_item(id, items), tag_item_impl_method) |mid| {
let m_did = ebml2::with_doc_data(mid, |d| parse_def_id(d)); let m_did = ebml::with_doc_data(mid, |d| parse_def_id(d));
if item_name(intr, find_item(m_did.node, items)) == name { if item_name(intr, find_item(m_did.node, items)) == name {
found = Some(translate_def_id(cdata, m_did)); found = Some(translate_def_id(cdata, m_did));
} }
@ -380,14 +381,14 @@ fn get_impl_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,
fn get_class_method(intr: @ident_interner, cdata: cmd, id: ast::node_id, fn get_class_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,
name: ast::ident) -> ast::def_id { name: ast::ident) -> ast::def_id {
let items = ebml2::get_doc(ebml2::Doc(cdata.data), tag_items); let items = ebml::get_doc(ebml::Doc(cdata.data), tag_items);
let mut found = None; let mut found = None;
let cls_items = match maybe_find_item(id, items) { let cls_items = match maybe_find_item(id, items) {
Some(it) => it, Some(it) => it,
None => fail (fmt!("get_class_method: class id not found \ None => fail (fmt!("get_class_method: class id not found \
when looking up method %s", *intr.get(name))) when looking up method %s", *intr.get(name)))
}; };
for ebml2::tagged_docs(cls_items, tag_item_trait_method) |mid| { for ebml::tagged_docs(cls_items, tag_item_trait_method) |mid| {
let m_did = item_def_id(mid, cdata); let m_did = item_def_id(mid, cdata);
if item_name(intr, mid) == name { if item_name(intr, mid) == name {
found = Some(m_did); found = Some(m_did);
@ -401,16 +402,16 @@ fn get_class_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,
} }
fn class_dtor(cdata: cmd, id: ast::node_id) -> Option<ast::def_id> { fn class_dtor(cdata: cmd, id: ast::node_id) -> Option<ast::def_id> {
let items = ebml2::get_doc(ebml2::Doc(cdata.data), tag_items); let items = ebml::get_doc(ebml::Doc(cdata.data), tag_items);
let mut found = None; let mut found = None;
let cls_items = match maybe_find_item(id, items) { let cls_items = match maybe_find_item(id, items) {
Some(it) => it, Some(it) => it,
None => fail (fmt!("class_dtor: class id not found \ None => fail (fmt!("class_dtor: class id not found \
when looking up dtor for %d", id)) when looking up dtor for %d", id))
}; };
for ebml2::tagged_docs(cls_items, tag_item_dtor) |doc| { for ebml::tagged_docs(cls_items, tag_item_dtor) |doc| {
let doc1 = ebml2::get_doc(doc, tag_def_id); let doc1 = ebml::get_doc(doc, tag_def_id);
let did = ebml2::with_doc_data(doc1, |d| parse_def_id(d)); let did = ebml::with_doc_data(doc1, |d| parse_def_id(d));
found = Some(translate_def_id(cdata, did)); found = Some(translate_def_id(cdata, did));
}; };
found found
@ -452,14 +453,14 @@ fn path_entry(path_string: ~str, def_like: def_like) -> path_entry {
/// Iterates over all the paths in the given crate. /// Iterates over all the paths in the given crate.
fn each_path(intr: @ident_interner, cdata: cmd, f: fn(path_entry) -> bool) { fn each_path(intr: @ident_interner, cdata: cmd, f: fn(path_entry) -> bool) {
let root = ebml2::Doc(cdata.data); let root = ebml::Doc(cdata.data);
let items = ebml2::get_doc(root, tag_items); let items = ebml::get_doc(root, tag_items);
let items_data = ebml2::get_doc(items, tag_items_data); let items_data = ebml::get_doc(items, tag_items_data);
let mut broken = false; let mut broken = false;
// First, go through all the explicit items. // First, go through all the explicit items.
for ebml2::tagged_docs(items_data, tag_items_data_item) |item_doc| { for ebml::tagged_docs(items_data, tag_items_data_item) |item_doc| {
if !broken { if !broken {
let path = ast_map::path_to_str_with_sep( let path = ast_map::path_to_str_with_sep(
item_path(intr, item_doc), ~"::", intr); item_path(intr, item_doc), ~"::", intr);
@ -482,16 +483,16 @@ fn each_path(intr: @ident_interner, cdata: cmd, f: fn(path_entry) -> bool) {
for each_reexport(item_doc) |reexport_doc| { for each_reexport(item_doc) |reexport_doc| {
if !broken { if !broken {
let def_id_doc = let def_id_doc =
ebml2::get_doc(reexport_doc, ebml::get_doc(reexport_doc,
tag_items_data_item_reexport_def_id); tag_items_data_item_reexport_def_id);
let def_id = let def_id =
ebml2::with_doc_data(def_id_doc, |d| parse_def_id(d)); ebml::with_doc_data(def_id_doc, |d| parse_def_id(d));
let def_id = translate_def_id(cdata, def_id); let def_id = translate_def_id(cdata, def_id);
let reexport_name_doc = let reexport_name_doc =
ebml2::get_doc(reexport_doc, ebml::get_doc(reexport_doc,
tag_items_data_item_reexport_name); tag_items_data_item_reexport_name);
let reexport_name = ebml2::doc_as_str(reexport_name_doc); let reexport_name = ebml::doc_as_str(reexport_name_doc);
let reexport_path; let reexport_path;
if path == ~"" { if path == ~"" {
@ -540,7 +541,7 @@ type decode_inlined_item = fn(
cdata: cstore::crate_metadata, cdata: cstore::crate_metadata,
tcx: ty::ctxt, tcx: ty::ctxt,
path: ast_map::path, path: ast_map::path,
par_doc: ebml2::Doc) -> Option<ast::inlined_item>; par_doc: ebml::Doc) -> Option<ast::inlined_item>;
fn maybe_get_item_ast(intr: @ident_interner, cdata: cmd, tcx: ty::ctxt, fn maybe_get_item_ast(intr: @ident_interner, cdata: cmd, tcx: ty::ctxt,
id: ast::node_id, id: ast::node_id,
@ -571,7 +572,7 @@ fn maybe_get_item_ast(intr: @ident_interner, cdata: cmd, tcx: ty::ctxt,
fn get_enum_variants(intr: @ident_interner, cdata: cmd, id: ast::node_id, fn get_enum_variants(intr: @ident_interner, cdata: cmd, id: ast::node_id,
tcx: ty::ctxt) -> ~[ty::variant_info] { tcx: ty::ctxt) -> ~[ty::variant_info] {
let data = cdata.data; let data = cdata.data;
let items = ebml2::get_doc(ebml2::Doc(data), tag_items); let items = ebml::get_doc(ebml::Doc(data), tag_items);
let item = find_item(id, items); let item = find_item(id, items);
let mut infos: ~[ty::variant_info] = ~[]; let mut infos: ~[ty::variant_info] = ~[];
let variant_ids = enum_variant_ids(item, cdata); let variant_ids = enum_variant_ids(item, cdata);
@ -609,7 +610,7 @@ type method_info = {
type _impl = {did: ast::def_id, ident: ast::ident, methods: ~[@method_info]}; type _impl = {did: ast::def_id, ident: ast::ident, methods: ~[@method_info]};
fn get_self_ty(item: ebml2::Doc) -> ast::self_ty_ { fn get_self_ty(item: ebml::Doc) -> ast::self_ty_ {
fn get_mutability(ch: u8) -> ast::mutability { fn get_mutability(ch: u8) -> ast::mutability {
match ch as char { match ch as char {
'i' => { ast::m_imm } 'i' => { ast::m_imm }
@ -621,8 +622,8 @@ fn get_self_ty(item: ebml2::Doc) -> ast::self_ty_ {
} }
} }
let self_type_doc = ebml2::get_doc(item, tag_item_trait_method_self_ty); let self_type_doc = ebml::get_doc(item, tag_item_trait_method_self_ty);
let string = ebml2::doc_as_str(self_type_doc); let string = ebml::doc_as_str(self_type_doc);
let self_ty_kind = string[0]; let self_ty_kind = string[0];
match self_ty_kind as char { match self_ty_kind as char {
@ -638,11 +639,11 @@ fn get_self_ty(item: ebml2::Doc) -> ast::self_ty_ {
} }
} }
fn item_impl_methods(intr: @ident_interner, cdata: cmd, item: ebml2::Doc, fn item_impl_methods(intr: @ident_interner, cdata: cmd, item: ebml::Doc,
base_tps: uint) -> ~[@method_info] { base_tps: uint) -> ~[@method_info] {
let mut rslt = ~[]; let mut rslt = ~[];
for ebml2::tagged_docs(item, tag_item_impl_method) |doc| { for ebml::tagged_docs(item, tag_item_impl_method) |doc| {
let m_did = ebml2::with_doc_data(doc, |d| parse_def_id(d)); let m_did = ebml::with_doc_data(doc, |d| parse_def_id(d));
let mth_item = lookup_item(m_did.node, cdata.data); let mth_item = lookup_item(m_did.node, cdata.data);
let self_ty = get_self_ty(mth_item); let self_ty = get_self_ty(mth_item);
rslt.push(@{did: translate_def_id(cdata, m_did), rslt.push(@{did: translate_def_id(cdata, m_did),
@ -662,8 +663,8 @@ fn get_impls_for_mod(intr: @ident_interner, cdata: cmd,
let data = cdata.data; let data = cdata.data;
let mod_item = lookup_item(m_id, data); let mod_item = lookup_item(m_id, data);
let mut result = ~[]; let mut result = ~[];
for ebml2::tagged_docs(mod_item, tag_mod_impl) |doc| { for ebml::tagged_docs(mod_item, tag_mod_impl) |doc| {
let did = ebml2::with_doc_data(doc, |d| parse_def_id(d)); let did = ebml::with_doc_data(doc, |d| parse_def_id(d));
let local_did = translate_def_id(cdata, did); let local_did = translate_def_id(cdata, did);
debug!("(get impls for mod) getting did %? for '%?'", debug!("(get impls for mod) getting did %? for '%?'",
local_did, name); local_did, name);
@ -690,7 +691,7 @@ fn get_trait_methods(intr: @ident_interner, cdata: cmd, id: ast::node_id,
let data = cdata.data; let data = cdata.data;
let item = lookup_item(id, data); let item = lookup_item(id, data);
let mut result = ~[]; let mut result = ~[];
for ebml2::tagged_docs(item, tag_item_trait_method) |mth| { for ebml::tagged_docs(item, tag_item_trait_method) |mth| {
let bounds = item_ty_param_bounds(mth, tcx, cdata); let bounds = item_ty_param_bounds(mth, tcx, cdata);
let name = item_name(intr, mth); let name = item_name(intr, mth);
let ty = doc_type(mth, tcx, cdata); let ty = doc_type(mth, tcx, cdata);
@ -722,7 +723,7 @@ fn get_method_names_if_trait(intr: @ident_interner, cdata: cmd,
} }
let resulting_methods = @DVec(); let resulting_methods = @DVec();
for ebml2::tagged_docs(item, tag_item_trait_method) |method| { for ebml::tagged_docs(item, tag_item_trait_method) |method| {
resulting_methods.push( resulting_methods.push(
(item_name(intr, method), get_self_ty(method))); (item_name(intr, method), get_self_ty(method)));
} }
@ -734,8 +735,8 @@ fn get_item_attrs(cdata: cmd,
f: fn(~[@ast::meta_item])) { f: fn(~[@ast::meta_item])) {
let item = lookup_item(node_id, cdata.data); let item = lookup_item(node_id, cdata.data);
for ebml2::tagged_docs(item, tag_attributes) |attributes| { for ebml::tagged_docs(item, tag_attributes) |attributes| {
for ebml2::tagged_docs(attributes, tag_attribute) |attribute| { for ebml::tagged_docs(attributes, tag_attribute) |attribute| {
f(get_meta_items(attribute)); f(get_meta_items(attribute));
} }
} }
@ -747,7 +748,7 @@ fn get_class_members(intr: @ident_interner, cdata: cmd, id: ast::node_id,
let data = cdata.data; let data = cdata.data;
let item = lookup_item(id, data); let item = lookup_item(id, data);
let mut result = ~[]; let mut result = ~[];
for ebml2::tagged_docs(item, tag_item_field) |an_item| { for ebml::tagged_docs(item, tag_item_field) |an_item| {
let f = item_family(an_item); let f = item_family(an_item);
if p(f) { if p(f) {
let name = item_name(intr, an_item); let name = item_name(intr, an_item);
@ -787,15 +788,15 @@ fn family_names_type(fam: Family) -> bool {
match fam { Type | Mod | Trait => true, _ => false } match fam { Type | Mod | Trait => true, _ => false }
} }
fn read_path(d: ebml2::Doc) -> {path: ~str, pos: uint} { fn read_path(d: ebml::Doc) -> {path: ~str, pos: uint} {
let desc = ebml2::doc_data(d); let desc = ebml::doc_data(d);
let pos = io::u64_from_be_bytes(desc, 0u, 4u) as uint; let pos = io::u64_from_be_bytes(desc, 0u, 4u) as uint;
let pathbytes = vec::slice::<u8>(desc, 4u, vec::len::<u8>(desc)); let pathbytes = vec::slice::<u8>(desc, 4u, vec::len::<u8>(desc));
let path = str::from_bytes(pathbytes); let path = str::from_bytes(pathbytes);
return {path: path, pos: pos}; return {path: path, pos: pos};
} }
fn describe_def(items: ebml2::Doc, id: ast::def_id) -> ~str { fn describe_def(items: ebml::Doc, id: ast::def_id) -> ~str {
if id.crate != ast::local_crate { return ~"external"; } if id.crate != ast::local_crate { return ~"external"; }
let it = match maybe_find_item(id.node, items) { let it = match maybe_find_item(id.node, items) {
Some(it) => it, Some(it) => it,
@ -830,36 +831,36 @@ fn item_family_to_str(fam: Family) -> ~str {
} }
} }
fn get_meta_items(md: ebml2::Doc) -> ~[@ast::meta_item] { fn get_meta_items(md: ebml::Doc) -> ~[@ast::meta_item] {
let mut items: ~[@ast::meta_item] = ~[]; let mut items: ~[@ast::meta_item] = ~[];
for ebml2::tagged_docs(md, tag_meta_item_word) |meta_item_doc| { for ebml::tagged_docs(md, tag_meta_item_word) |meta_item_doc| {
let nd = ebml2::get_doc(meta_item_doc, tag_meta_item_name); let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
let n = str::from_bytes(ebml2::doc_data(nd)); let n = str::from_bytes(ebml::doc_data(nd));
items.push(attr::mk_word_item(n)); items.push(attr::mk_word_item(n));
}; };
for ebml2::tagged_docs(md, tag_meta_item_name_value) |meta_item_doc| { for ebml::tagged_docs(md, tag_meta_item_name_value) |meta_item_doc| {
let nd = ebml2::get_doc(meta_item_doc, tag_meta_item_name); let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
let vd = ebml2::get_doc(meta_item_doc, tag_meta_item_value); let vd = ebml::get_doc(meta_item_doc, tag_meta_item_value);
let n = str::from_bytes(ebml2::doc_data(nd)); let n = str::from_bytes(ebml::doc_data(nd));
let v = str::from_bytes(ebml2::doc_data(vd)); let v = str::from_bytes(ebml::doc_data(vd));
// FIXME (#623): Should be able to decode meta_name_value variants, // FIXME (#623): Should be able to decode meta_name_value variants,
// but currently the encoder just drops them // but currently the encoder just drops them
items.push(attr::mk_name_value_item_str(n, v)); items.push(attr::mk_name_value_item_str(n, v));
}; };
for ebml2::tagged_docs(md, tag_meta_item_list) |meta_item_doc| { for ebml::tagged_docs(md, tag_meta_item_list) |meta_item_doc| {
let nd = ebml2::get_doc(meta_item_doc, tag_meta_item_name); let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
let n = str::from_bytes(ebml2::doc_data(nd)); let n = str::from_bytes(ebml::doc_data(nd));
let subitems = get_meta_items(meta_item_doc); let subitems = get_meta_items(meta_item_doc);
items.push(attr::mk_list_item(n, subitems)); items.push(attr::mk_list_item(n, subitems));
}; };
return items; return items;
} }
fn get_attributes(md: ebml2::Doc) -> ~[ast::attribute] { fn get_attributes(md: ebml::Doc) -> ~[ast::attribute] {
let mut attrs: ~[ast::attribute] = ~[]; let mut attrs: ~[ast::attribute] = ~[];
match ebml2::maybe_get_doc(md, tag_attributes) { match ebml::maybe_get_doc(md, tag_attributes) {
option::Some(attrs_d) => { option::Some(attrs_d) => {
for ebml2::tagged_docs(attrs_d, tag_attribute) |attr_doc| { for ebml::tagged_docs(attrs_d, tag_attribute) |attr_doc| {
let meta_items = get_meta_items(attr_doc); let meta_items = get_meta_items(attr_doc);
// Currently it's only possible to have a single meta item on // Currently it's only possible to have a single meta item on
// an attribute // an attribute
@ -877,13 +878,13 @@ fn get_attributes(md: ebml2::Doc) -> ~[ast::attribute] {
} }
fn list_meta_items(intr: @ident_interner, fn list_meta_items(intr: @ident_interner,
meta_items: ebml2::Doc, out: io::Writer) { meta_items: ebml::Doc, out: io::Writer) {
for get_meta_items(meta_items).each |mi| { for get_meta_items(meta_items).each |mi| {
out.write_str(fmt!("%s\n", pprust::meta_item_to_str(*mi, intr))); out.write_str(fmt!("%s\n", pprust::meta_item_to_str(*mi, intr)));
} }
} }
fn list_crate_attributes(intr: @ident_interner, md: ebml2::Doc, hash: ~str, fn list_crate_attributes(intr: @ident_interner, md: ebml::Doc, hash: ~str,
out: io::Writer) { out: io::Writer) {
out.write_str(fmt!("=Crate Attributes (%s)=\n", hash)); out.write_str(fmt!("=Crate Attributes (%s)=\n", hash));
@ -895,7 +896,7 @@ fn list_crate_attributes(intr: @ident_interner, md: ebml2::Doc, hash: ~str,
} }
fn get_crate_attributes(data: @~[u8]) -> ~[ast::attribute] { fn get_crate_attributes(data: @~[u8]) -> ~[ast::attribute] {
return get_attributes(ebml2::Doc(data)); return get_attributes(ebml::Doc(data));
} }
type crate_dep = {cnum: ast::crate_num, name: ast::ident, type crate_dep = {cnum: ast::crate_num, name: ast::ident,
@ -903,13 +904,13 @@ type crate_dep = {cnum: ast::crate_num, name: ast::ident,
fn get_crate_deps(intr: @ident_interner, data: @~[u8]) -> ~[crate_dep] { fn get_crate_deps(intr: @ident_interner, data: @~[u8]) -> ~[crate_dep] {
let mut deps: ~[crate_dep] = ~[]; let mut deps: ~[crate_dep] = ~[];
let cratedoc = ebml2::Doc(data); let cratedoc = ebml::Doc(data);
let depsdoc = ebml2::get_doc(cratedoc, tag_crate_deps); let depsdoc = ebml::get_doc(cratedoc, tag_crate_deps);
let mut crate_num = 1; let mut crate_num = 1;
fn docstr(doc: ebml2::Doc, tag_: uint) -> ~str { fn docstr(doc: ebml::Doc, tag_: uint) -> ~str {
str::from_bytes(ebml2::doc_data(ebml2::get_doc(doc, tag_))) str::from_bytes(ebml::doc_data(ebml::get_doc(doc, tag_)))
} }
for ebml2::tagged_docs(depsdoc, tag_crate_dep) |depdoc| { for ebml::tagged_docs(depsdoc, tag_crate_dep) |depdoc| {
deps.push({cnum: crate_num, deps.push({cnum: crate_num,
name: intr.intern(@docstr(depdoc, tag_crate_dep_name)), name: intr.intern(@docstr(depdoc, tag_crate_dep_name)),
vers: docstr(depdoc, tag_crate_dep_vers), vers: docstr(depdoc, tag_crate_dep_vers),
@ -932,9 +933,9 @@ fn list_crate_deps(intr: @ident_interner, data: @~[u8], out: io::Writer) {
} }
fn get_crate_hash(data: @~[u8]) -> ~str { fn get_crate_hash(data: @~[u8]) -> ~str {
let cratedoc = ebml2::Doc(data); let cratedoc = ebml::Doc(data);
let hashdoc = ebml2::get_doc(cratedoc, tag_crate_hash); let hashdoc = ebml::get_doc(cratedoc, tag_crate_hash);
return str::from_bytes(ebml2::doc_data(hashdoc)); return str::from_bytes(ebml::doc_data(hashdoc));
} }
fn get_crate_vers(data: @~[u8]) -> ~str { fn get_crate_vers(data: @~[u8]) -> ~str {
@ -988,7 +989,7 @@ fn get_crate_module_paths(intr: @ident_interner, cdata: cmd)
fn list_crate_metadata(intr: @ident_interner, bytes: @~[u8], fn list_crate_metadata(intr: @ident_interner, bytes: @~[u8],
out: io::Writer) { out: io::Writer) {
let hash = get_crate_hash(bytes); let hash = get_crate_hash(bytes);
let md = ebml2::Doc(bytes); let md = ebml::Doc(bytes);
list_crate_attributes(intr, md, hash, out); list_crate_attributes(intr, md, hash, out);
list_crate_deps(intr, bytes, out); list_crate_deps(intr, bytes, out);
} }

View file

@ -2,10 +2,10 @@
use util::ppaux::ty_to_str; use util::ppaux::ty_to_str;
use std::{ebml2, map}; use std::{ebml, map};
use std::map::HashMap; use std::map::HashMap;
use io::WriterUtil; use io::WriterUtil;
use ebml2::Serializer; use ebml::Serializer;
use syntax::ast::*; use syntax::ast::*;
use syntax::print::pprust; use syntax::print::pprust;
use syntax::{ast_util, visit}; use syntax::{ast_util, visit};
@ -40,7 +40,7 @@ export encode_def_id;
type abbrev_map = map::HashMap<ty::t, tyencode::ty_abbrev>; type abbrev_map = map::HashMap<ty::t, tyencode::ty_abbrev>;
type encode_inlined_item = fn@(ecx: @encode_ctxt, type encode_inlined_item = fn@(ecx: @encode_ctxt,
ebml_w: ebml2::Serializer, ebml_w: ebml::Serializer,
path: ast_map::path, path: ast_map::path,
ii: ast::inlined_item); ii: ast::inlined_item);
@ -86,15 +86,15 @@ fn reachable(ecx: @encode_ctxt, id: node_id) -> bool {
ecx.reachable.contains_key(id) ecx.reachable.contains_key(id)
} }
fn encode_name(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, name: ident) { fn encode_name(ecx: @encode_ctxt, ebml_w: ebml::Serializer, name: ident) {
ebml_w.wr_tagged_str(tag_paths_data_name, ecx.tcx.sess.str_of(name)); ebml_w.wr_tagged_str(tag_paths_data_name, ecx.tcx.sess.str_of(name));
} }
fn encode_def_id(ebml_w: ebml2::Serializer, id: def_id) { fn encode_def_id(ebml_w: ebml::Serializer, id: def_id) {
ebml_w.wr_tagged_str(tag_def_id, def_to_str(id)); ebml_w.wr_tagged_str(tag_def_id, def_to_str(id));
} }
fn encode_region_param(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_region_param(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
it: @ast::item) { it: @ast::item) {
let opt_rp = ecx.tcx.region_paramd_items.find(it.id); let opt_rp = ecx.tcx.region_paramd_items.find(it.id);
for opt_rp.each |rp| { for opt_rp.each |rp| {
@ -104,7 +104,7 @@ fn encode_region_param(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
} }
} }
fn encode_mutability(ebml_w: ebml2::Serializer, mt: class_mutability) { fn encode_mutability(ebml_w: ebml::Serializer, mt: class_mutability) {
do ebml_w.wr_tag(tag_class_mut) { do ebml_w.wr_tag(tag_class_mut) {
let val = match mt { let val = match mt {
class_immutable => 'a', class_immutable => 'a',
@ -116,7 +116,7 @@ fn encode_mutability(ebml_w: ebml2::Serializer, mt: class_mutability) {
type entry<T> = {val: T, pos: uint}; type entry<T> = {val: T, pos: uint};
fn add_to_index(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, path: &[ident], fn add_to_index(ecx: @encode_ctxt, ebml_w: ebml::Serializer, path: &[ident],
index: &mut ~[entry<~str>], name: ident) { index: &mut ~[entry<~str>], name: ident) {
let mut full_path = ~[]; let mut full_path = ~[];
full_path.push_all(path); full_path.push_all(path);
@ -127,7 +127,7 @@ fn add_to_index(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, path: &[ident],
pos: ebml_w.writer.tell()}); pos: ebml_w.writer.tell()});
} }
fn encode_trait_ref(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, fn encode_trait_ref(ebml_w: ebml::Serializer, ecx: @encode_ctxt,
t: @trait_ref) { t: @trait_ref) {
ebml_w.start_tag(tag_impl_trait); ebml_w.start_tag(tag_impl_trait);
encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, t.ref_id)); encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, t.ref_id));
@ -136,7 +136,7 @@ fn encode_trait_ref(ebml_w: ebml2::Serializer, ecx: @encode_ctxt,
// Item info table encoding // Item info table encoding
fn encode_family(ebml_w: ebml2::Serializer, c: char) { fn encode_family(ebml_w: ebml::Serializer, c: char) {
ebml_w.start_tag(tag_items_data_item_family); ebml_w.start_tag(tag_items_data_item_family);
ebml_w.writer.write(&[c as u8]); ebml_w.writer.write(&[c as u8]);
ebml_w.end_tag(); ebml_w.end_tag();
@ -144,7 +144,7 @@ fn encode_family(ebml_w: ebml2::Serializer, c: char) {
fn def_to_str(did: def_id) -> ~str { fmt!("%d:%d", did.crate, did.node) } fn def_to_str(did: def_id) -> ~str { fmt!("%d:%d", did.crate, did.node) }
fn encode_ty_type_param_bounds(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, fn encode_ty_type_param_bounds(ebml_w: ebml::Serializer, ecx: @encode_ctxt,
params: @~[ty::param_bounds]) { params: @~[ty::param_bounds]) {
let ty_str_ctxt = @{diag: ecx.diag, let ty_str_ctxt = @{diag: ecx.diag,
ds: def_to_str, ds: def_to_str,
@ -158,7 +158,7 @@ fn encode_ty_type_param_bounds(ebml_w: ebml2::Serializer, ecx: @encode_ctxt,
} }
} }
fn encode_type_param_bounds(ebml_w: ebml2::Serializer, ecx: @encode_ctxt, fn encode_type_param_bounds(ebml_w: ebml::Serializer, ecx: @encode_ctxt,
params: ~[ty_param]) { params: ~[ty_param]) {
let ty_param_bounds = let ty_param_bounds =
@params.map(|param| ecx.tcx.ty_param_bounds.get(param.id)); @params.map(|param| ecx.tcx.ty_param_bounds.get(param.id));
@ -166,13 +166,13 @@ fn encode_type_param_bounds(ebml_w: ebml2::Serializer, ecx: @encode_ctxt,
} }
fn encode_variant_id(ebml_w: ebml2::Serializer, vid: def_id) { fn encode_variant_id(ebml_w: ebml::Serializer, vid: def_id) {
ebml_w.start_tag(tag_items_data_item_variant); ebml_w.start_tag(tag_items_data_item_variant);
ebml_w.writer.write(str::to_bytes(def_to_str(vid))); ebml_w.writer.write(str::to_bytes(def_to_str(vid)));
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn write_type(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, typ: ty::t) { fn write_type(ecx: @encode_ctxt, ebml_w: ebml::Serializer, typ: ty::t) {
let ty_str_ctxt = let ty_str_ctxt =
@{diag: ecx.diag, @{diag: ecx.diag,
ds: def_to_str, ds: def_to_str,
@ -182,7 +182,7 @@ fn write_type(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, typ: ty::t) {
tyencode::enc_ty(ebml_w.writer, ty_str_ctxt, typ); tyencode::enc_ty(ebml_w.writer, ty_str_ctxt, typ);
} }
fn write_vstore(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn write_vstore(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
vstore: ty::vstore) { vstore: ty::vstore) {
let ty_str_ctxt = let ty_str_ctxt =
@{diag: ecx.diag, @{diag: ecx.diag,
@ -193,13 +193,13 @@ fn write_vstore(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
tyencode::enc_vstore(ebml_w.writer, ty_str_ctxt, vstore); tyencode::enc_vstore(ebml_w.writer, ty_str_ctxt, vstore);
} }
fn encode_type(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, typ: ty::t) { fn encode_type(ecx: @encode_ctxt, ebml_w: ebml::Serializer, typ: ty::t) {
ebml_w.start_tag(tag_items_data_item_type); ebml_w.start_tag(tag_items_data_item_type);
write_type(ecx, ebml_w, typ); write_type(ecx, ebml_w, typ);
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_symbol(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, id: node_id) { fn encode_symbol(ecx: @encode_ctxt, ebml_w: ebml::Serializer, id: node_id) {
ebml_w.start_tag(tag_items_data_item_symbol); ebml_w.start_tag(tag_items_data_item_symbol);
let sym = match ecx.item_symbols.find(id) { let sym = match ecx.item_symbols.find(id) {
Some(x) => x, Some(x) => x,
@ -212,27 +212,27 @@ fn encode_symbol(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, id: node_id) {
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_discriminant(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_discriminant(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
id: node_id) { id: node_id) {
ebml_w.start_tag(tag_items_data_item_symbol); ebml_w.start_tag(tag_items_data_item_symbol);
ebml_w.writer.write(str::to_bytes(ecx.discrim_symbols.get(id))); ebml_w.writer.write(str::to_bytes(ecx.discrim_symbols.get(id)));
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_disr_val(_ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_disr_val(_ecx: @encode_ctxt, ebml_w: ebml::Serializer,
disr_val: int) { disr_val: int) {
ebml_w.start_tag(tag_disr_val); ebml_w.start_tag(tag_disr_val);
ebml_w.writer.write(str::to_bytes(int::to_str(disr_val,10u))); ebml_w.writer.write(str::to_bytes(int::to_str(disr_val,10u)));
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_parent_item(ebml_w: ebml2::Serializer, id: def_id) { fn encode_parent_item(ebml_w: ebml::Serializer, id: def_id) {
ebml_w.start_tag(tag_items_data_parent_item); ebml_w.start_tag(tag_items_data_parent_item);
ebml_w.writer.write(str::to_bytes(def_to_str(id))); ebml_w.writer.write(str::to_bytes(def_to_str(id)));
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
id: node_id, variants: ~[variant], id: node_id, variants: ~[variant],
path: ast_map::path, index: @mut ~[entry<int>], path: ast_map::path, index: @mut ~[entry<int>],
ty_params: ~[ty_param]) { ty_params: ~[ty_param]) {
@ -269,9 +269,9 @@ fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
} }
} }
fn encode_path(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_path(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
path: ast_map::path, name: ast_map::path_elt) { path: ast_map::path, name: ast_map::path_elt) {
fn encode_path_elt(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_path_elt(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
elt: ast_map::path_elt) { elt: ast_map::path_elt) {
let (tag, name) = match elt { let (tag, name) = match elt {
ast_map::path_mod(name) => (tag_path_elt_mod, name), ast_map::path_mod(name) => (tag_path_elt_mod, name),
@ -290,7 +290,7 @@ fn encode_path(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
} }
} }
fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, md: _mod, fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: ebml::Serializer, md: _mod,
id: node_id, path: ast_map::path, name: ident) { id: node_id, path: ast_map::path, name: ident) {
ebml_w.start_tag(tag_items_data_item); ebml_w.start_tag(tag_items_data_item);
encode_def_id(ebml_w, local_def(id)); encode_def_id(ebml_w, local_def(id));
@ -348,7 +348,7 @@ fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, md: _mod,
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_visibility(ebml_w: ebml2::Serializer, visibility: visibility) { fn encode_visibility(ebml_w: ebml::Serializer, visibility: visibility) {
encode_family(ebml_w, match visibility { encode_family(ebml_w, match visibility {
public => 'g', public => 'g',
private => 'j', private => 'j',
@ -356,7 +356,7 @@ fn encode_visibility(ebml_w: ebml2::Serializer, visibility: visibility) {
}); });
} }
fn encode_self_type(ebml_w: ebml2::Serializer, self_type: ast::self_ty_) { fn encode_self_type(ebml_w: ebml::Serializer, self_type: ast::self_ty_) {
ebml_w.start_tag(tag_item_trait_method_self_ty); ebml_w.start_tag(tag_item_trait_method_self_ty);
// Encode the base self type. // Encode the base self type.
@ -389,7 +389,7 @@ fn encode_self_type(ebml_w: ebml2::Serializer, self_type: ast::self_ty_) {
} }
/* Returns an index of items in this class */ /* Returns an index of items in this class */
fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
id: node_id, path: ast_map::path, id: node_id, path: ast_map::path,
class_tps: ~[ty_param], class_tps: ~[ty_param],
fields: ~[@struct_field], fields: ~[@struct_field],
@ -445,7 +445,7 @@ fn encode_info_for_class(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
} }
// This is for encoding info for ctors and dtors // This is for encoding info for ctors and dtors
fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
id: node_id, ident: ident, path: ast_map::path, id: node_id, ident: ident, path: ast_map::path,
item: Option<inlined_item>, tps: ~[ty_param]) { item: Option<inlined_item>, tps: ~[ty_param]) {
ebml_w.start_tag(tag_items_data_item); ebml_w.start_tag(tag_items_data_item);
@ -470,7 +470,7 @@ fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_info_for_method(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_info_for_method(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
impl_path: ast_map::path, should_inline: bool, impl_path: ast_map::path, should_inline: bool,
parent_id: node_id, parent_id: node_id,
m: @method, all_tps: ~[ty_param]) { m: @method, all_tps: ~[ty_param]) {
@ -520,7 +520,7 @@ fn should_inline(attrs: ~[attribute]) -> bool {
} }
fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
item: @item, index: @mut ~[entry<int>], item: @item, index: @mut ~[entry<int>],
path: ast_map::path) { path: ast_map::path) {
@ -533,7 +533,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
}; };
if !must_write && !reachable(ecx, item.id) { return; } if !must_write && !reachable(ecx, item.id) { return; }
fn add_to_index_(item: @item, ebml_w: ebml2::Serializer, fn add_to_index_(item: @item, ebml_w: ebml::Serializer,
index: @mut ~[entry<int>]) { index: @mut ~[entry<int>]) {
index.push({val: item.id, pos: ebml_w.writer.tell()}); index.push({val: item.id, pos: ebml_w.writer.tell()});
} }
@ -810,7 +810,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
} }
} }
fn encode_info_for_foreign_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_info_for_foreign_item(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
nitem: @foreign_item, nitem: @foreign_item,
index: @mut ~[entry<int>], index: @mut ~[entry<int>],
path: ast_map::path, abi: foreign_abi) { path: ast_map::path, abi: foreign_abi) {
@ -843,7 +843,7 @@ fn encode_info_for_foreign_item(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
crate: @crate) -> ~[entry<int>] { crate: @crate) -> ~[entry<int>] {
let index = @mut ~[]; let index = @mut ~[];
ebml_w.start_tag(tag_items_data); ebml_w.start_tag(tag_items_data);
@ -898,7 +898,7 @@ fn create_index<T: Copy Hash IterBytes>(index: ~[entry<T>]) ->
return buckets_frozen; return buckets_frozen;
} }
fn encode_index<T>(ebml_w: ebml2::Serializer, buckets: ~[@~[entry<T>]], fn encode_index<T>(ebml_w: ebml::Serializer, buckets: ~[@~[entry<T>]],
write_fn: fn(io::Writer, T)) { write_fn: fn(io::Writer, T)) {
let writer = ebml_w.writer; let writer = ebml_w.writer;
ebml_w.start_tag(tag_index); ebml_w.start_tag(tag_index);
@ -933,7 +933,7 @@ fn write_int(writer: io::Writer, &&n: int) {
writer.write_be_u32(n as u32); writer.write_be_u32(n as u32);
} }
fn encode_meta_item(ebml_w: ebml2::Serializer, mi: meta_item) { fn encode_meta_item(ebml_w: ebml::Serializer, mi: meta_item) {
match mi.node { match mi.node {
meta_word(name) => { meta_word(name) => {
ebml_w.start_tag(tag_meta_item_word); ebml_w.start_tag(tag_meta_item_word);
@ -970,7 +970,7 @@ fn encode_meta_item(ebml_w: ebml2::Serializer, mi: meta_item) {
} }
} }
fn encode_attributes(ebml_w: ebml2::Serializer, attrs: ~[attribute]) { fn encode_attributes(ebml_w: ebml::Serializer, attrs: ~[attribute]) {
ebml_w.start_tag(tag_attributes); ebml_w.start_tag(tag_attributes);
for attrs.each |attr| { for attrs.each |attr| {
ebml_w.start_tag(tag_attribute); ebml_w.start_tag(tag_attribute);
@ -1031,7 +1031,7 @@ fn synthesize_crate_attrs(ecx: @encode_ctxt, crate: @crate) -> ~[attribute] {
return attrs; return attrs;
} }
fn encode_crate_deps(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_crate_deps(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
cstore: cstore::cstore) { cstore: cstore::cstore) {
fn get_ordered_deps(ecx: @encode_ctxt, cstore: cstore::cstore) fn get_ordered_deps(ecx: @encode_ctxt, cstore: cstore::cstore)
@ -1077,7 +1077,7 @@ fn encode_crate_deps(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_crate_dep(ecx: @encode_ctxt, ebml_w: ebml2::Serializer, fn encode_crate_dep(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
dep: decoder::crate_dep) { dep: decoder::crate_dep) {
ebml_w.start_tag(tag_crate_dep); ebml_w.start_tag(tag_crate_dep);
ebml_w.start_tag(tag_crate_dep_name); ebml_w.start_tag(tag_crate_dep_name);
@ -1092,7 +1092,7 @@ fn encode_crate_dep(ecx: @encode_ctxt, ebml_w: ebml2::Serializer,
ebml_w.end_tag(); ebml_w.end_tag();
} }
fn encode_hash(ebml_w: ebml2::Serializer, hash: ~str) { fn encode_hash(ebml_w: ebml::Serializer, hash: ~str) {
ebml_w.start_tag(tag_crate_hash); ebml_w.start_tag(tag_crate_hash);
ebml_w.writer.write(str::to_bytes(hash)); ebml_w.writer.write(str::to_bytes(hash));
ebml_w.end_tag(); ebml_w.end_tag();
@ -1130,7 +1130,7 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
type_abbrevs: ty::new_ty_hash() type_abbrevs: ty::new_ty_hash()
}); });
let ebml_w = ebml2::Serializer(wr as io::Writer); let ebml_w = ebml::Serializer(wr as io::Writer);
encode_hash(ebml_w, ecx.link_meta.extras_hash); encode_hash(ebml_w, ecx.link_meta.extras_hash);

View file

@ -7,12 +7,11 @@ use syntax::visit;
use syntax::ast_map; use syntax::ast_map;
use syntax::ast_util; use syntax::ast_util;
use syntax::codemap::span; use syntax::codemap::span;
use std::ebml2; use std::ebml;
use std::ebml2::Serializer; use std::ebml::{Serializer, get_doc};
use std::ebml2::get_doc;
use std::map::HashMap; use std::map::HashMap;
use std::serialization2; use std::serialization;
use std::serialization2::{Serializable, use std::serialization::{Serializable,
SerializerHelpers, SerializerHelpers,
DeserializerHelpers, DeserializerHelpers,
deserialize}; deserialize};
@ -74,7 +73,7 @@ trait tr {
// Top-level methods. // Top-level methods.
fn encode_inlined_item(ecx: @e::encode_ctxt, fn encode_inlined_item(ecx: @e::encode_ctxt,
ebml_w: ebml2::Serializer, ebml_w: ebml::Serializer,
path: ast_map::path, path: ast_map::path,
ii: ast::inlined_item, ii: ast::inlined_item,
maps: maps) { maps: maps) {
@ -100,14 +99,14 @@ fn decode_inlined_item(cdata: cstore::crate_metadata,
tcx: ty::ctxt, tcx: ty::ctxt,
maps: maps, maps: maps,
path: ast_map::path, path: ast_map::path,
par_doc: ebml2::Doc) -> Option<ast::inlined_item> { par_doc: ebml::Doc) -> Option<ast::inlined_item> {
let dcx = @{cdata: cdata, tcx: tcx, maps: maps}; let dcx = @{cdata: cdata, tcx: tcx, maps: maps};
match par_doc.opt_child(c::tag_ast) { match par_doc.opt_child(c::tag_ast) {
None => None, None => None,
Some(ast_doc) => { Some(ast_doc) => {
debug!("> Decoding inlined fn: %s::?", debug!("> Decoding inlined fn: %s::?",
ast_map::path_to_str(path, tcx.sess.parse_sess.interner)); ast_map::path_to_str(path, tcx.sess.parse_sess.interner));
let ast_dsr = &ebml2::Deserializer(ast_doc); let ast_dsr = &ebml::Deserializer(ast_doc);
let from_id_range = deserialize(ast_dsr); let from_id_range = deserialize(ast_dsr);
let to_id_range = reserve_id_range(dcx.tcx.sess, from_id_range); let to_id_range = reserve_id_range(dcx.tcx.sess, from_id_range);
let xcx = extended_decode_ctxt_(@{dcx: dcx, let xcx = extended_decode_ctxt_(@{dcx: dcx,
@ -185,7 +184,7 @@ trait def_id_serializer_helpers {
fn emit_def_id(did: ast::def_id); fn emit_def_id(did: ast::def_id);
} }
impl<S: serialization2::Serializer> S: def_id_serializer_helpers { impl<S: serialization::Serializer> S: def_id_serializer_helpers {
fn emit_def_id(did: ast::def_id) { fn emit_def_id(did: ast::def_id) {
did.serialize(&self) did.serialize(&self)
} }
@ -195,7 +194,7 @@ trait def_id_deserializer_helpers {
fn read_def_id(xcx: extended_decode_ctxt) -> ast::def_id; fn read_def_id(xcx: extended_decode_ctxt) -> ast::def_id;
} }
impl<D: serialization2::Deserializer> D: def_id_deserializer_helpers { impl<D: serialization::Deserializer> D: def_id_deserializer_helpers {
fn read_def_id(xcx: extended_decode_ctxt) -> ast::def_id { fn read_def_id(xcx: extended_decode_ctxt) -> ast::def_id {
let did: ast::def_id = deserialize(&self); let did: ast::def_id = deserialize(&self);
@ -218,7 +217,7 @@ impl<D: serialization2::Deserializer> D: def_id_deserializer_helpers {
// We also have to adjust the spans: for now we just insert a dummy span, // We also have to adjust the spans: for now we just insert a dummy span,
// but eventually we should add entries to the local codemap as required. // but eventually we should add entries to the local codemap as required.
fn encode_ast(ebml_w: ebml2::Serializer, item: ast::inlined_item) { fn encode_ast(ebml_w: ebml::Serializer, item: ast::inlined_item) {
do ebml_w.wr_tag(c::tag_tree as uint) { do ebml_w.wr_tag(c::tag_tree as uint) {
item.serialize(&ebml_w) item.serialize(&ebml_w)
} }
@ -278,9 +277,9 @@ fn simplify_ast(ii: ast::inlined_item) -> ast::inlined_item {
} }
} }
fn decode_ast(par_doc: ebml2::Doc) -> ast::inlined_item { fn decode_ast(par_doc: ebml::Doc) -> ast::inlined_item {
let chi_doc = par_doc[c::tag_tree as uint]; let chi_doc = par_doc[c::tag_tree as uint];
let d = &ebml2::Deserializer(chi_doc); let d = &ebml::Deserializer(chi_doc);
deserialize(d) deserialize(d)
} }
@ -332,12 +331,12 @@ fn renumber_ast(xcx: extended_decode_ctxt, ii: ast::inlined_item)
// ______________________________________________________________________ // ______________________________________________________________________
// Encoding and decoding of ast::def // Encoding and decoding of ast::def
fn encode_def(ebml_w: ebml2::Serializer, def: ast::def) { fn encode_def(ebml_w: ebml::Serializer, def: ast::def) {
def.serialize(&ebml_w) def.serialize(&ebml_w)
} }
fn decode_def(xcx: extended_decode_ctxt, doc: ebml2::Doc) -> ast::def { fn decode_def(xcx: extended_decode_ctxt, doc: ebml::Doc) -> ast::def {
let dsr = &ebml2::Deserializer(doc); let dsr = &ebml::Deserializer(doc);
let def: ast::def = deserialize(dsr); let def: ast::def = deserialize(dsr);
def.tr(xcx) def.tr(xcx)
} }
@ -423,7 +422,7 @@ impl ty::bound_region: tr {
// ______________________________________________________________________ // ______________________________________________________________________
// Encoding and decoding of freevar information // Encoding and decoding of freevar information
fn encode_freevar_entry(ebml_w: ebml2::Serializer, fv: @freevar_entry) { fn encode_freevar_entry(ebml_w: ebml::Serializer, fv: @freevar_entry) {
(*fv).serialize(&ebml_w) (*fv).serialize(&ebml_w)
} }
@ -431,7 +430,7 @@ trait ebml_deserializer_helper {
fn read_freevar_entry(xcx: extended_decode_ctxt) -> freevar_entry; fn read_freevar_entry(xcx: extended_decode_ctxt) -> freevar_entry;
} }
impl ebml2::Deserializer: ebml_deserializer_helper { impl ebml::Deserializer: ebml_deserializer_helper {
fn read_freevar_entry(xcx: extended_decode_ctxt) -> freevar_entry { fn read_freevar_entry(xcx: extended_decode_ctxt) -> freevar_entry {
let fv: freevar_entry = deserialize(&self); let fv: freevar_entry = deserialize(&self);
fv.tr(xcx) fv.tr(xcx)
@ -452,7 +451,7 @@ trait read_method_map_entry_helper {
} }
fn serialize_method_map_entry(ecx: @e::encode_ctxt, fn serialize_method_map_entry(ecx: @e::encode_ctxt,
ebml_w: ebml2::Serializer, ebml_w: ebml::Serializer,
mme: method_map_entry) { mme: method_map_entry) {
do ebml_w.emit_rec { do ebml_w.emit_rec {
do ebml_w.emit_field(~"self_arg", 0u) { do ebml_w.emit_field(~"self_arg", 0u) {
@ -464,7 +463,7 @@ fn serialize_method_map_entry(ecx: @e::encode_ctxt,
} }
} }
impl ebml2::Deserializer: read_method_map_entry_helper { impl ebml::Deserializer: read_method_map_entry_helper {
fn read_method_map_entry(xcx: extended_decode_ctxt) -> method_map_entry { fn read_method_map_entry(xcx: extended_decode_ctxt) -> method_map_entry {
do self.read_rec { do self.read_rec {
{self_arg: {self_arg:
@ -503,7 +502,7 @@ impl method_origin: tr {
// Encoding and decoding vtable_res // Encoding and decoding vtable_res
fn encode_vtable_res(ecx: @e::encode_ctxt, fn encode_vtable_res(ecx: @e::encode_ctxt,
ebml_w: ebml2::Serializer, ebml_w: ebml::Serializer,
dr: typeck::vtable_res) { dr: typeck::vtable_res) {
// can't autogenerate this code because automatic serialization of // can't autogenerate this code because automatic serialization of
// ty::t doesn't work, and there is no way (atm) to have // ty::t doesn't work, and there is no way (atm) to have
@ -515,7 +514,7 @@ fn encode_vtable_res(ecx: @e::encode_ctxt,
} }
fn encode_vtable_origin(ecx: @e::encode_ctxt, fn encode_vtable_origin(ecx: @e::encode_ctxt,
ebml_w: ebml2::Serializer, ebml_w: ebml::Serializer,
vtable_origin: typeck::vtable_origin) { vtable_origin: typeck::vtable_origin) {
do ebml_w.emit_enum(~"vtable_origin") { do ebml_w.emit_enum(~"vtable_origin") {
match vtable_origin { match vtable_origin {
@ -562,7 +561,7 @@ trait vtable_deserialization_helpers {
fn read_vtable_origin(xcx: extended_decode_ctxt) -> typeck::vtable_origin; fn read_vtable_origin(xcx: extended_decode_ctxt) -> typeck::vtable_origin;
} }
impl ebml2::Deserializer: vtable_deserialization_helpers { impl ebml::Deserializer: vtable_deserialization_helpers {
fn read_vtable_res(xcx: extended_decode_ctxt) -> typeck::vtable_res { fn read_vtable_res(xcx: extended_decode_ctxt) -> typeck::vtable_res {
@self.read_to_vec(|| self.read_vtable_origin(xcx) ) @self.read_to_vec(|| self.read_vtable_origin(xcx) )
} }
@ -639,7 +638,7 @@ trait ebml_writer_helpers {
fn emit_tpbt(ecx: @e::encode_ctxt, tpbt: ty::ty_param_bounds_and_ty); fn emit_tpbt(ecx: @e::encode_ctxt, tpbt: ty::ty_param_bounds_and_ty);
} }
impl ebml2::Serializer: ebml_writer_helpers { impl ebml::Serializer: ebml_writer_helpers {
fn emit_ty(ecx: @e::encode_ctxt, ty: ty::t) { fn emit_ty(ecx: @e::encode_ctxt, ty: ty::t) {
do self.emit_opaque { do self.emit_opaque {
e::write_type(ecx, self, ty) e::write_type(ecx, self, ty)
@ -692,7 +691,7 @@ trait write_tag_and_id {
fn id(id: ast::node_id); fn id(id: ast::node_id);
} }
impl ebml2::Serializer: write_tag_and_id { impl ebml::Serializer: write_tag_and_id {
fn tag(tag_id: c::astencode_tag, f: fn()) { fn tag(tag_id: c::astencode_tag, f: fn()) {
do self.wr_tag(tag_id as uint) { f() } do self.wr_tag(tag_id as uint) { f() }
} }
@ -704,7 +703,7 @@ impl ebml2::Serializer: write_tag_and_id {
fn encode_side_tables_for_ii(ecx: @e::encode_ctxt, fn encode_side_tables_for_ii(ecx: @e::encode_ctxt,
maps: maps, maps: maps,
ebml_w: ebml2::Serializer, ebml_w: ebml::Serializer,
ii: ast::inlined_item) { ii: ast::inlined_item) {
do ebml_w.wr_tag(c::tag_table as uint) { do ebml_w.wr_tag(c::tag_table as uint) {
ast_util::visit_ids_for_inlined_item( ast_util::visit_ids_for_inlined_item(
@ -720,7 +719,7 @@ fn encode_side_tables_for_ii(ecx: @e::encode_ctxt,
fn encode_side_tables_for_id(ecx: @e::encode_ctxt, fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
maps: maps, maps: maps,
ebml_w: ebml2::Serializer, ebml_w: ebml::Serializer,
id: ast::node_id) { id: ast::node_id) {
let tcx = ecx.tcx; let tcx = ecx.tcx;
@ -849,13 +848,13 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
trait doc_decoder_helpers { trait doc_decoder_helpers {
fn as_int() -> int; fn as_int() -> int;
fn opt_child(tag: c::astencode_tag) -> Option<ebml2::Doc>; fn opt_child(tag: c::astencode_tag) -> Option<ebml::Doc>;
} }
impl ebml2::Doc: doc_decoder_helpers { impl ebml::Doc: doc_decoder_helpers {
fn as_int() -> int { ebml2::doc_as_u64(self) as int } fn as_int() -> int { ebml::doc_as_u64(self) as int }
fn opt_child(tag: c::astencode_tag) -> Option<ebml2::Doc> { fn opt_child(tag: c::astencode_tag) -> Option<ebml::Doc> {
ebml2::maybe_get_doc(self, tag as uint) ebml::maybe_get_doc(self, tag as uint)
} }
} }
@ -868,7 +867,7 @@ trait ebml_deserializer_decoder_helpers {
-> ty::ty_param_bounds_and_ty; -> ty::ty_param_bounds_and_ty;
} }
impl ebml2::Deserializer: ebml_deserializer_decoder_helpers { impl ebml::Deserializer: ebml_deserializer_decoder_helpers {
fn read_arg(xcx: extended_decode_ctxt) -> ty::arg { fn read_arg(xcx: extended_decode_ctxt) -> ty::arg {
do self.read_opaque |doc| { do self.read_opaque |doc| {
@ -923,10 +922,10 @@ impl ebml2::Deserializer: ebml_deserializer_decoder_helpers {
} }
fn decode_side_tables(xcx: extended_decode_ctxt, fn decode_side_tables(xcx: extended_decode_ctxt,
ast_doc: ebml2::Doc) { ast_doc: ebml::Doc) {
let dcx = xcx.dcx; let dcx = xcx.dcx;
let tbl_doc = ast_doc[c::tag_table as uint]; let tbl_doc = ast_doc[c::tag_table as uint];
for ebml2::docs(tbl_doc) |tag, entry_doc| { for ebml::docs(tbl_doc) |tag, entry_doc| {
let id0 = entry_doc[c::tag_table_id as uint].as_int(); let id0 = entry_doc[c::tag_table_id as uint].as_int();
let id = xcx.tr_id(id0); let id = xcx.tr_id(id0);
@ -940,7 +939,7 @@ fn decode_side_tables(xcx: extended_decode_ctxt,
dcx.tcx.legacy_boxed_traits.insert(id, ()); dcx.tcx.legacy_boxed_traits.insert(id, ());
} else { } else {
let val_doc = entry_doc[c::tag_table_val as uint]; let val_doc = entry_doc[c::tag_table_val as uint];
let val_dsr = &ebml2::Deserializer(val_doc); let val_dsr = &ebml::Deserializer(val_doc);
if tag == (c::tag_table_def as uint) { if tag == (c::tag_table_def as uint) {
let def = decode_def(xcx, val_doc); let def = decode_def(xcx, val_doc);
dcx.tcx.def_map.insert(id, def); dcx.tcx.def_map.insert(id, def);
@ -993,16 +992,16 @@ fn decode_side_tables(xcx: extended_decode_ctxt,
// Testing of astencode_gen // Testing of astencode_gen
#[cfg(test)] #[cfg(test)]
fn encode_item_ast(ebml_w: ebml2::Serializer, item: @ast::item) { fn encode_item_ast(ebml_w: ebml::Serializer, item: @ast::item) {
do ebml_w.wr_tag(c::tag_tree as uint) { do ebml_w.wr_tag(c::tag_tree as uint) {
(*item).serialize(&ebml_w) (*item).serialize(&ebml_w)
} }
} }
#[cfg(test)] #[cfg(test)]
fn decode_item_ast(par_doc: ebml2::Doc) -> @ast::item { fn decode_item_ast(par_doc: ebml::Doc) -> @ast::item {
let chi_doc = par_doc[c::tag_tree as uint]; let chi_doc = par_doc[c::tag_tree as uint];
let d = &ebml2::Deserializer(chi_doc); let d = &ebml::Deserializer(chi_doc);
@deserialize(d) @deserialize(d)
} }
@ -1029,17 +1028,17 @@ fn mk_ctxt() -> fake_ext_ctxt {
#[cfg(test)] #[cfg(test)]
fn roundtrip(in_item: @ast::item) { fn roundtrip(in_item: @ast::item) {
let bytes = do io::with_bytes_writer |wr| { let bytes = do io::with_bytes_writer |wr| {
let ebml_w = ebml2::Serializer(wr); let ebml_w = ebml::Serializer(wr);
encode_item_ast(ebml_w, in_item); encode_item_ast(ebml_w, in_item);
}; };
let ebml_doc = ebml2::Doc(@bytes); let ebml_doc = ebml::Doc(@bytes);
let out_item = decode_item_ast(ebml_doc); let out_item = decode_item_ast(ebml_doc);
let exp_str = do io::with_str_writer |w| { let exp_str = do io::with_str_writer |w| {
in_item.serialize(&std::prettyprint2::Serializer(w)) in_item.serialize(&std::prettyprint::Serializer(w))
}; };
let out_str = do io::with_str_writer |w| { let out_str = do io::with_str_writer |w| {
out_item.serialize(&std::prettyprint2::Serializer(w)) out_item.serialize(&std::prettyprint::Serializer(w))
}; };
debug!("expected string: %s", exp_str); debug!("expected string: %s", exp_str);

View file

@ -16,8 +16,8 @@ export has_freevars;
// A vector of defs representing the free variables referred to in a function. // A vector of defs representing the free variables referred to in a function.
// (The def_upvar will already have been stripped). // (The def_upvar will already have been stripped).
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type freevar_entry = { type freevar_entry = {
def: ast::def, //< The variable being accessed free. def: ast::def, //< The variable being accessed free.
span: span //< First span where it is accessed (there can be multiple) span: span //< First span where it is accessed (there can be multiple)

View file

@ -211,8 +211,8 @@ type method = {ident: ast::ident,
type mt = {ty: t, mutbl: ast::mutability}; type mt = {ty: t, mutbl: ast::mutability};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum vstore { enum vstore {
vstore_fixed(uint), vstore_fixed(uint),
vstore_uniq, vstore_uniq,
@ -271,8 +271,8 @@ enum ast_ty_to_ty_cache_entry {
type opt_region_variance = Option<region_variance>; type opt_region_variance = Option<region_variance>;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum region_variance { rv_covariant, rv_invariant, rv_contravariant } enum region_variance { rv_covariant, rv_invariant, rv_contravariant }
impl region_variance : cmp::Eq { impl region_variance : cmp::Eq {
@ -289,23 +289,23 @@ impl region_variance : cmp::Eq {
pure fn ne(other: &region_variance) -> bool { !self.eq(other) } pure fn ne(other: &region_variance) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type AutoAdjustment = { type AutoAdjustment = {
autoderefs: uint, autoderefs: uint,
autoref: Option<AutoRef> autoref: Option<AutoRef>
}; };
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type AutoRef = { type AutoRef = {
kind: AutoRefKind, kind: AutoRefKind,
region: region, region: region,
mutbl: ast::mutability mutbl: ast::mutability
}; };
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum AutoRefKind { enum AutoRefKind {
/// Convert from @[]/~[] to &[] (or str) /// Convert from @[]/~[] to &[] (or str)
AutoSlice, AutoSlice,
@ -509,8 +509,8 @@ impl param_ty : to_bytes::IterBytes {
/// Representation of regions: /// Representation of regions:
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum region { enum region {
/// Bound regions are found (primarily) in function types. They indicate /// Bound regions are found (primarily) in function types. They indicate
/// region parameters that have yet to be replaced with actual regions /// region parameters that have yet to be replaced with actual regions
@ -538,8 +538,8 @@ enum region {
re_var(RegionVid) re_var(RegionVid)
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum bound_region { enum bound_region {
/// The self region for classes, impls (&T in a type defn or &self/T) /// The self region for classes, impls (&T in a type defn or &self/T)
br_self, br_self,
@ -669,8 +669,8 @@ enum param_bound {
enum TyVid = uint; enum TyVid = uint;
enum IntVid = uint; enum IntVid = uint;
enum FnVid = uint; enum FnVid = uint;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum RegionVid = uint; enum RegionVid = uint;
enum InferTy { enum InferTy {

View file

@ -75,8 +75,8 @@ export method_static, method_param, method_trait, method_self;
export vtable_static, vtable_param, vtable_trait; export vtable_static, vtable_param, vtable_trait;
export provided_methods_map; export provided_methods_map;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum method_origin { enum method_origin {
// fully statically resolved method // fully statically resolved method
method_static(ast::def_id), method_static(ast::def_id),
@ -93,8 +93,8 @@ enum method_origin {
// details for a method invoked with a receiver whose type is a type parameter // details for a method invoked with a receiver whose type is a type parameter
// with a bounded trait. // with a bounded trait.
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type method_param = { type method_param = {
// the trait containing the method to be invoked // the trait containing the method to be invoked
trait_id: ast::def_id, trait_id: ast::def_id,

View file

@ -4,10 +4,10 @@ extern mod std;
// the common code. // the common code.
use cmp::Eq; use cmp::Eq;
use std::ebml2; use std::ebml;
use io::Writer; use io::Writer;
use std::serialization2::{Serializable, Deserializable, deserialize}; use std::serialization::{Serializable, Deserializable, deserialize};
use std::prettyprint2; use std::prettyprint;
fn test_ser_and_deser<A:Eq Serializable Deserializable>( fn test_ser_and_deser<A:Eq Serializable Deserializable>(
a1: &A, a1: &A,
@ -15,23 +15,23 @@ fn test_ser_and_deser<A:Eq Serializable Deserializable>(
) { ) {
// check the pretty printer: // check the pretty printer:
let s = do io::with_str_writer |w| { let s = do io::with_str_writer |w| {
a1.serialize(&prettyprint2::Serializer(w)) a1.serialize(&prettyprint::Serializer(w))
}; };
debug!("s == %?", s); debug!("s == %?", s);
assert s == expected; assert s == expected;
// check the EBML serializer: // check the EBML serializer:
let bytes = do io::with_bytes_writer |wr| { let bytes = do io::with_bytes_writer |wr| {
let ebml_w = &ebml2::Serializer(wr); let ebml_w = &ebml::Serializer(wr);
a1.serialize(ebml_w) a1.serialize(ebml_w)
}; };
let d = ebml2::Doc(@bytes); let d = ebml::Doc(@bytes);
let a2: A = deserialize(&ebml2::Deserializer(d)); let a2: A = deserialize(&ebml::Deserializer(d));
assert *a1 == a2; assert *a1 == a2;
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum Expr { enum Expr {
Val(uint), Val(uint),
Plus(@Expr, @Expr), Plus(@Expr, @Expr),
@ -105,8 +105,8 @@ impl CLike : cmp::Eq {
pure fn ne(other: &CLike) -> bool { !self.eq(other) } pure fn ne(other: &CLike) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type Spanned<T> = {lo: uint, hi: uint, node: T}; type Spanned<T> = {lo: uint, hi: uint, node: T};
impl<T:cmp::Eq> Spanned<T> : cmp::Eq { impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
@ -116,27 +116,27 @@ impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
pure fn ne(other: &Spanned<T>) -> bool { !self.eq(other) } pure fn ne(other: &Spanned<T>) -> bool { !self.eq(other) }
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
type SomeRec = {v: ~[uint]}; type SomeRec = {v: ~[uint]};
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum AnEnum = SomeRec; enum AnEnum = SomeRec;
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
struct Point {x: uint, y: uint} struct Point {x: uint, y: uint}
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum Quark<T> { enum Quark<T> {
Top(T), Top(T),
Bottom(T) Bottom(T)
} }
#[auto_serialize2] #[auto_serialize]
#[auto_deserialize2] #[auto_deserialize]
enum CLike { A, B, C } enum CLike { A, B, C }
fn main() { fn main() {