wion/values/
convert.rs

1use super::*;
2
3impl From<u8> for WasiValue {
4    fn from(value: u8) -> Self {
5        Self::Unsigned8(value)
6    }
7}
8
9impl From<u16> for WasiValue {
10    fn from(value: u16) -> Self {
11        Self::Unsigned16(value)
12    }
13}
14
15impl From<u32> for WasiValue {
16    fn from(value: u32) -> Self {
17        Self::Unsigned32(value)
18    }
19}
20
21impl From<u64> for WasiValue {
22    fn from(value: u64) -> Self {
23        Self::Unsigned64(value)
24    }
25}
26
27impl From<i8> for WasiValue {
28    fn from(value: i8) -> Self {
29        Self::Integer8(value)
30    }
31}
32
33impl From<i16> for WasiValue {
34    fn from(value: i16) -> Self {
35        Self::Integer16(value)
36    }
37}
38
39impl From<i32> for WasiValue {
40    fn from(value: i32) -> Self {
41        Self::Integer32(value)
42    }
43}
44
45impl From<i64> for WasiValue {
46    fn from(value: i64) -> Self {
47        Self::Integer64(value)
48    }
49}
50
51impl From<f32> for WasiValue {
52    fn from(value: f32) -> Self {
53        Self::Float32(value)
54    }
55}
56
57impl From<f64> for WasiValue {
58    fn from(value: f64) -> Self {
59        Self::Float64(value)
60    }
61}
62
63impl WasiValue {
64    /// Check if the value is a boolean
65    pub fn is_bool(&self) -> bool {
66        self.as_bool().is_some()
67    }
68    /// Check if the value is an 8-bit unsigned integer
69    pub fn is_u8(&self) -> bool {
70        self.as_u8().is_some()
71    }
72    /// Check if the value is a 16-bit unsigned integer
73    pub fn is_u16(&self) -> bool {
74        self.as_u16().is_some()
75    }
76    /// Check if the value is a 32-bit unsigned integer
77    pub fn is_u32(&self) -> bool {
78        self.as_u32().is_some()
79    }
80    /// Check if the value is a 64-bit unsigned integer
81    pub fn is_u64(&self) -> bool {
82        self.as_u64().is_some()
83    }
84    /// Check if the value is an 8-bit signed integer
85    pub fn is_i8(&self) -> bool {
86        self.as_i8().is_some()
87    }
88    /// Check if the value is a 16-bit signed integer
89    pub fn is_i16(&self) -> bool {
90        self.as_i16().is_some()
91    }
92    /// Check if the value is a 32-bit signed integer
93    pub fn is_i32(&self) -> bool {
94        self.as_i32().is_some()
95    }
96    /// Check if the value is a 64-bit signed integer
97    pub fn is_i64(&self) -> bool {
98        self.as_i64().is_some()
99    }
100    /// Check if the value is a 32-bit floating point number
101    pub fn is_f32(&self) -> bool {
102        self.as_f32().is_some()
103    }
104    /// Check if the value is a 64-bit floating point number
105    pub fn is_f64(&self) -> bool {
106        self.as_f64().is_some()
107    }
108    /// Check if the value is a unicode character
109    pub fn is_unicode(&self) -> bool {
110        self.as_unicode().is_some()
111    }
112    /// Check if the value is a UTF-8 string
113    pub fn is_utf8(&self) -> bool {
114        self.as_utf8().is_some()
115    }
116    /// Check if the value is a record
117    pub fn is_structure(&self) -> bool {
118        self.as_structure().is_some()
119    }
120}
121impl WasiValue {
122    /// Try to convert the value to a boolean
123    pub fn as_bool(&self) -> Option<bool> {
124        match self {
125            Self::Boolean(v) => Some(*v),
126            _ => None,
127        }
128    }
129    /// Try to convert the value to an 8-bit unsigned integer
130    pub fn as_u8(&self) -> Option<u8> {
131        match self {
132            Self::Unsigned8(v) => Some(*v),
133            _ => None,
134        }
135    }
136    /// Try to convert the value to a 16-bit unsigned integer
137    pub fn as_u16(&self) -> Option<u16> {
138        match self {
139            Self::Unsigned16(v) => Some(*v),
140            _ => None,
141        }
142    }
143    /// Try to convert the value to a 32-bit unsigned integer
144    pub fn as_u32(&self) -> Option<u32> {
145        match self {
146            Self::Unsigned32(v) => Some(*v),
147            _ => None,
148        }
149    }
150    /// Try to convert the value to a 64-bit unsigned integer
151    pub fn as_u64(&self) -> Option<u64> {
152        match self {
153            Self::Unsigned64(v) => Some(*v),
154            _ => None,
155        }
156    }
157    /// Try to convert the value to an 8-bit signed integer
158    pub fn as_i8(&self) -> Option<i8> {
159        match self {
160            Self::Integer8(v) => Some(*v),
161            _ => None,
162        }
163    }
164    /// Try to convert the value to a 16-bit signed integer
165    pub fn as_i16(&self) -> Option<i16> {
166        match self {
167            Self::Integer16(v) => Some(*v),
168            _ => None,
169        }
170    }
171    /// Try to convert the value to a 32-bit signed integer
172    pub fn as_i32(&self) -> Option<i32> {
173        match self {
174            Self::Integer32(v) => Some(*v),
175            _ => None,
176        }
177    }
178    /// Try to convert the value to a 64-bit signed integer
179    pub fn as_i64(&self) -> Option<i64> {
180        match self {
181            Self::Integer64(v) => Some(*v),
182            _ => None,
183        }
184    }
185    /// Try to convert the value to a 32-bit floating point number
186    pub fn as_f32(&self) -> Option<f32> {
187        match self {
188            Self::Float32(v) => Some(*v),
189            _ => None,
190        }
191    }
192    /// Try to convert the value to a 64-bit floating point number
193    pub fn as_f64(&self) -> Option<f64> {
194        match self {
195            Self::Float64(v) => Some(*v),
196            _ => None,
197        }
198    }
199    /// Try to convert the value to a unicode character
200    pub fn as_unicode(&self) -> Option<char> {
201        match self {
202            Self::Unicode(v) => Some(*v),
203            _ => None,
204        }
205    }
206    /// Try to convert the value to a UTF-8 string
207    pub fn as_utf8(&self) -> Option<&str> {
208        match self {
209            Self::UTF8(v) => Some(v),
210            _ => None,
211        }
212    }
213    /// Try to convert the value to a record
214    pub fn as_structure(&self) -> Option<&WasiObject> {
215        match self {
216            Self::Object(v) => Some(v),
217            _ => None,
218        }
219    }
220}