Skip to content

Splitting serialization2 into a serializer and deserializer trait #3620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ pub pure fn to_bytes(s: &str) -> ~[u8] unsafe {
#[inline(always)]
pub pure fn byte_slice<T>(s: &str, f: fn(v: &[u8]) -> T) -> T {
do as_buf(s) |p,n| {
unsafe { vec::raw::form_slice(p, n-1u, f) }
unsafe { vec::raw::buf_as_slice(p, n-1u, f) }
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/libcore/uint-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ pub pure fn to_str_bytes<U>(neg: bool, num: T, radix: uint,
*ptr::mut_offset(mp, i) = '-' as u8;
}

vec::raw::form_slice(ptr::offset(p, i),
len - i, f)
vec::raw::buf_as_slice(ptr::offset(p, i), len - i, f)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,7 +1838,7 @@ mod raw {
* not bytes).
*/
#[inline(always)]
unsafe fn form_slice<T,U>(p: *T, len: uint, f: fn(v: &[T]) -> U) -> U {
unsafe fn buf_as_slice<T,U>(p: *T, len: uint, f: fn(v: &[T]) -> U) -> U {
let pair = (p, len * sys::size_of::<T>());
let v : *(&blk/[T]) =
::cast::reinterpret_cast(&addr_of(&pair));
Expand Down
93 changes: 67 additions & 26 deletions src/libstd/ebml2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,25 @@ impl Serializer: serialization2::Serializer {
fail ~"Unimplemented: serializing a float";
}

fn emit_str(&self, v: &str) { self.wr_tagged_str(EsStr as uint, v) }
fn emit_char(&self, _v: char) {
fail ~"Unimplemented: serializing a char";
}

fn emit_borrowed_str(&self, v: &str) {
self.wr_tagged_str(EsStr as uint, v)
}

fn emit_owned_str(&self, v: &str) {
self.emit_borrowed_str(v)
}

fn emit_managed_str(&self, v: &str) {
self.emit_borrowed_str(v)
}

fn emit_borrowed(&self, f: fn()) { f() }
fn emit_owned(&self, f: fn()) { f() }
fn emit_managed(&self, f: fn()) { f() }

fn emit_enum(&self, name: &str, f: fn()) {
self._emit_label(name);
Expand All @@ -397,25 +415,33 @@ impl Serializer: serialization2::Serializer {
}
fn emit_enum_variant_arg(&self, _idx: uint, f: fn()) { f() }

fn emit_vec(&self, len: uint, f: fn()) {
fn emit_borrowed_vec(&self, len: uint, f: fn()) {
do self.wr_tag(EsVec as uint) {
self._emit_tagged_uint(EsVecLen, len);
f()
}
}

fn emit_owned_vec(&self, len: uint, f: fn()) {
self.emit_borrowed_vec(len, f)
}

fn emit_managed_vec(&self, len: uint, f: fn()) {
self.emit_borrowed_vec(len, f)
}

fn emit_vec_elt(&self, _idx: uint, f: fn()) {
self.wr_tag(EsVecElt as uint, f)
}

fn emit_box(&self, f: fn()) { f() }
fn emit_uniq(&self, f: fn()) { f() }
fn emit_rec(&self, f: fn()) { f() }
fn emit_rec_field(&self, f_name: &str, _f_idx: uint, f: fn()) {
self._emit_label(f_name);
fn emit_struct(&self, _name: &str, f: fn()) { f() }
fn emit_field(&self, name: &str, _idx: uint, f: fn()) {
self._emit_label(name);
f()
}
fn emit_tup(&self, _sz: uint, f: fn()) { f() }

fn emit_tup(&self, _len: uint, f: fn()) { f() }
fn emit_tup_elt(&self, _idx: uint, f: fn()) { f() }
}

Expand Down Expand Up @@ -525,9 +551,22 @@ impl Deserializer: serialization2::Deserializer {
fn read_f32(&self) -> f32 { fail ~"read_f32()"; }
fn read_float(&self) -> float { fail ~"read_float()"; }

fn read_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) }
fn read_char(&self) -> char { fail ~"read_char()"; }

fn read_owned_str(&self) -> ~str { doc_as_str(self.next_doc(EsStr)) }
fn read_managed_str(&self) -> @str { fail ~"read_managed_str()"; }

// Compound types:
fn read_owned<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);
Expand All @@ -548,8 +587,17 @@ impl Deserializer: serialization2::Deserializer {
f()
}

fn read_vec<T>(&self, f: fn(uint) -> T) -> T {
debug!("read_vec()");
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);
Expand All @@ -562,30 +610,24 @@ impl Deserializer: serialization2::Deserializer {
self.push_doc(self.next_doc(EsVecElt), f)
}

fn read_box<T>(&self, f: fn() -> T) -> T {
debug!("read_box()");
fn read_rec<T>(&self, f: fn() -> T) -> T {
debug!("read_rec()");
f()
}

fn read_uniq<T>(&self, f: fn() -> T) -> T {
debug!("read_uniq()");
fn read_struct<T>(&self, name: &str, f: fn() -> T) -> T {
debug!("read_struct(name=%s)", name);
f()
}

fn read_rec<T>(&self, f: fn() -> T) -> T {
debug!("read_rec()");
f()
}

fn read_rec_field<T>(&self, f_name: &str, f_idx: uint,
f: fn() -> T) -> T {
debug!("read_rec_field(%s, idx=%u)", f_name, f_idx);
self._check_label(f_name);
fn read_field<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, sz: uint, f: fn() -> T) -> T {
debug!("read_tup(sz=%u)", sz);
fn read_tup<T>(&self, len: uint, f: fn() -> T) -> T {
debug!("read_tup(len=%u)", len);
f()
}

Expand All @@ -595,7 +637,6 @@ impl Deserializer: serialization2::Deserializer {
}
}


// ___________________________________________________________________________
// Testing

Expand Down
Loading