EmbarkStudios / mirror-mirror

🪞 Powerful reflection library for Rust
Apache License 2.0
79 stars 2 forks source link

Support setting custom functions for `FromReflect` impl #6

Closed davidpdrsn closed 1 year ago

davidpdrsn commented 1 year ago

For validating fields or maintaining invariants.

API could be something like

#[derive(Reflect)]
struct Foo {
    #[reflect(from_reflect_with(clamp_ratio))]
    ratio: f32,
}

fn clamp_ratio(ratio: &dyn Reflect) -> Option<f32> {
    Some(ratio.downcast_ref::<f32>()?.clamp(0, 1.0))
}