linera_witty/runtime/wasmtime/
results.rs

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