#[derive(Versioned)]
{
// Attributes available to this derive:
#[versioned]
}
Expand description
Derives the Versioned trait for a struct.
This macro generates:
- A version enum (e.g.,
UserVersion) with variants for each version - Implementation of
Versionedtrait withfrom_versionandto_versionmethods
§Attributes
The macro accepts a versioned attribute with the following format:
ⓘ
#[versioned(versions = [Version1, Version2, ...])]§Requirements
- The struct must have named fields (not tuple structs or unit structs)
- Each version struct must implement
FromVersion<CurrentStruct> - Each version struct must implement
Serialize,Deserialize, andClone
§Panics
This function will panic if versions is empty (which should be caught during compilation),
or if the latest version cannot be determined.
§Example
use serde_versioned::Versioned;
use serde::{Deserialize, Serialize};
#[derive(Versioned, Serialize, Deserialize, Clone)]
#[versioned(versions = [UserV1, UserV2])]
struct User {
pub name: String,
pub age: u32,
}
#[derive(Serialize, Deserialize, Clone)]
struct UserV1 {
pub name: String,
}
#[derive(Serialize, Deserialize, Clone)]
struct UserV2 {
pub name: String,
pub age: u32,
}
impl serde_versioned::FromVersion<User> for UserV1 {
fn convert(self) -> User {
User { name: self.name, age: 0 }
}
}
impl serde_versioned::FromVersion<User> for UserV2 {
fn convert(self) -> User {
User { name: self.name, age: self.age }
}
}