use frunk::{hlist, hlist_pat, HList};
use wasmtime::{WasmParams, WasmTy};
use crate::{primitive_types::FlatType, Layout};
pub trait WasmtimeParameters {
type Parameters: WasmParams;
fn into_wasmtime(self) -> Self::Parameters;
fn from_wasmtime(parameters: Self::Parameters) -> Self;
}
macro_rules! parameters {
($( $names:ident : $types:ident ),*) => {
impl<$( $types ),*> WasmtimeParameters for HList![$( $types ),*]
where
$( $types: FlatType + WasmTy, )*
{
type Parameters = ($( $types, )*);
#[allow(clippy::unused_unit)]
fn into_wasmtime(self) -> Self::Parameters {
let hlist_pat![$( $names ),*] = self;
($( $names, )*)
}
fn from_wasmtime(($( $names, )*): Self::Parameters) -> Self {
hlist![$( $names ),*]
}
}
};
}
repeat_macro!(parameters =>
a: A,
b: B,
c: C,
d: D,
e: E,
f: F,
g: G,
h: H,
i: I,
j: J,
k: K,
l: L,
m: M,
n: N,
o: O,
p: P,
q: Q
);
impl<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, Rest> WasmtimeParameters for HList![A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, ...Rest]
where
A: FlatType,
B: FlatType,
C: FlatType,
D: FlatType,
E: FlatType,
F: FlatType,
G: FlatType,
H: FlatType,
I: FlatType,
J: FlatType,
K: FlatType,
L: FlatType,
M: FlatType,
N: FlatType,
O: FlatType,
P: FlatType,
Q: FlatType,
R: FlatType,
Rest: Layout,
{
type Parameters = (i32,);
fn into_wasmtime(self) -> Self::Parameters {
unreachable!("Attempt to convert a list of flat parameters larger than the maximum limit");
}
fn from_wasmtime(_: Self::Parameters) -> Self {
unreachable!(
"Attempt to convert into a list of flat parameters larger than the maximum limit"
);
}
}