Skip to content

Introduce #[derive(CgpData)] and #[derive(CgpRecord)] Macros#154

Merged
soareschen merged 4 commits intomainfrom
derive-cgp-data
Sep 13, 2025
Merged

Introduce #[derive(CgpData)] and #[derive(CgpRecord)] Macros#154
soareschen merged 4 commits intomainfrom
derive-cgp-data

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Sep 13, 2025

Summary

This PR introduces the macros #[derive(CgpData)] and #[derive(CgpRecord)] to simplify the deriving of extensible data traits. The macros are shortcuts for the following:

  • #[derive(CgpRecord)] is the same as #[derive(HasField, HasFields, BuildField)]
  • #[derive(CgpVariant)] is the same as #[derive(FromVariant, HasFields, ExtractField)]
  • #[derive(CgpData)] is the same as deriving CgpRecord or CgpVariant, depending on whether the body is a struct or enum.

Example

Given the following:

#[derive(CgpData)]
pub struct Person {
    pub name: String,
    pub age: u8,
}

#[derive(CgpData)]
pub enum User {
    Known(Person),
    Anonymous(u64),
}

is the same as:

#[derive(HasField, HasFields, BuildField)]
pub struct Person {
    pub name: String,
    pub age: u8,
}

#[derive(FromVariant, HasFields, ExtractField)]
pub enum User {
    Known(Person),
    Anonymous(u64),
}

@soareschen soareschen merged commit bd2827a into main Sep 13, 2025
5 checks passed
@soareschen soareschen deleted the derive-cgp-data branch September 13, 2025 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant