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
48
49
50
51
52
53
54
55
56
57
use crate::{
func::{Func, UnguardedFunc},
store::{Handle, StoreId},
};
/// A nullable reference to a [`Func`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct FuncRef(pub(crate) Option<Func>);
impl FuncRef {
/// Creates a new [`FuncRef`].
pub fn new(func: impl Into<Option<Func>>) -> Self {
Self(func.into())
}
/// Creates a null [`FuncRef`].
pub fn null() -> Self {
Self(None)
}
/// Returns `true` if this [`FuncRef`] is null.
pub fn is_null(self) -> bool {
self.0.is_none()
}
/// Returns the underlying [`Func`] if this [`FuncRef`] is not null.
pub fn get(self) -> Option<Func> {
self.0
}
/// Converts the given [`UnguardedFuncRef`] to a [`FuncRef`].
///
/// # Safety
///
/// The [[`UnguardedFuncRef`] must be owned by the [`Store`] with the given [`StoreId`].
pub(crate) unsafe fn from_unguarded(func: UnguardedFuncRef, store_id: StoreId) -> Self {
Self(func.map(|func| unsafe { Func(Handle::from_unguarded(func, store_id)) }))
}
/// Converts this [`FuncRef`] to an [`UnguardedFuncRef`].
///
/// # Panics
///
/// This [`FuncRef`] is not owned by the [`Store`] with the given [`StoreId`].
pub(crate) fn to_unguarded(self, store_id: StoreId) -> UnguardedFuncRef {
self.0.map(|func| func.0.to_unguarded(store_id))
}
}
impl From<Func> for FuncRef {
fn from(func: Func) -> Self {
Self::new(func)
}
}
/// An unguarded [`FuncRef`].
pub(crate) type UnguardedFuncRef = Option<UnguardedFunc>;