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 the memory example
use anyhow::{bail, ensure, Context as _, Error};
use std::cell::Ref;
use wasmtime::*;
fn get_export_memory(exports: &[Extern], i: usize) -> Result<HostRef<Memory>, Error> {
@@ -92,12 +91,11 @@ fn main() -> Result<(), Error> {
// Instantiate.
println!("Instantiating module...");
let instance =
HostRef::new(Instance::new(&store, &module, &[]).context("> Error instantiating module!")?);
let instance = Instance::new(&store, &module, &[]).context("> Error instantiating module!")?;
// Extract export.
println!("Extracting export...");
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
let exports = instance.exports();
ensure!(!exports.is_empty(), "> Error accessing exports!");
let memory = get_export_memory(&exports, 0)?;
let size_func = get_export_func(&exports, 1)?;