1
Fork 0

std: add serialize {read,emit}_tuple{,_arg,_struct,_struct_arg}

This commit is contained in:
Erick Tryzelaar 2013-04-09 19:44:12 -07:00
parent 441df26f5a
commit e31f7b7c74
4 changed files with 84 additions and 0 deletions

View file

@ -356,6 +356,26 @@ pub mod reader {
f()
}
fn read_tuple<T>(&self, f: &fn(uint) -> T) -> T {
debug!("read_tuple()");
self.read_seq(f)
}
fn read_tuple_arg<T>(&self, idx: uint, f: &fn() -> T) -> T {
debug!("read_tuple_arg(idx=%u)", idx);
self.read_seq_elt(idx, f)
}
fn read_tuple_struct<T>(&self, name: &str, f: &fn(uint) -> T) -> T {
debug!("read_tuple_struct(name=%?)", name);
self.read_tuple(f)
}
fn read_tuple_struct_arg<T>(&self, idx: uint, f: &fn() -> T) -> T {
debug!("read_tuple_struct_arg(idx=%u)", idx);
self.read_tuple_arg(idx, f)
}
fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
debug!("read_option()");
do self.read_enum("Option") || {
@ -637,6 +657,12 @@ pub mod writer {
f()
}
fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) }
fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) }
fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) }
fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) }
fn emit_option(&self, f: &fn()) {
self.emit_enum("Option", f);
}

View file

@ -152,6 +152,12 @@ impl serialize::Encoder for Encoder {
f();
}
fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) }
fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) }
fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) }
fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) }
fn emit_option(&self, f: &fn()) { f(); }
fn emit_option_none(&self) { self.emit_nil(); }
fn emit_option_some(&self, f: &fn()) { f(); }
@ -291,6 +297,12 @@ impl serialize::Encoder for PrettyEncoder {
f();
}
fn emit_tuple(&self, len: uint, f: &fn()) { self.emit_seq(len, f) }
fn emit_tuple_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) }
fn emit_tuple_struct(&self, _name: &str, len: uint, f: &fn()) { self.emit_seq(len, f) }
fn emit_tuple_struct_arg(&self, idx: uint, f: &fn()) { self.emit_seq_elt(idx, f) }
fn emit_option(&self, f: &fn()) { f(); }
fn emit_option_none(&self) { self.emit_nil(); }
fn emit_option_some(&self, f: &fn()) { f(); }
@ -901,6 +913,26 @@ impl serialize::Decoder for Decoder {
}
}
fn read_tuple<T>(&self, f: &fn(uint) -> T) -> T {
debug!("read_tuple()");
self.read_seq(f)
}
fn read_tuple_arg<T>(&self, idx: uint, f: &fn() -> T) -> T {
debug!("read_tuple_arg(idx=%u)", idx);
self.read_seq_elt(idx, f)
}
fn read_tuple_struct<T>(&self, name: &str, f: &fn(uint) -> T) -> T {
debug!("read_tuple_struct(name=%?)", name);
self.read_tuple(f)
}
fn read_tuple_struct_arg<T>(&self, idx: uint, f: &fn() -> T) -> T {
debug!("read_tuple_struct_arg(idx=%u)", idx);
self.read_tuple_arg(idx, f)
}
fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
match self.stack.pop() {
Null => f(false),

View file

@ -56,6 +56,12 @@ pub trait Encoder {
#[cfg(stage3)]
fn emit_struct_field(&self, f_name: &str, f_idx: uint, f: &fn());
fn emit_tuple(&self, len: uint, f: &fn());
fn emit_tuple_arg(&self, idx: uint, f: &fn());
fn emit_tuple_struct(&self, name: &str, len: uint, f: &fn());
fn emit_tuple_struct_arg(&self, f_idx: uint, f: &fn());
// Specialized types:
fn emit_option(&self, f: &fn());
fn emit_option_none(&self);
@ -102,6 +108,12 @@ pub trait Decoder {
#[cfg(stage3)]
fn read_struct_field<T>(&self, f_name: &str, f_idx: uint, f: &fn() -> T) -> T;
fn read_tuple<T>(&self, f: &fn(uint) -> T) -> T;
fn read_tuple_arg<T>(&self, a_idx: uint, f: &fn() -> T) -> T;
fn read_tuple_struct<T>(&self, s_name: &str, f: &fn(uint) -> T) -> T;
fn read_tuple_struct_arg<T>(&self, a_idx: uint, f: &fn() -> T) -> T;
// Specialized types:
fn read_option<T>(&self, f: &fn(bool) -> T) -> T;

View file

@ -1260,6 +1260,20 @@ mod test {
self.add_to_log(CallToEmitField (name.to_str(),idx)); f();
}
fn emit_tuple(&self, _len: uint, f: &fn()) {
self.add_unknown_to_log(); f();
}
fn emit_tuple_arg(&self, _idx: uint, f: &fn()) {
self.add_unknown_to_log(); f();
}
fn emit_tuple_struct(&self, _name: &str, _len: uint, f: &fn()) {
self.add_unknown_to_log(); f();
}
fn emit_tuple_struct_arg(&self, _idx: uint, f: &fn()) {
self.add_unknown_to_log(); f();
}
fn emit_option(&self, f: &fn()) {
self.add_to_log(CallToEmitOption);
f();