Macro bevy_reflect::impl_reflect

source ·
impl_reflect!() { /* proc-macro */ }
Expand description

A replacement for #[derive(Reflect)] to be used with foreign types which the definitions of cannot be altered.

This macro is an alternative to impl_reflect_value! and impl_from_reflect_value! which implement foreign types as Value types. Note that there is no impl_from_reflect, as this macro will do the job of both. This macro implements them using one of the reflect variant traits (bevy_reflect::{Struct, TupleStruct, Enum}, etc.), which have greater functionality. The type being reflected must be in scope, as you cannot qualify it in the macro as e.g. bevy::prelude::Vec3.

It is necessary to add a #[type_path = "my_crate::foo"] attribute to all types.

It may be necessary to add #[reflect(Default)] for some types, specifically non-constructible foreign types. Without Default reflected for such types, you will usually get an arcane error message and fail to compile. If the type does not implement Default, it may not be possible to reflect without extending the macro.

§Example

Implementing Reflect for bevy::prelude::Vec3 as a struct type:

use bevy::prelude::Vec3;

impl_reflect!(
    #[reflect(PartialEq, Serialize, Deserialize, Default)]
    #[type_path = "bevy::prelude"]
    struct Vec3 {
        x: f32,
        y: f32,
        z: f32
    }
);