Files
wasmtime/crates/misc/rust/src/lib.rs
Jakub Konka 9182971697 Add missing import to wasmtime-rust macro (#589)
This commit does two things: 1) it fixes `wasmtime_rust::wasmtime` proc macro by
adding the missing import to the `__rt` module, and fixing the scoping inside
the macro itself; and 2) it augments the `wasmtime_rust::wasmtime` proc macro with
custom error messages in case the implementor forgets the `self` argument in the
trait methods.
2019-11-18 10:39:40 -06: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 anyhow;
pub use wasmtime_api;
pub use wasmtime_interface_types;
pub use wasmtime_jit;
pub use wasmtime_wasi;
use std::convert::{TryFrom, TryInto};
use wasmtime_interface_types::Value;
pub trait FromVecValue: Sized {
fn from(list: Vec<Value>) -> anyhow::Result<Self>;
}
macro_rules! tuple {
($(($($a:ident),*),)*) => ($(
impl<$($a: TryFrom<Value>),*> FromVecValue for ($($a,)*)
where $(anyhow::Error: From<$a::Error>,)*
{
#[allow(non_snake_case)]
fn from(list: Vec<Value>) -> anyhow::Result<Self> {
let mut iter = list.into_iter();
$(
let $a = iter.next()
.ok_or_else(|| anyhow::format_err!("not enough values"))?
.try_into()?;
)*
if iter.next().is_some() {
anyhow::bail!("too many return values");
}
Ok(($($a,)*))
}
}
)*)
}
tuple! {
(),
(A),
(A, B),
(A, B, C),
}
}