Decode a handle into a typed `Value`; errors on composites (use `Handle::call`).
(h: u32)
| 228 | |
| 229 | /// Decode a handle into a typed `Value`; errors on composites (use `Handle::call`). |
| 230 | pub fn decode(h: u32) -> Result<Value> { |
| 231 | let mut tag: u32 = 0; |
| 232 | let mut buf = alloc::vec![0u8; 256]; |
| 233 | loop { |
| 234 | let r = unsafe { |
| 235 | edge_decode(h, &mut tag as *mut u32, buf.as_mut_ptr(), buf.len() as u32) |
| 236 | }; |
| 237 | if r >= 0 { |
| 238 | if tag == u32::MAX { |
| 239 | return Err(Error::Type(alloc::string::String::from("value is not a primitive (use Handle::call for composites)"))); |
| 240 | } |
| 241 | buf.truncate(r as usize); |
| 242 | return Ok(match tag { |
| 243 | 0 => Value::None, |
| 244 | 1 => Value::Bool(buf[0] != 0), |
| 245 | 2 => { |
| 246 | let mut a = [0u8; 16]; |
| 247 | a.copy_from_slice(&buf[..16]); |
| 248 | Value::Int(i128::from_le_bytes(a)) |
| 249 | } |
| 250 | 3 => { |
| 251 | let mut a = [0u8; 8]; |
| 252 | a.copy_from_slice(&buf[..8]); |
| 253 | Value::Float(f64::from_le_bytes(a)) |
| 254 | } |
| 255 | 4 => Value::Bytes(buf), |
| 256 | 5 => Value::Raw(buf), |
| 257 | _ => return Err(Error::Type(alloc::string::String::from("unknown tag"))), |
| 258 | }); |
| 259 | } |
| 260 | // Negative = -needed. |
| 261 | buf.resize((-r) as usize, 0); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /// Encode a primitive into a handle. |
| 266 | pub fn encode(v: Value) -> Result<Handle> { |
no test coverage detected