From b2b39468325b9ed7043fed48a15a77b83b13d63e Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Sat, 11 May 2024 21:07:41 +0200 Subject: chore: simplify code Signed-off-by: Henry Gressmann --- crates/parser/src/conversion.rs | 52 ++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) (limited to 'crates/parser/src/conversion.rs') diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index e94986d..4e4434b 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -83,12 +83,11 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result Some(max.try_into().map_err(|_| { crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {}", max)) - })?) - } else { - None + })?), + None => None, }, }), wasmparser::TypeRef::Memory(ty) => ImportKind::Memory(convert_module_memory(ty)?), @@ -105,10 +104,7 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result>>( memory_types: T, ) -> Result> { - let memory_type = - memory_types.into_iter().map(|memory| convert_module_memory(memory?)).collect::>>()?; - - Ok(memory_type) + memory_types.into_iter().map(|memory| convert_module_memory(memory?)).collect::>>() } pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result { @@ -125,26 +121,23 @@ pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result>>>( table_types: T, ) -> Result> { - let table_type = table_types.into_iter().map(|table| convert_module_table(table?)).collect::>>()?; - Ok(table_type) + table_types.into_iter().map(|table| convert_module_table(table?)).collect::>>() } pub(crate) fn convert_module_table(table: wasmparser::Table<'_>) -> Result { - let ty = convert_reftype(&table.ty.element_type); - let size_initial = table.ty.initial.try_into().map_err(|_| { crate::ParseError::UnsupportedOperator(format!("Table size initial is too large: {}", table.ty.initial)) })?; - let size_max = if let Some(max) = table.ty.maximum { - Some( + + let size_max = match table.ty.maximum { + Some(max) => Some( max.try_into() .map_err(|_| crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {}", max)))?, - ) - } else { - None + ), + None => None, }; - Ok(TableType { element_type: ty, size_initial: size_initial, size_max }) + Ok(TableType { element_type: convert_reftype(&table.ty.element_type), size_initial: size_initial, size_max }) } pub(crate) fn convert_module_globals<'a, T: IntoIterator>>>( @@ -208,12 +201,8 @@ pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result )); } let ty = types.next().unwrap().unwrap_func(); - - let params = - ty.params().iter().map(|p| Ok(convert_valtype(p))).collect::>>()?.into_boxed_slice(); - - let results = - ty.results().iter().map(|p| Ok(convert_valtype(p))).collect::>>()?.into_boxed_slice(); + let params = ty.params().iter().map(convert_valtype).collect::>().into_boxed_slice(); + let results = ty.results().iter().map(convert_valtype).collect::>().into_boxed_slice(); Ok(FuncType { params, results }) } @@ -235,14 +224,13 @@ pub(crate) fn convert_reftype(reftype: &wasmparser::RefType) -> ValType { } pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType { - use wasmparser::ValType::*; match valtype { - I32 => ValType::I32, - I64 => ValType::I64, - F32 => ValType::F32, - F64 => ValType::F64, - Ref(r) => convert_reftype(r), - V128 => unimplemented!("128-bit values are not supported yet"), + wasmparser::ValType::I32 => ValType::I32, + wasmparser::ValType::I64 => ValType::I64, + wasmparser::ValType::F32 => ValType::F32, + wasmparser::ValType::F64 => ValType::F64, + wasmparser::ValType::Ref(r) => convert_reftype(r), + wasmparser::ValType::V128 => unimplemented!("128-bit values are not supported yet"), } } -- cgit v1.3.1