Files
wasmtime/misc/wasmtime-rust/src/lib.rs
Alex Crichton d1b1500d19 Add an example #[wasmtime] Rust macro (#283)
This commit adds a `wasmtime-rust` crate to the `misc` folder next to
the previously added Python extension. The intention is that this
showcases loading a WebAssembly file natively in Rust and how with an
attribute macro it can feel lightweight in terms of boilerplate.

The macro itself is pretty non-featureful today beyond the bare bones to
get anything working, but there's all sorts of possibilities like
JIT-compiled entry stubs we could eventually do with all the type
information!
2019-08-19 19:45:42 +02:00

48 lines
1.3 KiB
Rust

pub use wasmtime_rust_macro::wasmtime;
// modules used by the macro
#[doc(hidden)]
pub mod __rt {
pub use cranelift_codegen;
pub use cranelift_native;
pub use failure;
pub use wasmtime_interface_types;
pub use wasmtime_jit;
use std::convert::{TryFrom, TryInto};
use wasmtime_interface_types::Value;
pub trait FromVecValue: Sized {
fn from(list: Vec<Value>) -> Result<Self, failure::Error>;
}
macro_rules! tuple {
($(($($a:ident),*),)*) => ($(
impl<$($a: TryFrom<Value>),*> FromVecValue for ($($a,)*)
where $(failure::Error: From<$a::Error>,)*
{
#[allow(non_snake_case)]
fn from(list: Vec<Value>) -> Result<Self, failure::Error> {
let mut iter = list.into_iter();
$(
let $a = iter.next()
.ok_or_else(|| failure::format_err!("not enough values"))?
.try_into()?;
)*
if iter.next().is_some() {
failure::format_err!("too many return values");
}
Ok(($($a,)*))
}
}
)*)
}
tuple! {
(),
(A),
(A, B),
(A, B, C),
}
}