-
-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write example for backwards compatbility via enums #393
Comments
Have you seen this example? https://github.com/rkyv/rkyv/blob/master/examples/backwards_compat/src/main.rs |
It's not really a good example, if I would have a lot of versions wouldn't I have to keep calling Is there no way to just do this: pub enum Test {
A(u16),
B(u32),
}
pub fn main() {
let test = Test::A(1);
let mut serializer = AllocSerializer::<0>::default();
serializer.serialize_value(&test).unwrap();
let bytes = serializer.into_serializer().into_inner();
println!("{:?}", bytes);
// [0, 0, 1, 0, 0, 0, 0, 0]
} increase the size of the enum by adding C, but keeping A and B the same: pub enum Test {
A(u16),
B(u32),
C(u64)
}
pub fn main() {
// bytes from above
let bytes: [u8; 8] = [0, 0, 1, 0, 0, 0, 0, 0];
let archived = rkyv::check_archived_root::<Test>(&bytes[..]).unwrap();
let deserialized: Test = archived.deserialize(&mut rkyv::Infallible).unwrap();
println!("{:?}", deserialized);
} this should work, since I haven't changed A nor B, but:
|
When the #[derive(Archive, Serialize, Deserialize)]
pub enum Test {
A(#[with(AsBox)] u16),
B(#[with(AsBox)] u32),
C(#[with(AsBox)] u64),
} This isn't 100% perfect because if you add enough variants to increase the size of the discriminant, the size of the enum may change. But it should be good enough in most cases. |
Is there anyway to do backwards compatibility for enums? a short example:
or is there a better way to do this? Adding a bigger struct to Door and trying to deserialize an older version of it fails.
The text was updated successfully, but these errors were encountered: