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 Wasmtime function result types.

use frunk::{hlist, hlist_pat, HList};
use wasmtime::{WasmResults, WasmTy};

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

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

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

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

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

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

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

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

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

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