1 stable release

1.0.1 Nov 9, 2023

#1903 in Encoding

MIT license

4KB

serde_cast

Works nicely with you_can!

  1. Have structs A and B
  2. Make sure they derive Serialize and Deserialize respectively
  3. Cast!
use serde::{Serialize, Deserialize};
use serde_cast::serde_cast;

#[derive(Serialize)]
struct A {
    field: String,
}

#[derive(Deserialize)]
struct B {
    field: String,
}

let a = A { field: "This was a mistake".to_string() };
let b: B = serde_cast!(&a);

assert_eq!(a.field, b.field);

lib.rs:

Cast one struct into another

use serde::{Serialize, Deserialize};
use serde_cast::serde_cast;

#[derive(Serialize)]
struct A {
    field: String,
}

#[derive(Deserialize)]
struct B {
    field: String,
}

let a = A { field: "This was a mistake".to_string() };
let b: B = serde_cast!(&a);

assert_eq!(a.field, b.field);

Dependencies

~0.9–1.5MB
~35K SLoC