#[doc = "Support code for serialization."]; use core; /* Core serialization interfaces. */ iface 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); // 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()); } iface 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_str() -> str; fn read_f64() -> f64; fn read_f32() -> f32; fn read_float() -> float; // Compound types: fn read_enum(name: str, f: fn() -> T) -> T; fn read_enum_variant(f: fn(uint) -> T) -> T; fn read_enum_variant_arg(idx: uint, f: fn() -> T) -> T; fn read_vec(f: fn(uint) -> T) -> T; fn read_vec_elt(idx: uint, f: fn() -> T) -> T; fn read_box(f: fn() -> T) -> T; fn read_uniq(f: fn() -> T) -> T; fn read_rec(f: fn() -> T) -> T; fn read_rec_field(f_name: str, f_idx: uint, f: fn() -> T) -> T; fn read_tup(sz: uint, f: fn() -> T) -> T; fn read_tup_elt(idx: uint, f: fn() -> T) -> T; } // ___________________________________________________________________________ // Helper routines // // In some cases, these should eventually be coded as traits. fn emit_from_vec(s: S, v: [T], f: fn(T)) { s.emit_vec(vec::len(v)) {|| vec::iteri(v) {|i,e| s.emit_vec_elt(i) {|| f(e) } } } } fn read_to_vec(d: D, f: fn() -> T) -> [T] { d.read_vec {|len| vec::from_fn(len) {|i| d.read_vec_elt(i) {|| f() } } } } impl serializer_helpers for S { fn emit_from_vec(v: [T], f: fn(T)) { emit_from_vec(self, v, f) } } impl deserializer_helpers for D { fn read_to_vec(f: fn() -> T) -> [T] { read_to_vec(self, f) } } fn serialize_uint(s: S, v: uint) { s.emit_uint(v); } fn deserialize_uint(d: D) -> uint { d.read_uint() } fn serialize_u8(s: S, v: u8) { s.emit_u8(v); } fn deserialize_u8(d: D) -> u8 { d.read_u8() } fn serialize_u16(s: S, v: u16) { s.emit_u16(v); } fn deserialize_u16(d: D) -> u16 { d.read_u16() } fn serialize_u32(s: S, v: u32) { s.emit_u32(v); } fn deserialize_u32(d: D) -> u32 { d.read_u32() } fn serialize_u64(s: S, v: u64) { s.emit_u64(v); } fn deserialize_u64(d: D) -> u64 { d.read_u64() } fn serialize_int(s: S, v: int) { s.emit_int(v); } fn deserialize_int(d: D) -> int { d.read_int() } fn serialize_i8(s: S, v: i8) { s.emit_i8(v); } fn deserialize_i8(d: D) -> i8 { d.read_i8() } fn serialize_i16(s: S, v: i16) { s.emit_i16(v); } fn deserialize_i16(d: D) -> i16 { d.read_i16() } fn serialize_i32(s: S, v: i32) { s.emit_i32(v); } fn deserialize_i32(d: D) -> i32 { d.read_i32() } fn serialize_i64(s: S, v: i64) { s.emit_i64(v); } fn deserialize_i64(d: D) -> i64 { d.read_i64() } fn serialize_str(s: S, v: str) { s.emit_str(v); } fn deserialize_str(d: D) -> str { d.read_str() } fn serialize_float(s: S, v: float) { s.emit_float(v); } fn deserialize_float(d: D) -> float { d.read_float() } fn serialize_f32(s: S, v: f32) { s.emit_f32(v); } fn deserialize_f32(d: D) -> f32 { d.read_f32() } fn serialize_f64(s: S, v: f64) { s.emit_f64(v); } fn deserialize_f64(d: D) -> f64 { d.read_f64() } fn serialize_bool(s: S, v: bool) { s.emit_bool(v); } fn deserialize_bool(d: D) -> bool { d.read_bool() } fn serialize_option(s: S, v: option, st: fn(T)) { s.emit_enum("option") {|| alt v { none { s.emit_enum_variant("none", 0u, 0u) {|| } } some(v) { s.emit_enum_variant("some", 1u, 1u) {|| s.emit_enum_variant_arg(0u) {|| st(v) } } } } } } fn deserialize_option(d: D, st: fn() -> T) -> option { d.read_enum("option") {|| d.read_enum_variant {|i| alt check i { 0u { // none none } 1u { // some(v) some(d.read_enum_variant_arg(0u) {|| st() }) } } } } }