Skip to main content

wasmedge_sys/instance/
mod.rs

1//! Defines WasmEdge instance structs, including Function, Global, Memory, Table, and etc.
2
3pub mod function;
4pub mod global;
5pub mod memory;
6pub mod module;
7pub mod table;
8
9use std::{
10    fmt::Debug,
11    ops::{Deref, DerefMut},
12};
13
14#[doc(hidden)]
15pub use function::Function;
16#[doc(hidden)]
17pub use global::Global;
18#[doc(hidden)]
19pub use memory::Memory;
20#[doc(hidden)]
21pub use module::Instance;
22#[doc(hidden)]
23pub use table::Table;
24pub use wasmedge_types::{FuncType, GlobalType, MemoryType, TableType};
25
26pub struct InnerRef<D, Ref: ?Sized> {
27    value: std::mem::ManuallyDrop<D>,
28    _ref: std::marker::PhantomData<Ref>,
29}
30
31impl<D, Ref: ?Sized> InnerRef<D, &Ref> {
32    /// # Safety
33    ///
34    /// The return value type of this function should ensure the correctness of lifetimes.
35    pub unsafe fn create_from_ref(value: std::mem::ManuallyDrop<D>, _r: &Ref) -> Self {
36        let r = Default::default();
37        Self { value, _ref: r }
38    }
39
40    /// # Safety
41    ///
42    /// The return value type of this function should ensure the correctness of lifetimes.
43    pub unsafe fn create_ref(value: std::mem::ManuallyDrop<D>) -> Self {
44        let r = Default::default();
45        Self { value, _ref: r }
46    }
47}
48
49impl<D, Ref: ?Sized> InnerRef<D, &mut Ref> {
50    /// # Safety
51    ///
52    /// The return value type of this function should ensure the correctness of lifetimes.
53    pub unsafe fn create_from_mut(value: std::mem::ManuallyDrop<D>, _r: &mut Ref) -> Self {
54        let r = Default::default();
55        Self { value, _ref: r }
56    }
57
58    /// # Safety
59    ///
60    /// The return value type of this function should ensure the correctness of lifetimes.
61    pub unsafe fn create_mut(value: std::mem::ManuallyDrop<D>) -> Self {
62        let r = Default::default();
63        Self { value, _ref: r }
64    }
65}
66
67impl<D: Debug, Ref> Debug for InnerRef<D, Ref> {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        self.value.fmt(f)
70    }
71}
72
73impl<D, Ref> Deref for InnerRef<D, &Ref> {
74    type Target = D;
75    fn deref(&self) -> &Self::Target {
76        &self.value
77    }
78}
79
80impl<D: Clone, Ref> Clone for InnerRef<D, &mut Ref> {
81    fn clone(&self) -> Self {
82        Self {
83            value: self.value.clone(),
84            _ref: self._ref,
85        }
86    }
87}
88
89impl<D, Ref> Deref for InnerRef<D, &mut Ref> {
90    type Target = D;
91    fn deref(&self) -> &Self::Target {
92        &self.value
93    }
94}
95
96impl<D, Ref> DerefMut for InnerRef<D, &mut Ref> {
97    fn deref_mut(&mut self) -> &mut Self::Target {
98        &mut self.value
99    }
100}