libstd: change serialization2 to take &self argument methods
Unfortunately this trips over issue (#3585), where auto-ref isn't playing nicely with @T implementations. Most serializers don't care, but prettyprint2 won't properly display "@" until #3585 is fixed.
This commit is contained in:
parent
cd93441705
commit
c0b9986c8f
5 changed files with 335 additions and 235 deletions
|
@ -4,179 +4,214 @@
|
|||
Core serialization interfaces.
|
||||
*/
|
||||
|
||||
trait Serializer {
|
||||
#[forbid(deprecated_mode)];
|
||||
#[forbid(deprecated_pattern)];
|
||||
#[forbid(non_camel_case_types)];
|
||||
|
||||
pub trait Serializer {
|
||||
// Primitive types:
|
||||
fn emit_nil();
|
||||
fn emit_uint(v: uint);
|
||||
fn emit_u64(v: u64);
|
||||
fn emit_u32(v: u32);
|
||||
fn emit_u16(v: u16);
|
||||
fn emit_u8(v: u8);
|
||||
fn emit_int(v: int);
|
||||
fn emit_i64(v: i64);
|
||||
fn emit_i32(v: i32);
|
||||
fn emit_i16(v: i16);
|
||||
fn emit_i8(v: i8);
|
||||
fn emit_bool(v: bool);
|
||||
fn emit_float(v: float);
|
||||
fn emit_f64(v: f64);
|
||||
fn emit_f32(v: f32);
|
||||
fn emit_str(v: &str);
|
||||
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_str(&self, v: &str);
|
||||
|
||||
// Compound types:
|
||||
fn emit_enum(name: &str, f: fn());
|
||||
fn emit_enum_variant(v_name: &str, v_id: uint, sz: uint, f: fn());
|
||||
fn emit_enum_variant_arg(idx: uint, f: fn());
|
||||
fn emit_vec(len: uint, f: fn());
|
||||
fn emit_vec_elt(idx: uint, f: fn());
|
||||
fn emit_box(f: fn());
|
||||
fn emit_uniq(f: fn());
|
||||
fn emit_rec(f: fn());
|
||||
fn emit_rec_field(f_name: &str, f_idx: uint, f: fn());
|
||||
fn emit_tup(sz: uint, f: fn());
|
||||
fn emit_tup_elt(idx: uint, f: fn());
|
||||
fn emit_enum(&self, name: &str, f: fn());
|
||||
fn emit_enum_variant(&self, v_name: &str, v_id: uint, sz: uint, f: fn());
|
||||
fn emit_enum_variant_arg(&self, idx: uint, f: fn());
|
||||
fn emit_vec(&self, len: uint, f: fn());
|
||||
fn emit_vec_elt(&self, idx: uint, f: fn());
|
||||
fn emit_box(&self, f: fn());
|
||||
fn emit_uniq(&self, f: fn());
|
||||
fn emit_rec(&self, f: fn());
|
||||
fn emit_rec_field(&self, f_name: &str, f_idx: uint, f: fn());
|
||||
fn emit_tup(&self, sz: uint, f: fn());
|
||||
fn emit_tup_elt(&self, idx: uint, f: fn());
|
||||
}
|
||||
|
||||
trait Deserializer {
|
||||
pub trait Deserializer {
|
||||
// Primitive types:
|
||||
fn read_nil() -> ();
|
||||
fn read_uint() -> uint;
|
||||
fn read_u64() -> u64;
|
||||
fn read_u32() -> u32;
|
||||
fn read_u16() -> u16;
|
||||
fn read_u8() -> u8;
|
||||
fn read_int() -> int;
|
||||
fn read_i64() -> i64;
|
||||
fn read_i32() -> i32;
|
||||
fn read_i16() -> i16;
|
||||
fn read_i8() -> i8;
|
||||
fn read_bool() -> bool;
|
||||
fn read_f64() -> f64;
|
||||
fn read_f32() -> f32;
|
||||
fn read_float() -> float;
|
||||
fn read_str() -> ~str;
|
||||
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_str(&self) -> ~str;
|
||||
|
||||
// Compound types:
|
||||
fn read_enum<T>(name: ~str, f: fn() -> T) -> T;
|
||||
fn read_enum_variant<T>(f: fn(uint) -> T) -> T;
|
||||
fn read_enum_variant_arg<T>(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_box<T>(f: fn() -> T) -> T;
|
||||
fn read_uniq<T>(f: fn() -> T) -> T;
|
||||
fn read_rec<T>(f: fn() -> T) -> T;
|
||||
fn read_rec_field<T>(f_name: ~str, f_idx: uint, f: fn() -> T) -> T;
|
||||
fn read_tup<T>(sz: uint, f: fn() -> T) -> T;
|
||||
fn read_tup_elt<T>(idx: uint, f: fn() -> T) -> T;
|
||||
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_vec<T>(&self, f: fn(uint) -> T) -> T;
|
||||
fn read_vec_elt<T>(&self, idx: uint, f: fn() -> T) -> T;
|
||||
fn read_box<T>(&self, f: fn() -> T) -> T;
|
||||
fn read_uniq<T>(&self, f: fn() -> T) -> T;
|
||||
fn read_rec<T>(&self, f: fn() -> T) -> T;
|
||||
fn read_rec_field<T>(&self, f_name: ~str, f_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;
|
||||
}
|
||||
|
||||
trait Serializable {
|
||||
fn serialize<S: Serializer>(s: S);
|
||||
static fn deserialize<D: Deserializer>(d: D) -> self;
|
||||
pub trait Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S);
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> self;
|
||||
}
|
||||
|
||||
impl uint: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_uint(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> uint { d.read_uint() }
|
||||
pub impl uint: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_uint(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> uint {
|
||||
d.read_uint()
|
||||
}
|
||||
}
|
||||
|
||||
impl u8: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_u8(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> u8 { d.read_u8() }
|
||||
pub impl u8: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u8(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> u8 {
|
||||
d.read_u8()
|
||||
}
|
||||
}
|
||||
|
||||
impl u16: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_u16(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> u16 { d.read_u16() }
|
||||
pub impl u16: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u16(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> u16 {
|
||||
d.read_u16()
|
||||
}
|
||||
}
|
||||
|
||||
impl u32: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_u32(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> u32 { d.read_u32() }
|
||||
pub impl u32: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u32(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> u32 {
|
||||
d.read_u32()
|
||||
}
|
||||
}
|
||||
|
||||
impl u64: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_u64(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> u64 { d.read_u64() }
|
||||
pub impl u64: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_u64(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> u64 {
|
||||
d.read_u64()
|
||||
}
|
||||
}
|
||||
|
||||
impl int: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_int(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> int { d.read_int() }
|
||||
pub impl int: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_int(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> int {
|
||||
d.read_int()
|
||||
}
|
||||
}
|
||||
|
||||
impl i8: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_i8(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> i8 { d.read_i8() }
|
||||
pub impl i8: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i8(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> i8 {
|
||||
d.read_i8()
|
||||
}
|
||||
}
|
||||
|
||||
impl i16: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_i16(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> i16 { d.read_i16() }
|
||||
pub impl i16: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i16(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> i16 {
|
||||
d.read_i16()
|
||||
}
|
||||
}
|
||||
|
||||
impl i32: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_i32(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> i32 { d.read_i32() }
|
||||
pub impl i32: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i32(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> i32 {
|
||||
d.read_i32()
|
||||
}
|
||||
}
|
||||
|
||||
impl i64: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_i64(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> i64 { d.read_i64() }
|
||||
pub impl i64: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_i64(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> i64 {
|
||||
d.read_i64()
|
||||
}
|
||||
}
|
||||
|
||||
impl ~str: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_str(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> ~str { d.read_str() }
|
||||
pub impl ~str: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_str(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~str {
|
||||
d.read_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl float: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_float(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> float { d.read_float() }
|
||||
pub impl float: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_float(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> float {
|
||||
d.read_float()
|
||||
}
|
||||
}
|
||||
|
||||
impl f32: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_f32(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> f32 { d.read_f32() }
|
||||
pub impl f32: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_f32(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> f32 {
|
||||
d.read_f32() }
|
||||
}
|
||||
|
||||
impl f64: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_f64(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> f64 { d.read_f64() }
|
||||
pub impl f64: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_f64(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> f64 {
|
||||
d.read_f64()
|
||||
}
|
||||
}
|
||||
|
||||
impl bool: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_bool(self) }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> bool { d.read_bool() }
|
||||
pub impl bool: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_bool(*self) }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> bool {
|
||||
d.read_bool()
|
||||
}
|
||||
}
|
||||
|
||||
impl (): Serializable {
|
||||
fn serialize<S: Serializer>(s: S) { s.emit_nil() }
|
||||
static fn deserialize<D: Deserializer>(d: D) -> () { d.read_nil() }
|
||||
pub impl (): Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) { s.emit_nil() }
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> () {
|
||||
d.read_nil()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serializable> @T: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) {
|
||||
pub impl<T: Serializable> @T: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) {
|
||||
s.emit_box(|| (*self).serialize(s))
|
||||
}
|
||||
|
||||
static fn deserialize<D: Deserializer>(d: D) -> @T {
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> @T {
|
||||
d.read_box(|| @deserialize(d))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serializable> ~T: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) {
|
||||
pub impl<T: Serializable> ~T: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) {
|
||||
s.emit_uniq(|| (*self).serialize(s))
|
||||
}
|
||||
|
||||
static fn deserialize<D: Deserializer>(d: D) -> ~T {
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~T {
|
||||
d.read_uniq(|| ~deserialize(d))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serializable> ~[T]: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) {
|
||||
pub impl<T: Serializable> ~[T]: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) {
|
||||
do s.emit_vec(self.len()) {
|
||||
for self.eachi |i, e| {
|
||||
s.emit_vec_elt(i, || e.serialize(s))
|
||||
|
@ -184,7 +219,7 @@ impl<T: Serializable> ~[T]: Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
static fn deserialize<D: Deserializer>(d: D) -> ~[T] {
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> ~[T] {
|
||||
do d.read_vec |len| {
|
||||
do vec::from_fn(len) |i| {
|
||||
d.read_vec_elt(i, || deserialize(d))
|
||||
|
@ -193,10 +228,10 @@ impl<T: Serializable> ~[T]: Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Serializable> Option<T>: Serializable {
|
||||
fn serialize<S: Serializer>(s: S) {
|
||||
pub impl<T: Serializable> Option<T>: Serializable {
|
||||
fn serialize<S: Serializer>(&self, s: &S) {
|
||||
do s.emit_enum(~"option") {
|
||||
match self {
|
||||
match *self {
|
||||
None => do s.emit_enum_variant(~"none", 0u, 0u) {
|
||||
},
|
||||
|
||||
|
@ -207,7 +242,7 @@ impl<T: Serializable> Option<T>: Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
static fn deserialize<D: Deserializer>(d: D) -> Option<T> {
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> Option<T> {
|
||||
do d.read_enum(~"option") {
|
||||
do d.read_enum_variant |i| {
|
||||
match i {
|
||||
|
@ -220,12 +255,12 @@ impl<T: Serializable> Option<T>: Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
pub impl<
|
||||
T0: Serializable,
|
||||
T1: Serializable
|
||||
> (T0, T1): Serializable {
|
||||
fn serialize<S: Serializer>(s: S) {
|
||||
match self {
|
||||
fn serialize<S: Serializer>(&self, s: &S) {
|
||||
match *self {
|
||||
(t0, t1) => {
|
||||
do s.emit_tup(2) {
|
||||
s.emit_tup_elt(0, || t0.serialize(s));
|
||||
|
@ -235,7 +270,7 @@ impl<
|
|||
}
|
||||
}
|
||||
|
||||
static fn deserialize<D: Deserializer>(d: D) -> (T0, T1) {
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1) {
|
||||
do d.read_tup(2) {
|
||||
(
|
||||
d.read_tup_elt(0, || deserialize(d)),
|
||||
|
@ -245,13 +280,13 @@ impl<
|
|||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
pub impl<
|
||||
T0: Serializable,
|
||||
T1: Serializable,
|
||||
T2: Serializable
|
||||
> (T0, T1, T2): Serializable {
|
||||
fn serialize<S: Serializer>(s: S) {
|
||||
match self {
|
||||
fn serialize<S: Serializer>(&self, s: &S) {
|
||||
match *self {
|
||||
(t0, t1, t2) => {
|
||||
do s.emit_tup(3) {
|
||||
s.emit_tup_elt(0, || t0.serialize(s));
|
||||
|
@ -262,7 +297,7 @@ impl<
|
|||
}
|
||||
}
|
||||
|
||||
static fn deserialize<D: Deserializer>(d: D) -> (T0, T1, T2) {
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1, T2) {
|
||||
do d.read_tup(3) {
|
||||
(
|
||||
d.read_tup_elt(0, || deserialize(d)),
|
||||
|
@ -273,14 +308,14 @@ impl<
|
|||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
pub impl<
|
||||
T0: Serializable,
|
||||
T1: Serializable,
|
||||
T2: Serializable,
|
||||
T3: Serializable
|
||||
> (T0, T1, T2, T3): Serializable {
|
||||
fn serialize<S: Serializer>(s: S) {
|
||||
match self {
|
||||
fn serialize<S: Serializer>(&self, s: &S) {
|
||||
match *self {
|
||||
(t0, t1, t2, t3) => {
|
||||
do s.emit_tup(4) {
|
||||
s.emit_tup_elt(0, || t0.serialize(s));
|
||||
|
@ -292,7 +327,7 @@ impl<
|
|||
}
|
||||
}
|
||||
|
||||
static fn deserialize<D: Deserializer>(d: D) -> (T0, T1, T2, T3) {
|
||||
static fn deserialize<D: Deserializer>(&self, d: &D) -> (T0, T1, T2, T3) {
|
||||
do d.read_tup(4) {
|
||||
(
|
||||
d.read_tup_elt(0, || deserialize(d)),
|
||||
|
@ -304,15 +339,15 @@ impl<
|
|||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
pub impl<
|
||||
T0: Serializable,
|
||||
T1: Serializable,
|
||||
T2: Serializable,
|
||||
T3: Serializable,
|
||||
T4: Serializable
|
||||
> (T0, T1, T2, T3, T4): Serializable {
|
||||
fn serialize<S: Serializer>(s: S) {
|
||||
match self {
|
||||
fn serialize<S: Serializer>(&self, s: &S) {
|
||||
match *self {
|
||||
(t0, t1, t2, t3, t4) => {
|
||||
do s.emit_tup(5) {
|
||||
s.emit_tup_elt(0, || t0.serialize(s));
|
||||
|
@ -325,7 +360,8 @@ impl<
|
|||
}
|
||||
}
|
||||
|
||||
static fn deserialize<D: Deserializer>(d: D) -> (T0, T1, T2, T3, T4) {
|
||||
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)),
|
||||
|
@ -343,40 +379,32 @@ impl<
|
|||
//
|
||||
// In some cases, these should eventually be coded as traits.
|
||||
|
||||
fn emit_from_vec<S: Serializer, T>(s: S, v: ~[T], f: fn(T)) {
|
||||
do s.emit_vec(v.len()) {
|
||||
pub trait SerializerHelpers {
|
||||
fn emit_from_vec<T>(&self, v: ~[T], f: fn(v: &T));
|
||||
}
|
||||
|
||||
pub impl<S: Serializer> S: SerializerHelpers {
|
||||
fn emit_from_vec<T>(&self, v: ~[T], f: fn(v: &T)) {
|
||||
do self.emit_vec(v.len()) {
|
||||
for v.eachi |i, e| {
|
||||
do s.emit_vec_elt(i) {
|
||||
f(*e)
|
||||
do self.emit_vec_elt(i) {
|
||||
f(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_to_vec<D: Deserializer, T: Copy>(d: D, f: fn() -> T) -> ~[T] {
|
||||
do d.read_vec |len| {
|
||||
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_vec |len| {
|
||||
do vec::from_fn(len) |i| {
|
||||
d.read_vec_elt(i, || f())
|
||||
self.read_vec_elt(i, || f())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait SerializerHelpers {
|
||||
fn emit_from_vec<T>(v: ~[T], f: fn(T));
|
||||
}
|
||||
|
||||
impl<S: Serializer> S: SerializerHelpers {
|
||||
fn emit_from_vec<T>(v: ~[T], f: fn(T)) {
|
||||
emit_from_vec(self, v, f)
|
||||
}
|
||||
}
|
||||
|
||||
trait DeserializerHelpers {
|
||||
fn read_to_vec<T: Copy>(f: fn() -> T) -> ~[T];
|
||||
}
|
||||
|
||||
impl<D: Deserializer> D: DeserializerHelpers {
|
||||
fn read_to_vec<T: Copy>(f: fn() -> T) -> ~[T] {
|
||||
read_to_vec(self, f)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -144,7 +144,6 @@ mod unicode;
|
|||
mod test;
|
||||
#[legacy_exports]
|
||||
mod serialization;
|
||||
#[legacy_exports]
|
||||
mod serialization2;
|
||||
|
||||
// Local Variables:
|
||||
|
|
|
@ -232,9 +232,24 @@ fn mk_ser_method(
|
|||
bounds: @~[ast::bound_trait(ser_bound)],
|
||||
}];
|
||||
|
||||
let ser_inputs = ~[{
|
||||
mode: ast::expl(ast::by_ref),
|
||||
let ty_s = @{
|
||||
id: cx.next_id(),
|
||||
node: ast::ty_rptr(
|
||||
@{
|
||||
id: cx.next_id(),
|
||||
node: ast::re_anon,
|
||||
},
|
||||
{
|
||||
ty: cx.ty_path(span, ~[cx.ident_of(~"__S")], ~[]),
|
||||
mutbl: ast::m_imm
|
||||
}
|
||||
),
|
||||
span: span,
|
||||
};
|
||||
|
||||
let ser_inputs = ~[{
|
||||
mode: ast::infer(cx.next_id()),
|
||||
ty: ty_s,
|
||||
ident: cx.ident_of(~"__s"),
|
||||
id: cx.next_id(),
|
||||
}];
|
||||
|
@ -255,7 +270,7 @@ fn mk_ser_method(
|
|||
ident: cx.ident_of(~"serialize"),
|
||||
attrs: ~[],
|
||||
tps: ser_tps,
|
||||
self_ty: { node: ast::sty_by_ref, span: span },
|
||||
self_ty: { node: ast::sty_region(ast::m_imm), span: span },
|
||||
purity: ast::impure_fn,
|
||||
decl: ser_decl,
|
||||
body: ser_body,
|
||||
|
@ -288,9 +303,24 @@ fn mk_deser_method(
|
|||
bounds: @~[ast::bound_trait(deser_bound)],
|
||||
}];
|
||||
|
||||
let deser_inputs = ~[{
|
||||
mode: ast::expl(ast::by_ref),
|
||||
let ty_d = @{
|
||||
id: cx.next_id(),
|
||||
node: ast::ty_rptr(
|
||||
@{
|
||||
id: cx.next_id(),
|
||||
node: ast::re_anon,
|
||||
},
|
||||
{
|
||||
ty: cx.ty_path(span, ~[cx.ident_of(~"__D")], ~[]),
|
||||
mutbl: ast::m_imm
|
||||
}
|
||||
),
|
||||
span: span,
|
||||
};
|
||||
|
||||
let deser_inputs = ~[{
|
||||
mode: ast::infer(cx.next_id()),
|
||||
ty: ty_d,
|
||||
ident: cx.ident_of(~"__d"),
|
||||
id: cx.next_id(),
|
||||
}];
|
||||
|
@ -608,11 +638,14 @@ fn mk_enum_ser_body(
|
|||
}
|
||||
};
|
||||
|
||||
// ast for `match self { $(arms) }`
|
||||
// ast for `match *self { $(arms) }`
|
||||
let match_expr = cx.expr(
|
||||
span,
|
||||
ast::expr_match(
|
||||
cx.expr_var(span, ~"self"),
|
||||
cx.expr(
|
||||
span,
|
||||
ast::expr_unary(ast::deref, cx.expr_var(span, ~"self"))
|
||||
),
|
||||
arms
|
||||
)
|
||||
);
|
||||
|
|
73
src/test/run-pass/auto_serialize2-box.rs
Normal file
73
src/test/run-pass/auto_serialize2-box.rs
Normal file
|
@ -0,0 +1,73 @@
|
|||
// xfail-test FIXME Blocked on (#3585)
|
||||
|
||||
extern mod std;
|
||||
|
||||
// These tests used to be separate files, but I wanted to refactor all
|
||||
// the common code.
|
||||
|
||||
use cmp::Eq;
|
||||
use std::ebml2;
|
||||
use io::Writer;
|
||||
use std::serialization2::{Serializer, Serializable, deserialize};
|
||||
use std::prettyprint2;
|
||||
|
||||
fn test_ser_and_deser<A:Eq Serializable>(
|
||||
a1: A,
|
||||
expected: ~str
|
||||
) {
|
||||
// check the pretty printer:
|
||||
let s = do io::with_str_writer |w| {
|
||||
a1.serialize(&prettyprint2::Serializer(w))
|
||||
};
|
||||
debug!("s == %?", s);
|
||||
assert s == expected;
|
||||
|
||||
// check the EBML serializer:
|
||||
let bytes = do io::with_bytes_writer |wr| {
|
||||
let ebml_w = &ebml2::Serializer(wr);
|
||||
a1.serialize(ebml_w)
|
||||
};
|
||||
let d = ebml2::Doc(@bytes);
|
||||
let a2: A = deserialize(&ebml2::Deserializer(d));
|
||||
assert a1 == a2;
|
||||
}
|
||||
|
||||
#[auto_serialize2]
|
||||
enum Expr {
|
||||
Val(uint),
|
||||
Plus(@Expr, @Expr),
|
||||
Minus(@Expr, @Expr)
|
||||
}
|
||||
|
||||
impl Expr : cmp::Eq {
|
||||
pure fn eq(other: &Expr) -> bool {
|
||||
match self {
|
||||
Val(e0a) => {
|
||||
match *other {
|
||||
Val(e0b) => e0a == e0b,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
Plus(e0a, e1a) => {
|
||||
match *other {
|
||||
Plus(e0b, e1b) => e0a == e0b && e1a == e1b,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
Minus(e0a, e1a) => {
|
||||
match *other {
|
||||
Minus(e0b, e1b) => e0a == e0b && e1a == e1b,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pure fn ne(other: &Expr) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test_ser_and_deser(Plus(@Minus(@Val(3u), @Val(10u)),
|
||||
@Plus(@Val(22u), @Val(5u))),
|
||||
~"Plus(@Minus(@Val(3u), @Val(10u)), \
|
||||
@Plus(@Val(22u), @Val(5u)))");
|
||||
}
|
|
@ -15,98 +15,71 @@ fn test_ser_and_deser<A:Eq Serializable>(
|
|||
) {
|
||||
|
||||
// check the pretty printer:
|
||||
let s = io::with_str_writer(|w| a1.serialize(w));
|
||||
let s = do io::with_str_writer |w| {
|
||||
a1.serialize(&prettyprint2::Serializer(w))
|
||||
};
|
||||
debug!("s == %?", s);
|
||||
assert s == expected;
|
||||
|
||||
// check the EBML serializer:
|
||||
let bytes = do io::with_bytes_writer |wr| {
|
||||
let ebml_w = ebml2::Serializer(wr);
|
||||
let ebml_w = &ebml2::Serializer(wr);
|
||||
a1.serialize(ebml_w)
|
||||
};
|
||||
let d = ebml2::Doc(@bytes);
|
||||
let a2: A = deserialize(ebml2::Deserializer(d));
|
||||
let a2: A = deserialize(&ebml2::Deserializer(d));
|
||||
assert a1 == a2;
|
||||
}
|
||||
|
||||
#[auto_serialize2]
|
||||
enum Expr {
|
||||
Val(uint),
|
||||
Plus(@Expr, @Expr),
|
||||
Minus(@Expr, @Expr)
|
||||
}
|
||||
|
||||
impl AnEnum : cmp::Eq {
|
||||
pure fn eq(&&other: AnEnum) -> bool {
|
||||
pure fn eq(other: &AnEnum) -> bool {
|
||||
self.v == other.v
|
||||
}
|
||||
pure fn ne(&&other: AnEnum) -> bool { !self.eq(other) }
|
||||
pure fn ne(other: &AnEnum) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
impl Point : cmp::Eq {
|
||||
pure fn eq(&&other: Point) -> bool {
|
||||
pure fn eq(other: &Point) -> bool {
|
||||
self.x == other.x && self.y == other.y
|
||||
}
|
||||
pure fn ne(&&other: Point) -> bool { !self.eq(other) }
|
||||
pure fn ne(other: &Point) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
impl<T:cmp::Eq> Quark<T> : cmp::Eq {
|
||||
pure fn eq(&&other: Quark<T>) -> bool {
|
||||
pure fn eq(other: &Quark<T>) -> bool {
|
||||
match self {
|
||||
Top(ref q) => match other {
|
||||
Top(ref q) => {
|
||||
match *other {
|
||||
Top(ref r) => q == r,
|
||||
Bottom(_) => false
|
||||
}
|
||||
},
|
||||
Bottom(ref q) => match other {
|
||||
Bottom(ref q) => {
|
||||
match *other {
|
||||
Top(_) => false,
|
||||
Bottom(ref r) => q == r
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
pure fn ne(&&other: Quark<T>) -> bool { !self.eq(other) }
|
||||
pure fn ne(other: &Quark<T>) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
impl CLike : cmp::Eq {
|
||||
pure fn eq(&&other: CLike) -> bool {
|
||||
self as int == other as int
|
||||
pure fn eq(other: &CLike) -> bool {
|
||||
self as int == *other as int
|
||||
}
|
||||
pure fn ne(&&other: CLike) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
impl Expr : cmp::Eq {
|
||||
pure fn eq(&&other: Expr) -> bool {
|
||||
match self {
|
||||
Val(e0a) => {
|
||||
match other {
|
||||
Val(e0b) => e0a == e0b,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
Plus(e0a, e1a) => {
|
||||
match other {
|
||||
Plus(e0b, e1b) => e0a == e0b && e1a == e1b,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
Minus(e0a, e1a) => {
|
||||
match other {
|
||||
Minus(e0b, e1b) => e0a == e0b && e1a == e1b,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pure fn ne(&&other: Expr) -> bool { !self.eq(other) }
|
||||
pure fn ne(other: &CLike) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
#[auto_serialize2]
|
||||
type Spanned<T> = {lo: uint, hi: uint, node: T};
|
||||
|
||||
impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
|
||||
pure fn eq(&&other: Spanned<T>) -> bool {
|
||||
self.lo == other.lo && self.hi == other.hi && self.node.eq(other.node)
|
||||
pure fn eq(other: &Spanned<T>) -> bool {
|
||||
self.lo == other.lo && self.hi == other.hi && self.node == other.node
|
||||
}
|
||||
pure fn ne(&&other: Spanned<T>) -> bool { !self.eq(other) }
|
||||
pure fn ne(other: &Spanned<T>) -> bool { !self.eq(other) }
|
||||
}
|
||||
|
||||
#[auto_serialize2]
|
||||
|
@ -128,12 +101,6 @@ enum Quark<T> {
|
|||
enum CLike { A, B, C }
|
||||
|
||||
fn main() {
|
||||
|
||||
test_ser_and_deser(Plus(@Minus(@Val(3u), @Val(10u)),
|
||||
@Plus(@Val(22u), @Val(5u))),
|
||||
~"Plus(@Minus(@Val(3u), @Val(10u)), \
|
||||
@Plus(@Val(22u), @Val(5u)))");
|
||||
|
||||
test_ser_and_deser({lo: 0u, hi: 5u, node: 22u},
|
||||
~"{lo: 0u, hi: 5u, node: 22u}");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue