1 stable release
1.0.1 | Nov 9, 2023 |
---|
#1903 in Encoding
4KB
serde_cast
Works nicely with
you_can
!
- Have structs
A
andB
- Make sure they derive
Serialize
andDeserialize
respectively - 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