Skip to content

Add typed variants of UserData and user_data_impl macros#29

Merged
Tired-Fox merged 5 commits into
mainfrom
typed-user-data-macro-impl
Apr 27, 2026
Merged

Add typed variants of UserData and user_data_impl macros#29
Tired-Fox merged 5 commits into
mainfrom
typed-user-data-macro-impl

Conversation

@Tired-Fox
Copy link
Copy Markdown
Owner

@Tired-Fox Tired-Fox commented Apr 22, 2026

  • Add support for typed user data derive and impl macros.
  • Impl Macro improvements
    • Support for field getter and setters
    • Support for static fields with const impl fields
    • Support for static fields with impl functions
/// Custom action
#[derive(Clone, TypedUserData)]
enum Custom {
    A,
    B(
        /// Variant B Data
        String,
    ),
    C {
        name: String,
        /// Age of variant C
        age: u8,
    },
    D(
        /// Variant D Data
        u32,
    ),
}

#[typed_user_data_impl]
impl Custom {
    /// Static field
    const COUNT: usize = 10;

    /// Static field fetched once
    #[field(rename = "PI")]
    fn pi() -> f32 {
        3.14
    }

    /// Support for float infinity
    #[field(rename = "INF")]
    fn inf() -> f32 {
        f32::INFINITY
    }

    /// Support for float nan
    #[field(rename = "NAN")]
    fn nan() -> f32 {
        f32::NAN
    }

    /// Field getter `add_field_function_get`
    #[getter("direction")]
    fn get_direction(&self) -> String {
        "west".into()
    }

    /// Field getter `add_field_function_set`
    #[setter("direction")]
    fn set_direction(&self, input: String) {
        _ = input;
    }

    /// Get the message based on the variant
    #[method]
    fn message(&self) -> String {
        match self {
            Self::A => "Hello, world!".into(),
            Self::B(msg) => msg.clone(),
            Self::C { name, age } => format!("{name} age {age}"),
            Self::D(count) => count.to_string(),
        }
    }
}

@Tired-Fox Tired-Fox force-pushed the typed-user-data-macro-impl branch 6 times, most recently from 4308365 to 199b05e Compare April 22, 2026 22:21
@Tired-Fox Tired-Fox force-pushed the typed-user-data-macro-impl branch from 199b05e to 9234e83 Compare April 27, 2026 16:01
@Tired-Fox Tired-Fox force-pushed the typed-user-data-macro-impl branch from 9234e83 to 57ae726 Compare April 27, 2026 16:05
@Tired-Fox
Copy link
Copy Markdown
Owner Author

Closes #21

@Tired-Fox Tired-Fox merged commit e4ff135 into main Apr 27, 2026
15 checks passed
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