Fix `int` serilization / deserialization size.

Before this change, encoding an int would be platform
dependent because it would use write_be_int rather than
picking a size up front.  This change chooses a size
(64 bit integer) and encodes and decodes with that size.

Closes #5
This commit is contained in:
TyOverby 2014-11-13 10:26:02 -08:00
parent 3c8f9792ab
commit 2131a69ec8
2 changed files with 5 additions and 11 deletions

View File

@ -1,7 +1,4 @@
use std::io::Buffer;
use std::io::Reader;
use std::io::IoError;
use std::io::IoResult;
use std::io::Buffer; use std::io::Reader; use std::io::IoError; use std::io::IoResult;
use std::io::OtherIoError;
use serialize::Decoder;
@ -22,10 +19,7 @@ impl<'a, R: Reader+Buffer> Decoder<IoError> for DecoderReader<'a, R> {
Ok(())
}
fn read_uint(&mut self) -> IoResult<uint> {
match self.reader.read_be_u64() {
Ok(x) => Ok(x as uint),
Err(e) => Err(e)
}
self.read_u64().map(|x| x as uint)
}
fn read_u64(&mut self) -> IoResult<u64> {
self.reader.read_be_u64()
@ -40,7 +34,7 @@ impl<'a, R: Reader+Buffer> Decoder<IoError> for DecoderReader<'a, R> {
self.reader.read_u8()
}
fn read_int(&mut self) -> IoResult<int> {
self.reader.read_be_int()
self.read_i64().map(|x| x as int)
}
fn read_i64(&mut self) -> IoResult<i64> {
self.reader.read_be_i64()

View File

@ -18,7 +18,7 @@ impl <'a, W: Writer> EncoderWriter<'a, W> {
impl<'a, W: Writer> Encoder<IoError> for EncoderWriter<'a, W> {
fn emit_nil(&mut self) -> EwResult { Ok(()) }
fn emit_uint(&mut self, v: uint) -> EwResult {
self.writer.write_be_u64(v as u64)
self.emit_u64(v as u64)
}
fn emit_u64(&mut self, v: u64) -> EwResult {
self.writer.write_be_u64(v)
@ -33,7 +33,7 @@ impl<'a, W: Writer> Encoder<IoError> for EncoderWriter<'a, W> {
self.writer.write_u8(v)
}
fn emit_int(&mut self, v: int) -> EwResult {
self.writer.write_be_int(v)
self.emit_i64(v as i64)
}
fn emit_i64(&mut self, v: i64) -> EwResult {
self.writer.write_be_i64(v)