linera_witty/runtime/wasmer/
results.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Representation of Wasmer function result types.
5
6use frunk::{hlist, hlist_pat, HList};
7use wasmer::{FromToNativeWasmType, WasmTypeList};
8
9use crate::{memory_layout::FlatLayout, primitive_types::FlatType};
10
11/// Conversions between flat layouts and Wasmer function result types.
12pub trait WasmerResults: FlatLayout {
13    /// The type Wasmer uses to represent the results.
14    type Results: WasmTypeList;
15
16    /// Converts from Wasmer's representation into a flat layout.
17    fn from_wasmer(results: Self::Results) -> Self;
18
19    /// Converts from this flat layout into Wasmer's representation.
20    fn into_wasmer(self) -> Self::Results;
21}
22
23impl WasmerResults for HList![] {
24    type Results = ();
25
26    fn from_wasmer((): Self::Results) -> Self::Flat {
27        hlist![]
28    }
29
30    fn into_wasmer(self) -> Self::Results {}
31}
32
33impl<T> WasmerResults for HList![T]
34where
35    T: FlatType + FromToNativeWasmType,
36{
37    type Results = T;
38
39    fn from_wasmer(value: Self::Results) -> Self {
40        hlist![value]
41    }
42
43    fn into_wasmer(self) -> Self::Results {
44        let hlist_pat![value] = self;
45        value
46    }
47}