From d27955c71e7ddf212cf71b84cba077e8b5da9db4 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Thu, 4 May 2023 16:04:31 -0700 Subject: [PATCH] try_into for Value --- src/lib.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index de095ee6..c1f3e2fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -223,10 +223,11 @@ mod wasm { } } - // TODO: try_into - impl From for Value { - fn from(value: WASMValue) -> Self { - value.0.unwrap() + impl TryFrom for Value { + type Error = WasmError; + + fn try_from(x: WASMValue) -> Result { + x.value() } } @@ -291,19 +292,22 @@ mod wasm { // We take in a reference to values, since they do not implement Copy. // We then clone them inside of this function, so that the API does not have a bunch of Clones everywhere - let params: Vec = params.into_iter().cloned().map(|val| val.into()).collect(); + let mut args: Vec = vec![]; + for param in params.into_iter().cloned() { + args.push(param.try_into()?) + } let func = self.instance.exports.get_function(name).map_err(|source| { WasmError::InvalidExport { name: name.to_string(), source, } })?; - let boxed_value = - func.call(¶ms) - .map_err(|source| WasmError::FunctionCallFailed { - name: name.to_string(), - source, - })?; + let boxed_value = func + .call(&args) + .map_err(|source| WasmError::FunctionCallFailed { + name: name.to_string(), + source, + })?; let option_value = boxed_value.first().cloned(); Ok(WASMValue(option_value))