Encode a primitive into a handle.
(v: Value)
| 264 | |
| 265 | /// Encode a primitive into a handle. |
| 266 | pub fn encode(v: Value) -> Result<Handle> { |
| 267 | let raw = match v { |
| 268 | Value::None => unsafe { edge_encode(tag::NONE, core::ptr::null(), 0) }, |
| 269 | Value::Bool(b) => { |
| 270 | let buf = [if b { 1u8 } else { 0u8 }]; |
| 271 | unsafe { edge_encode(tag::BOOL, buf.as_ptr(), 1) } |
| 272 | } |
| 273 | Value::Int(i) => { |
| 274 | let buf = i.to_le_bytes(); |
| 275 | unsafe { edge_encode(tag::INT, buf.as_ptr(), 16) } |
| 276 | } |
| 277 | Value::Float(f) => { |
| 278 | let buf = f.to_le_bytes(); |
| 279 | unsafe { edge_encode(tag::FLOAT, buf.as_ptr(), 8) } |
| 280 | } |
| 281 | Value::Bytes(b) => unsafe { edge_encode(tag::BYTES, b.as_ptr(), b.len() as u32) }, |
| 282 | Value::Raw(b) => unsafe { edge_encode(tag::RAW, b.as_ptr(), b.len() as u32) }, |
| 283 | }; |
| 284 | if raw == 0 { Err(Error::Runtime(alloc::string::String::from("encode failed"))) } |
| 285 | else { Ok(Handle::from_raw(raw)) } |
| 286 | } |
| 287 | |
| 288 | /// Typed primitive value; composites (list, dict, set, instances) go through `Handle::call`. |
| 289 | #[derive(Debug, Clone)] |
no test coverage detected