Simplify examples: avoid unnecessary HostRef wrap/unwrap

Several of the examples wrap the Instance in a HostRef, only to
immediately borrow it again to get the exports,and then never touch it
again. Simplify this by owning the Instance directly.
This commit is contained in:
Josh Triplett
2019-11-19 21:01:09 -08:00
committed by Jakub Konka
parent 204b4d376a
commit 7c8ac3d71c
4 changed files with 10 additions and 21 deletions

View File

@@ -1,7 +1,6 @@
//! Translation of hello example
use anyhow::{ensure, format_err, Context as _, Result};
use std::cell::Ref;
use std::rc::Rc;
use wasmtime::*;
@@ -49,14 +48,12 @@ fn main() -> Result<()> {
// Note that this is where the wasm `start` function, if any, would run.
println!("Instantiating module...");
let imports = vec![hello_func.into()];
let instance = HostRef::new(
Instance::new(&store, &module, imports.as_slice())
.context("> Error instantiating module!")?,
);
let instance = Instance::new(&store, &module, imports.as_slice())
.context("> Error instantiating module!")?;
// Next we poke around a bit to extract the `run` function from the module.
println!("Extracting export...");
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
let exports = instance.exports();
ensure!(!exports.is_empty(), "> Error accessing exports!");
let run_func = exports[0].func().context("> Error accessing exports!")?;