Deserialize a value of this type from the heap.
(cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8])
| 226 | |
| 227 | /// Deserialize a value of this type from the heap. |
| 228 | pub(crate) fn load(cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Val> { |
| 229 | Ok(match ty { |
| 230 | InterfaceType::Bool => Val::Bool(bool::linear_lift_from_memory(cx, ty, bytes)?), |
| 231 | InterfaceType::S8 => Val::S8(i8::linear_lift_from_memory(cx, ty, bytes)?), |
| 232 | InterfaceType::U8 => Val::U8(u8::linear_lift_from_memory(cx, ty, bytes)?), |
| 233 | InterfaceType::S16 => Val::S16(i16::linear_lift_from_memory(cx, ty, bytes)?), |
| 234 | InterfaceType::U16 => Val::U16(u16::linear_lift_from_memory(cx, ty, bytes)?), |
| 235 | InterfaceType::S32 => Val::S32(i32::linear_lift_from_memory(cx, ty, bytes)?), |
| 236 | InterfaceType::U32 => Val::U32(u32::linear_lift_from_memory(cx, ty, bytes)?), |
| 237 | InterfaceType::S64 => Val::S64(i64::linear_lift_from_memory(cx, ty, bytes)?), |
| 238 | InterfaceType::U64 => Val::U64(u64::linear_lift_from_memory(cx, ty, bytes)?), |
| 239 | InterfaceType::Float32 => Val::Float32(f32::linear_lift_from_memory(cx, ty, bytes)?), |
| 240 | InterfaceType::Float64 => Val::Float64(f64::linear_lift_from_memory(cx, ty, bytes)?), |
| 241 | InterfaceType::Char => Val::Char(char::linear_lift_from_memory(cx, ty, bytes)?), |
| 242 | InterfaceType::String => Val::String(<_>::linear_lift_from_memory(cx, ty, bytes)?), |
| 243 | InterfaceType::Own(_) | InterfaceType::Borrow(_) => { |
| 244 | Val::Resource(ResourceAny::linear_lift_from_memory(cx, ty, bytes)?) |
| 245 | } |
| 246 | InterfaceType::List(i) => { |
| 247 | let (ptr, len) = load_flat_pointer_pair(bytes); |
| 248 | load_list(cx, i, ptr, len)? |
| 249 | } |
| 250 | InterfaceType::Map(i) => { |
| 251 | let (ptr, len) = load_flat_pointer_pair(bytes); |
| 252 | load_map(cx, i, ptr, len)? |
| 253 | } |
| 254 | |
| 255 | InterfaceType::Record(i) => { |
| 256 | let mut offset = 0; |
| 257 | let fields = cx.types[i].fields.iter(); |
| 258 | Val::Record( |
| 259 | fields |
| 260 | .map(|field| -> Result<(String, Val)> { |
| 261 | let abi = cx.types.canonical_abi(&field.ty); |
| 262 | let offset = abi.next_field32(&mut offset); |
| 263 | let offset = usize::try_from(offset).unwrap(); |
| 264 | let size = usize::try_from(abi.size32).unwrap(); |
| 265 | Ok(( |
| 266 | field.name.to_string(), |
| 267 | Val::load(cx, field.ty, &bytes[offset..][..size])?, |
| 268 | )) |
| 269 | }) |
| 270 | .collect::<Result<_>>()?, |
| 271 | ) |
| 272 | } |
| 273 | InterfaceType::Tuple(i) => { |
| 274 | let types = cx.types[i].types.iter().copied(); |
| 275 | let mut offset = 0; |
| 276 | Val::Tuple( |
| 277 | types |
| 278 | .map(|ty| { |
| 279 | let abi = cx.types.canonical_abi(&ty); |
| 280 | let offset = abi.next_field32(&mut offset); |
| 281 | let offset = usize::try_from(offset).unwrap(); |
| 282 | let size = usize::try_from(abi.size32).unwrap(); |
| 283 | Val::load(cx, ty, &bytes[offset..][..size]) |
| 284 | }) |
| 285 | .collect::<Result<_>>()?, |
no test coverage detected