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