1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Representation of Wasmer function result types.

use frunk::{hlist, hlist_pat, HList};
use wasmer::{FromToNativeWasmType, WasmTypeList};

use crate::{memory_layout::FlatLayout, primitive_types::FlatType};

/// Conversions between flat layouts and Wasmer function result types.
pub trait WasmerResults: FlatLayout {
    /// The type Wasmer uses to represent the results.
    type Results: WasmTypeList;

    /// Converts from Wasmer's representation into a flat layout.
    fn from_wasmer(results: Self::Results) -> Self;

    /// Converts from this flat layout into Wasmer's representation.
    fn into_wasmer(self) -> Self::Results;
}

impl WasmerResults for HList![] {
    type Results = ();

    fn from_wasmer((): Self::Results) -> Self::Flat {
        hlist![]
    }

    fn into_wasmer(self) -> Self::Results {}
}

impl<T> WasmerResults for HList![T]
where
    T: FlatType + FromToNativeWasmType,
{
    type Results = T;

    fn from_wasmer(value: Self::Results) -> Self {
        hlist![value]
    }

    fn into_wasmer(self) -> Self::Results {
        let hlist_pat![value] = self;
        value
    }
}