From f6b6eefdac84d87ac669a21974aec001d83f730f Mon Sep 17 00:00:00 2001 From: Francesco Mazzoli Date: Sat, 3 Jan 2015 16:33:08 +0100 Subject: [PATCH] Use u32 to represent variant tags. This makes it possible to exchange binary serialized structures between different architectures, and reduces bandwidth. I doubt it's even possible to have more than 4 billions variants anyway :). --- src/reader.rs | 3 ++- src/writer.rs | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 2cc0cb1..198940e 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -76,7 +76,8 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> { } fn read_enum_variant(&mut self, names: &[&str], mut f: F) -> IoResult where F: FnMut(&mut DecoderReader<'a, R>, uint) -> IoResult { - let id = try!(self.read_uint()); + let id = try!(self.read_u32()); + let id = id as uint; if id >= names.len() { Err(IoError { kind: OtherIoError, diff --git a/src/writer.rs b/src/writer.rs index ac61687..2e0bba3 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,5 +1,6 @@ use std::io::{Writer, IoError, IoResult}; use rustc_serialize::Encoder; +use std::num::Int; type EwResult = IoResult<()>; @@ -70,7 +71,11 @@ impl<'a, W: Writer> Encoder for EncoderWriter<'a, W> { _: uint, f: F) -> EwResult where F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult { - try!(self.emit_uint(v_id)); + let max_u32: u32 = Int::max_value(); + if v_id > (max_u32 as uint) { + panic!("Variant tag doesn't fit in a u32") + } + try!(self.emit_u32(v_id as u32)); f(self) } fn emit_enum_variant_arg(&mut self, _: uint, f: F) -> EwResult where