|
1 | 1 | use crate::{ |
2 | 2 | collections::arena::ArenaIndex, |
3 | | - core::UntypedVal, |
| 3 | + core::{ReadAs, UntypedVal, WriteAs}, |
4 | 4 | store::Stored, |
5 | 5 | AsContextMut, |
6 | 6 | Func, |
@@ -174,6 +174,39 @@ fn funcref_null_to_zero() { |
174 | 174 | macro_rules! impl_conversions { |
175 | 175 | ( $( $reftype:ty ),* $(,)? ) => { |
176 | 176 | $( |
| 177 | + impl ReadAs<$reftype> for UntypedVal { |
| 178 | + fn read_as(&self) -> $reftype { |
| 179 | + let bits = u64::from(*self); |
| 180 | + unsafe { mem::transmute::<u64, $reftype>(bits) } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + impl ReadAs<Ref<$reftype>> for UntypedVal { |
| 185 | + fn read_as(&self) -> Ref<$reftype> { |
| 186 | + let bits = u64::from(*self); |
| 187 | + if bits == 0 { |
| 188 | + return <Ref<$reftype>>::Null; |
| 189 | + } |
| 190 | + <Ref<$reftype>>::Val(<Self as ReadAs<$reftype>>::read_as(self)) |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + impl WriteAs<$reftype> for UntypedVal { |
| 195 | + fn write_as(&mut self, value: $reftype) { |
| 196 | + let bits = unsafe { mem::transmute::<$reftype, u64>(value) }; |
| 197 | + self.write_as(bits) |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + impl WriteAs<Ref<$reftype>> for UntypedVal { |
| 202 | + fn write_as(&mut self, value: Ref<$reftype>) { |
| 203 | + match value { |
| 204 | + Ref::Null => self.write_as(0_u64), |
| 205 | + Ref::Val(value) => self.write_as(value), |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
177 | 210 | impl From<UntypedVal> for Ref<$reftype> { |
178 | 211 | fn from(untyped: UntypedVal) -> Self { |
179 | 212 | if u64::from(untyped) == 0 { |
|
0 commit comments