-
Notifications
You must be signed in to change notification settings - Fork 8
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
Dynamically load correct version? #12
Comments
Have you tried deserializing as |
Maybe I misunderstood, but isn't |
What I tried was something like this: fn deser(s: &str) -> Result<AnyVersion<MyStruct>> {
match s {
"0.1.0" => <MyStruct!["0.1.0"]>::unpack(...),
...
}
} But obviously this needs to be adapted every time a new version comes around and is not particularly elegant. 😁 |
|
Not sure I understand. I'm using |
Hope this shows the issue a bit better: use packed_struct::prelude::*;
#[obake::versioned]
#[obake(version("0.1.0"))]
#[obake(version("1.0.0"))]
#[derive(Copy, Clone, Debug, Default, PackedStruct)]
#[packed_struct(endian = "msb")]
pub struct Data {
#[obake(cfg("<1.0"))]
#[packed_field]
val1: i32,
#[obake(cfg(">=1.0"))]
val1: u32,
}
impl From<Data!["0.1.0"]> for Data!["1.0.0"] {
fn from(d: Data!["0.1.0"]) -> Self {
Self {
val1: if d.val1 >= 0 { d.val1 as u32 } else { 0_u32 },
}
}
}
fn main() {
// an older sw stored this version of Data:
let mut old_data = <Data!["0.1.0"]>::default();
old_data.val1 = -1;
println!("old_data = {:?}", old_data);
// a newer sw reads out with the current def
// obviously, this will fail (val1 = -1 reinterpreted as u32):
let new_data = Data::unpack(&old_data.pack().unwrap()).unwrap();
println!("new_data = {:?}", new_data);
// What I'm trying to solve is this:
// I've got the actual version from storage (dynamic)
let _vers = "0.1.0";
// now I need to call `unpack` on the right Data def:
// let b = <Data![_vers]>::unpack( ... );
} |
My serialized structs have a header with their version; I'd like to load the exact version dynamically while deserializing, then try to convert to the latest version of the struct. The
MyStruct!
macros seem to only accept string literals. Is there any way to do this?The text was updated successfully, but these errors were encountered: