tinywasm_types/
reference.rs1use crate::{ExternAddr, FuncAddr};
2
3const NULL_REF: u32 = u32::MAX;
4
5#[derive(Clone, Copy, PartialEq, Eq)]
6pub struct ExternRef(u32);
7
8#[derive(Clone, Copy, PartialEq, Eq)]
9pub struct FuncRef(u32);
10
11#[cfg(feature = "debug")]
12impl core::fmt::Debug for ExternRef {
13 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
14 match self.addr() {
15 Some(addr) => write!(f, "extern({addr:?})"),
16 None => write!(f, "extern(null)"),
17 }
18 }
19}
20
21#[cfg(feature = "debug")]
22impl core::fmt::Debug for FuncRef {
23 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24 match self.addr() {
25 Some(addr) => write!(f, "func({addr:?})"),
26 None => write!(f, "func(null)"),
27 }
28 }
29}
30
31impl FuncRef {
32 #[inline]
33 pub const fn new(addr: Option<FuncAddr>) -> Self {
35 match addr {
36 Some(addr) => Self(addr),
37 None => Self::null(),
38 }
39 }
40
41 #[inline]
42 pub const fn null() -> Self {
44 Self(NULL_REF)
45 }
46
47 #[inline]
48 pub const fn is_null(&self) -> bool {
50 self.0 == NULL_REF
51 }
52
53 #[inline]
54 pub const fn addr(&self) -> Option<FuncAddr> {
56 if self.is_null() { None } else { Some(self.0) }
57 }
58
59 #[inline]
60 #[doc(hidden)]
61 pub const fn from_raw(raw: u32) -> Self {
62 Self(raw)
63 }
64
65 #[inline]
66 #[doc(hidden)]
67 pub const fn raw(&self) -> u32 {
68 self.0
69 }
70}
71
72impl ExternRef {
73 #[inline]
74 pub const fn new(addr: Option<ExternAddr>) -> Self {
77 match addr {
78 Some(addr) => Self(addr),
79 None => Self::null(),
80 }
81 }
82
83 #[inline]
85 pub const fn null() -> Self {
86 Self(NULL_REF)
87 }
88
89 #[inline]
91 pub const fn is_null(&self) -> bool {
92 self.0 == NULL_REF
93 }
94
95 #[inline]
97 pub const fn addr(&self) -> Option<ExternAddr> {
98 if self.is_null() { None } else { Some(self.0) }
99 }
100
101 #[inline]
102 #[doc(hidden)]
103 pub const fn from_raw(raw: u32) -> Self {
104 Self(raw)
105 }
106
107 #[inline]
108 #[doc(hidden)]
109 pub const fn raw(&self) -> u32 {
110 self.0
111 }
112}