linera_wasmer/
typed_function.rs

1//! Native Functions.
2//!
3//! This module creates the helper `TypedFunction` that let us call WebAssembly
4//! functions with the native ABI, that is:
5//!
6//! ```ignore
7//! let add_one = instance.exports.get_function("function_name")?;
8//! let add_one_native: TypedFunction<i32, i32> = add_one.native().unwrap();
9//! ```
10use crate::{Function, WasmTypeList};
11use std::marker::PhantomData;
12
13use crate::store::AsStoreRef;
14
15/// A WebAssembly function that can be called natively
16/// (using the Native ABI).
17#[derive(Clone)]
18pub struct TypedFunction<Args, Rets> {
19    pub(crate) func: Function,
20    _phantom: PhantomData<fn(Args) -> Rets>,
21}
22
23unsafe impl<Args, Rets> Send for TypedFunction<Args, Rets> {}
24unsafe impl<Args, Rets> Sync for TypedFunction<Args, Rets> {}
25
26impl<Args, Rets> TypedFunction<Args, Rets>
27where
28    Args: WasmTypeList,
29    Rets: WasmTypeList,
30{
31    #[allow(dead_code)]
32    pub(crate) fn new(_store: &impl AsStoreRef, func: Function) -> Self {
33        Self {
34            func,
35            _phantom: PhantomData,
36        }
37    }
38
39    pub(crate) fn into_function(self) -> Function {
40        self.func
41    }
42}