mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-29 22:51:56 +00:00
serialize buffer takes slice
This commit is contained in:
@ -120,11 +120,11 @@ pub fn deserialize_file<P: AsRef<::std::path::Path>>(p: P) -> Result<Module, Err
|
||||
let mut contents = Vec::new();
|
||||
::std::fs::File::open(p)?.read_to_end(&mut contents)?;
|
||||
|
||||
deserialize_buffer(contents)
|
||||
deserialize_buffer(&contents)
|
||||
}
|
||||
|
||||
/// 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);
|
||||
T::deserialize(&mut reader)
|
||||
}
|
||||
|
@ -266,7 +266,7 @@ mod integration_tests {
|
||||
let module = deserialize_file("./res/cases/v1/test5.wasm").expect("Should be deserialized");
|
||||
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");
|
||||
|
||||
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 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");
|
||||
assert_eq!(
|
||||
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 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");
|
||||
assert_eq!(
|
||||
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 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");
|
||||
assert_eq!(
|
||||
module_old.code_section().expect("code section exists").bodies().len(),
|
||||
|
@ -1195,7 +1195,7 @@ impl Serialize for InitExpr {
|
||||
#[test]
|
||||
fn ifelse() {
|
||||
// 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");
|
||||
let opcodes = opcode.elements();
|
||||
match &opcodes[0] {
|
||||
|
@ -571,7 +571,7 @@ mod tests {
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@ -588,7 +588,7 @@ mod tests {
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@ -605,7 +605,7 @@ mod tests {
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@ -622,7 +622,7 @@ mod tests {
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@ -726,7 +726,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn counted_list() {
|
||||
let payload = vec![
|
||||
let payload = [
|
||||
133u8, //(128+5), length is 5
|
||||
0x80, 0x80, 0x80, 0x0, // padding
|
||||
0x01,
|
||||
@ -737,7 +737,7 @@ mod tests {
|
||||
];
|
||||
|
||||
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();
|
||||
assert_eq!(5, vars.len());
|
||||
|
@ -763,8 +763,8 @@ mod tests {
|
||||
assert!(found, "There should be import section in test5.wasm");
|
||||
}
|
||||
|
||||
fn functions_test_payload() -> Vec<u8> {
|
||||
vec![
|
||||
fn functions_test_payload() -> &'static [u8] {
|
||||
&[
|
||||
// functions section id
|
||||
0x03u8,
|
||||
// functions section length
|
||||
@ -825,8 +825,8 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn types_test_payload() -> Vec<u8> {
|
||||
vec![
|
||||
fn types_test_payload() -> &'static [u8] {
|
||||
&[
|
||||
// section length
|
||||
148u8, 0x80, 0x80, 0x80, 0x0,
|
||||
|
||||
@ -875,8 +875,8 @@ mod tests {
|
||||
assert_eq!(2, t1.params().len());
|
||||
}
|
||||
|
||||
fn export_payload() -> Vec<u8> {
|
||||
vec![
|
||||
fn export_payload() -> &'static [u8] {
|
||||
&[
|
||||
// section id
|
||||
0x07,
|
||||
// section length
|
||||
@ -913,8 +913,8 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn code_payload() -> Vec<u8> {
|
||||
vec![
|
||||
fn code_payload() -> &'static [u8] {
|
||||
&[
|
||||
// sectionid
|
||||
0x0Au8,
|
||||
// section length, 32
|
||||
@ -957,8 +957,8 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn data_payload() -> Vec<u8> {
|
||||
vec![
|
||||
fn data_payload() -> &'static [u8] {
|
||||
&[
|
||||
0x0bu8, // section id
|
||||
19, // 19 bytes overall
|
||||
0x01, // number of segments
|
||||
@ -1060,7 +1060,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
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 {
|
||||
} else {
|
||||
panic!("Payload should be a start section");
|
||||
|
Reference in New Issue
Block a user