submilli_wasm/value/
type_access.rs1use super::gc_type::{ArrayType, StructType};
6use super::tag_type::TagType;
7use super::types::{
8 ExternType, FuncType, GlobalType, HeapType, MemoryType, RefType, TableType, ValType,
9};
10
11impl ValType {
12 pub fn is_ref(&self) -> bool {
14 matches!(self, ValType::Ref(_))
15 }
16
17 #[allow(clippy::should_implement_trait)]
20 pub fn as_ref(&self) -> Option<&RefType> {
21 match self {
22 ValType::Ref(r) => Some(r),
23 _ => None,
24 }
25 }
26
27 pub fn unwrap_ref(&self) -> &RefType {
29 self.as_ref().expect("expected a reference type")
30 }
31}
32
33macro_rules! extern_type_accessors {
34 ($(($variant:ident($ty:ty) $get:ident $unwrap:ident))*) => ($(
35 pub fn $get(&self) -> Option<&$ty> {
37 if let ExternType::$variant(e) = self {
38 Some(e)
39 } else {
40 None
41 }
42 }
43
44 pub fn $unwrap(&self) -> &$ty {
46 self.$get().expect(concat!("expected ", stringify!($ty)))
47 }
48 )*)
49}
50
51impl ExternType {
52 extern_type_accessors! {
53 (Func(FuncType) func unwrap_func)
54 (Global(GlobalType) global unwrap_global)
55 (Table(TableType) table unwrap_table)
56 (Memory(MemoryType) memory unwrap_memory)
57 (Tag(TagType) tag unwrap_tag)
58 }
59}
60
61impl HeapType {
62 pub fn as_concrete_struct(&self) -> Option<&StructType> {
64 match self {
65 HeapType::ConcreteStruct(t) => Some(t),
66 _ => None,
67 }
68 }
69
70 pub fn unwrap_concrete_struct(&self) -> &StructType {
72 self.as_concrete_struct()
73 .expect("expected a concrete struct heap type")
74 }
75
76 pub fn as_concrete_array(&self) -> Option<&ArrayType> {
78 match self {
79 HeapType::ConcreteArray(t) => Some(t),
80 _ => None,
81 }
82 }
83
84 pub fn unwrap_concrete_array(&self) -> &ArrayType {
86 self.as_concrete_array()
87 .expect("expected a concrete array heap type")
88 }
89
90 pub fn as_concrete_func(&self) -> Option<&FuncType> {
92 match self {
93 HeapType::ConcreteFunc(t) => Some(t),
94 _ => None,
95 }
96 }
97
98 pub fn unwrap_concrete_func(&self) -> &FuncType {
100 self.as_concrete_func()
101 .expect("expected a concrete func heap type")
102 }
103}