feat(api) Add the WasmTypeList::from_slice method.

This method creates a `WasmTypeList` from a slice of `i128`, similarly
to what `from_array` does, but without knowing the size ahead of time.
This commit is contained in:
Ivan Enderlin
2020-06-26 14:43:41 +02:00
parent fffddb78a9
commit 3c2e191d6a

View File

@@ -498,7 +498,8 @@ impl<T: VMDynamicFunction> VMDynamicFunctionCall<T> for VMDynamicFunctionContext
/// This private inner module contains the low-level implementation
/// for `Function` and its siblings.
mod inner {
use std::convert::Infallible;
use std::array::TryFromSliceError;
use std::convert::{Infallible, TryInto};
use std::error::Error;
use std::marker::PhantomData;
use std::panic::{self, AssertUnwindSafe};
@@ -557,7 +558,10 @@ mod inner {
/// The `WasmTypeList` trait represents a tuple (list) of Wasm
/// typed values. It is used to get low-level representation of
/// such a tuple.
pub trait WasmTypeList {
pub trait WasmTypeList
where
Self: Sized,
{
/// The C type (a struct) that can hold/represent all the
/// represented values.
type CStruct;
@@ -570,6 +574,14 @@ mod inner {
/// Constructs `Self` based on an array of values.
fn from_array(array: Self::Array) -> Self;
/// Constructs `Self` based on a slice of values.
///
/// `from_slice` returns a `Result` because it is possible
/// that the slice doesn't have the same size than
/// `Self::Array`, in which circumstance an error of kind
/// `TryFromSliceError` will be returned.
fn from_slice(slice: &[i128]) -> Result<Self, TryFromSliceError>;
/// Builds and returns an array of type `Array` from a tuple
/// (list) of values.
fn into_array(self) -> Self::Array;
@@ -748,6 +760,10 @@ mod inner {
)
}
fn from_slice(slice: &[i128]) -> Result<Self, TryFromSliceError> {
Ok(Self::from_array(slice.try_into()?))
}
fn into_array(self) -> Self::Array {
// Unpack items of the tuple.
#[allow(non_snake_case)]
@@ -939,6 +955,10 @@ mod inner {
unreachable!()
}
fn from_slice(_: &[i128]) -> Result<Self, TryFromSliceError> {
unreachable!()
}
fn into_array(self) -> Self::Array {
[]
}