Function wasmer_interface_types_fl::to_interface_value[][src]

pub fn to_interface_value<T>(value: &T) -> Result<IValue, SerializeError> where
    T: Serialize
Expand description

Serialize a type T that implements the Serialize trait to an IValue.

This is not a requirement to use WIT, but Serde provides an even nicer API to the user to send its complex types to WIT.

Example

use wasmer_interface_types::{
    values::{IValue, to_interface_value},
    vec1::Vec1,
};
use serde::Serialize;

#[derive(Serialize)]
struct S(i32, i64);

#[derive(Serialize)]
struct T {
    x: String,
    s: S,
    y: f32,
};

let input = T {
    x: "abc".to_string(),
    s: S(1, 2),
    y: 3.,
};

assert_eq!(
    to_interface_value(&input).unwrap(),
    IValue::Record(NEVec::new(vec![
        IValue::String("abc".to_string()),
        IValue::Record(NEVec::new(vec![IValue::I32(1), IValue::I64(2)]).unwrap()),
        IValue::F32(3.),
    ]).unwrap()),
);