Skip to content

Inscribe vs serialize #7

@cmlsharp

Description

@cmlsharp

It's not super clear to me what the difference between Serialize and Inscribe is and why the latter is preferred. I get that inscribe allows you to add in additional contextual information not stored in the struct, but serde also lets you do this. Is the idea that Inscribe just makes this nicer syntactically?

In other words, are the following two implementations effectively equivalent (not in that they produce the exact same transcript, but in the guarantees being provided)?

#[derive(Serialize)]
struct Data {
    x: i32,
    y: i32,
    #[serde(serialize_with = "Data::add_context")]
    context: () // note this is a zst so there is no runtime cost to this member

#[derive(Serialize)]
struct Context {
    // ... 
}

impl Data {
    fn new(x: i32, y: i32) -> Self {
        Self {x, y, context: () }
    }
    fn add_context<S: Serializer>(_: &(), s: S) -> Result<S::Ok, S::Err> {
        let ctx = // ...
        ctx.serialize(s)
    }
}

// ...
let data = Data::new(x,y);
transcript.add_serial(label, data)

and

#[derive(Inscribe)]
#[inscribe_addl("add_context")]
struct Data {
    #[inscribe(serial)]
    x: i32,
    #[inscribe(serial)]
    y: i32,
}


#[derive(Inscribe)]
struct Context {
    // ...
}

impl Data {
    fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }
    fn add_context(&self) -> Context {
        let ctx = // ...
        ctx
    }
}

// ...

let data = Data::new(x,y);
transcript.add(label, data);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions