Skip to content

How to rename inner types when encoding/decoding foreign structs using serde? #667

@yizhepku

Description

@yizhepku

Suppose I have a Rust struct like this:

#[derive(Serialize, Deserialize)]
#[serde(rename = "Elixir.MyPackage.Event")]
struct Event {
    name: String,
    duration: std::time::Duration,
}

And we create an instance like this:

Event { name: "foo".to_string(), duration: std::time::Duration::from_secs(10) }

Currently, rustler will convert it to this:

%{
  :__struct__ => MyPackage.Event,
  :name => "foo",
  :duration => %{:__struct__ => "Duration", "nanos" => 0, "secs" => 10}
}

Is there a way to map std::time::Duration to Elixir.MyPackage.Duration instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions