Added VarUint8 serialization/deserialization type.

This commit is contained in:
Simon Heath
2018-01-17 13:16:31 -05:00
parent 95c52451a7
commit 5b19c5fb94
3 changed files with 42 additions and 5 deletions

View File

@ -25,7 +25,7 @@ pub use self::import_entry::{ImportEntry, ResizableLimits, MemoryType, TableType
pub use self::export_entry::{ExportEntry, Internal};
pub use self::global_entry::GlobalEntry;
pub use self::primitives::{
VarUint32, VarUint7, VarUint1, VarInt7, Uint32, VarInt32, VarInt64,
VarUint32, VarUint7, VarUint8, VarUint1, VarInt7, Uint32, VarInt32, VarInt64,
Uint64, VarUint64, CountedList, CountedWriter, CountedListWriter,
};
pub use self::types::{Type, ValueType, BlockType, FunctionType, TableElementType};

View File

@ -1,7 +1,7 @@
use std::{io, fmt};
use super::{
Serialize, Deserialize, Error, VarUint7,
VarUint32, CountedList, BlockType,
VarUint8, VarUint32, CountedList, BlockType,
Uint32, Uint64, CountedListWriter,
VarInt32, VarInt64,
};
@ -346,7 +346,7 @@ impl Deserialize for Opcode {
0x10 => Call(VarUint32::deserialize(reader)?.into()),
0x11 => CallIndirect(
VarUint32::deserialize(reader)?.into(),
VarUint7::deserialize(reader)?.into()),
VarUint8::deserialize(reader)?.into()),
0x1a => Drop,
0x1b => Select,
@ -449,8 +449,8 @@ impl Deserialize for Opcode {
VarUint32::deserialize(reader)?.into()),
0x3f => CurrentMemory(VarUint7::deserialize(reader)?.into()),
0x40 => GrowMemory(VarUint7::deserialize(reader)?.into()),
0x3f => CurrentMemory(VarUint8::deserialize(reader)?.into()),
0x40 => GrowMemory(VarUint8::deserialize(reader)?.into()),
0x41 => I32Const(VarInt32::deserialize(reader)?.into()),
0x42 => I64Const(VarInt64::deserialize(reader)?.into()),

View File

@ -206,6 +206,43 @@ impl Serialize for VarInt7 {
}
}
/// 8-bit unsigned integer, NOT encoded in LEB128;
/// it's just a single byte.
#[derive(Debug, Copy, Clone)]
pub struct VarUint8(u8);
impl From<VarUint8> for u8 {
fn from(v: VarUint8) -> u8 {
v.0
}
}
impl From<u8> for VarUint8 {
fn from(v: u8) -> Self {
VarUint8(v)
}
}
impl Deserialize for VarUint8 {
type Error = Error;
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
let mut u8buf = [0u8; 1];
reader.read_exact(&mut u8buf)?;
Ok(VarUint8(u8buf[0]))
}
}
impl Serialize for VarUint8 {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
writer.write_all(&[self.0])?;
Ok(())
}
}
/// 32-bit signed integer, encoded in LEB128 (can be 1-5 bytes length)
#[derive(Debug, Copy, Clone)]
pub struct VarInt32(i32);