Skip to main content

from_slice_with_shape

Function from_slice_with_shape 

Source
pub fn from_slice_with_shape(
    input: &[u8],
    source_shape: &'static Shape,
) -> Result<Value, DeserializeError<PostcardError>>
Expand description

Deserialize postcard bytes into a Value using shape information.

Since postcard is not a self-describing format, you need to provide the shape that describes the structure of the data.

ยงExample

use facet::Facet;
use facet_postcard::{from_slice_with_shape, to_vec};

#[derive(Facet)]
struct Point { x: i32, y: i32 }

let point = Point { x: 10, y: 20 };
let bytes = to_vec(&point).unwrap();

// Deserialize using the shape, not the type
let value = from_slice_with_shape(&bytes, Point::SHAPE).unwrap();
let obj = value.as_object().unwrap();
assert_eq!(obj.get("x").unwrap().as_number().unwrap().to_i64(), Some(10));
assert_eq!(obj.get("y").unwrap().as_number().unwrap().to_i64(), Some(20));