Is it possible to use postcard/serde with complex structures?

Say if I wanted to save Object:

struct Object
{
    rect: Rect,
    square: Square,
    rectangular_prism: RectangularPrism,
}

struct Rect
{
    x: u32,
    y: u32,
}
struct Square
{
    x: u32,
    y: u32,
}
struct RectangularPrism
{
    x: u32,
    y: u32,
    z: u32,
}

fn main()
{
    
}

fn save(object: &Object)
{
    let data = match postcard::to_stdvec(object)
    {
        Ok(d)           => d,
        Err(e)          => {println!("Error on save: {e}"); return;}
    };
}

Would this save with complex data structures and to even load from the postcard file easily?

Add #[derive(Serialize, Deserialize)] on every struct used in the value in question, and you should be fine, unless there's something you aren't telling us which makes the explicitly documented way not work.

ah yeah I forgot to add that, but how would I load from the file?

But I am not entirely sure what to type for something more complex such as my exanple?

Is it some complicated code?

Serde handles all serializeable/deserializeable types in exactly the same way.

No, it is as trivial as it gets. They have an example in the official documentation. It's exactly what you would expect.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.