use crate::{value::WithType, Value};
use core::{iter, slice};
use wasmi_core::UntypedValue;
pub trait CallParams {
type Params: ExactSizeIterator<Item = UntypedValue>;
fn call_params(self) -> Self::Params;
}
impl<'a> CallParams for &'a [Value] {
type Params = CallParamsValueIter<'a>;
#[inline]
fn call_params(self) -> Self::Params {
CallParamsValueIter {
iter: self.iter().cloned(),
}
}
}
#[derive(Debug)]
pub struct CallParamsValueIter<'a> {
iter: iter::Cloned<slice::Iter<'a, Value>>,
}
impl<'a> Iterator for CallParamsValueIter<'a> {
type Item = UntypedValue;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(UntypedValue::from)
}
}
impl ExactSizeIterator for CallParamsValueIter<'_> {}
pub trait CallResults {
type Results;
fn call_results(self, results: &[UntypedValue]) -> Self::Results;
}
impl<'a> CallResults for &'a mut [Value] {
type Results = ();
fn call_results(self, results: &[UntypedValue]) -> Self::Results {
assert_eq!(self.len(), results.len());
self.iter_mut().zip(results).for_each(|(dst, src)| {
*dst = src.with_type(dst.ty());
})
}
}