serialize buffer takes slice

This commit is contained in:
NikVolf
2017-12-27 12:28:42 +03:00
parent 138e35da3b
commit 97257a7865
5 changed files with 24 additions and 24 deletions

View File

@ -120,11 +120,11 @@ pub fn deserialize_file<P: AsRef<::std::path::Path>>(p: P) -> Result<Module, Err
let mut contents = Vec::new(); let mut contents = Vec::new();
::std::fs::File::open(p)?.read_to_end(&mut contents)?; ::std::fs::File::open(p)?.read_to_end(&mut contents)?;
deserialize_buffer(contents) deserialize_buffer(&contents)
} }
/// Deserialize deserializable type from buffer. /// Deserialize deserializable type from buffer.
pub fn deserialize_buffer<T: Deserialize>(contents: Vec<u8>) -> Result<T, T::Error> { pub fn deserialize_buffer<T: Deserialize>(contents: &[u8]) -> Result<T, T::Error> {
let mut reader = io::Cursor::new(contents); let mut reader = io::Cursor::new(contents);
T::deserialize(&mut reader) T::deserialize(&mut reader)
} }

View File

@ -266,7 +266,7 @@ mod integration_tests {
let module = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized"); let module = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized");
let buf = serialize(module).expect("serialization to succeed"); let buf = serialize(module).expect("serialization to succeed");
let module_new: Module = deserialize_buffer(buf).expect("deserialization to succeed"); let module_new: Module = deserialize_buffer(&buf).expect("deserialization to succeed");
let module_old = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized"); let module_old = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized");
assert_eq!(module_old.sections().len(), module_new.sections().len()); assert_eq!(module_old.sections().len(), module_new.sections().len());
@ -281,7 +281,7 @@ mod integration_tests {
let buf = serialize(module).expect("serialization to succeed"); let buf = serialize(module).expect("serialization to succeed");
let module_new: Module = deserialize_buffer(buf).expect("deserialization to succeed"); let module_new: Module = deserialize_buffer(&buf).expect("deserialization to succeed");
let module_old = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized"); let module_old = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized");
assert_eq!( assert_eq!(
module_old.type_section().expect("type section exists").types().len(), module_old.type_section().expect("type section exists").types().len(),
@ -299,7 +299,7 @@ mod integration_tests {
let buf = serialize(module).expect("serialization to succeed"); let buf = serialize(module).expect("serialization to succeed");
let module_new: Module = deserialize_buffer(buf).expect("deserialization to succeed"); let module_new: Module = deserialize_buffer(&buf).expect("deserialization to succeed");
let module_old = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized"); let module_old = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized");
assert_eq!( assert_eq!(
module_old.import_section().expect("import section exists").entries().len(), module_old.import_section().expect("import section exists").entries().len(),
@ -317,7 +317,7 @@ mod integration_tests {
let buf = serialize(module).expect("serialization to succeed"); let buf = serialize(module).expect("serialization to succeed");
let module_new: Module = deserialize_buffer(buf).expect("deserialization to succeed"); let module_new: Module = deserialize_buffer(&buf).expect("deserialization to succeed");
let module_old = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized"); let module_old = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized");
assert_eq!( assert_eq!(
module_old.code_section().expect("code section exists").bodies().len(), module_old.code_section().expect("code section exists").bodies().len(),

View File

@ -1195,7 +1195,7 @@ impl Serialize for InitExpr {
#[test] #[test]
fn ifelse() { fn ifelse() {
// see if-else.wast/if-else.wasm // see if-else.wast/if-else.wasm
let opcode = super::deserialize_buffer::<Opcodes>(vec![0x04, 0x7F, 0x41, 0x05, 0x05, 0x41, 0x07, 0x0B, 0x0B]) let opcode = super::deserialize_buffer::<Opcodes>(&[0x04, 0x7F, 0x41, 0x05, 0x05, 0x41, 0x07, 0x0B, 0x0B])
.expect("valid hex of if instruction"); .expect("valid hex of if instruction");
let opcodes = opcode.elements(); let opcodes = opcode.elements();
match &opcodes[0] { match &opcodes[0] {

View File

@ -571,7 +571,7 @@ mod tests {
} }
fn varuint32_de_test(dt: Vec<u8>, expected: u32) { fn varuint32_de_test(dt: Vec<u8>, expected: u32) {
let val: VarUint32 = super::super::deserialize_buffer(dt).expect("buf to be serialized"); let val: VarUint32 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
assert_eq!(expected, val.into()); assert_eq!(expected, val.into());
} }
@ -588,7 +588,7 @@ mod tests {
} }
fn varint32_de_test(dt: Vec<u8>, expected: i32) { fn varint32_de_test(dt: Vec<u8>, expected: i32) {
let val: VarInt32 = super::super::deserialize_buffer(dt).expect("buf to be serialized"); let val: VarInt32 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
assert_eq!(expected, val.into()); assert_eq!(expected, val.into());
} }
@ -605,7 +605,7 @@ mod tests {
} }
fn varuint64_de_test(dt: Vec<u8>, expected: u64) { fn varuint64_de_test(dt: Vec<u8>, expected: u64) {
let val: VarUint64 = super::super::deserialize_buffer(dt).expect("buf to be serialized"); let val: VarUint64 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
assert_eq!(expected, val.into()); assert_eq!(expected, val.into());
} }
@ -622,7 +622,7 @@ mod tests {
} }
fn varint64_de_test(dt: Vec<u8>, expected: i64) { fn varint64_de_test(dt: Vec<u8>, expected: i64) {
let val: VarInt64 = super::super::deserialize_buffer(dt).expect("buf to be serialized"); let val: VarInt64 = super::super::deserialize_buffer(&dt).expect("buf to be serialized");
assert_eq!(expected, val.into()); assert_eq!(expected, val.into());
} }
@ -726,7 +726,7 @@ mod tests {
#[test] #[test]
fn counted_list() { fn counted_list() {
let payload = vec![ let payload = [
133u8, //(128+5), length is 5 133u8, //(128+5), length is 5
0x80, 0x80, 0x80, 0x0, // padding 0x80, 0x80, 0x80, 0x0, // padding
0x01, 0x01,
@ -737,7 +737,7 @@ mod tests {
]; ];
let list: CountedList<VarInt7> = let list: CountedList<VarInt7> =
deserialize_buffer(payload).expect("type_section be deserialized"); deserialize_buffer(&payload).expect("type_section be deserialized");
let vars = list.into_inner(); let vars = list.into_inner();
assert_eq!(5, vars.len()); assert_eq!(5, vars.len());

View File

@ -763,8 +763,8 @@ mod tests {
assert!(found, "There should be import section in test5.wasm"); assert!(found, "There should be import section in test5.wasm");
} }
fn functions_test_payload() -> Vec<u8> { fn functions_test_payload() -> &'static [u8] {
vec![ &[
// functions section id // functions section id
0x03u8, 0x03u8,
// functions section length // functions section length
@ -825,8 +825,8 @@ mod tests {
} }
} }
fn types_test_payload() -> Vec<u8> { fn types_test_payload() -> &'static [u8] {
vec![ &[
// section length // section length
148u8, 0x80, 0x80, 0x80, 0x0, 148u8, 0x80, 0x80, 0x80, 0x0,
@ -875,8 +875,8 @@ mod tests {
assert_eq!(2, t1.params().len()); assert_eq!(2, t1.params().len());
} }
fn export_payload() -> Vec<u8> { fn export_payload() -> &'static [u8] {
vec![ &[
// section id // section id
0x07, 0x07,
// section length // section length
@ -913,8 +913,8 @@ mod tests {
} }
} }
fn code_payload() -> Vec<u8> { fn code_payload() -> &'static [u8] {
vec![ &[
// sectionid // sectionid
0x0Au8, 0x0Au8,
// section length, 32 // section length, 32
@ -957,8 +957,8 @@ mod tests {
} }
} }
fn data_payload() -> Vec<u8> { fn data_payload() -> &'static [u8] {
vec![ &[
0x0bu8, // section id 0x0bu8, // section id
19, // 19 bytes overall 19, // 19 bytes overall
0x01, // number of segments 0x01, // number of segments
@ -1060,7 +1060,7 @@ mod tests {
#[test] #[test]
fn start_section() { fn start_section() {
let section: Section = deserialize_buffer(vec![08u8, 01u8, 00u8]).expect("Start section to deserialize"); let section: Section = deserialize_buffer(&[08u8, 01u8, 00u8]).expect("Start section to deserialize");
if let Section::Start(_) = section { if let Section::Start(_) = section {
} else { } else {
panic!("Payload should be a start section"); panic!("Payload should be a start section");