#How does `#[reflect(ignore)]` on `enum` types work?

7 messages · Page 1 of 1 (latest)

haughty dirge
#

Suppose I have an enum like this:

#[derive(Default, Reflect, FromReflect)]
enum E {
  #[default]
  A,
  #[reflect(ignore)]
  B,
  C,
}

Based on my tests, when I try to serialize such a type, the engine actually does serialize E::B. So what happens when this is loaded? Does it just load default? Is this even correct usage or is the ignored attribute... ignored... 😛 on enums?

knotty berry
#

Looking at

https://github.com/bevyengine/bevy/blob/main/crates/bevy_reflect/bevy_reflect_derive/src/enum_utility.rs #[reflect(ignore)] is used on an enum when it needs to determine the value of the field, like if it was

enum E {
  #[default]
  A,
  #[reflect(ignore)]
  B(usize),
  C,
}

Reflecting E::B would return the default value for usize

IIUC 'ignore' is really only used when you want to prevent reflection from modifying the value in a particular field of a struct

GitHub

A refreshingly simple data-driven game engine built in Rust - bevy/enum_utility.rs at main · bevyengine/bevy

haughty dirge
#

Ahh, I see. In my specific use case, my enum's state is only B for a single stage. Otherwise it's A or C. So I thought maybe #[reflect(ignore)] just ignores the whole enum case. But even as-is, I think it still solves my issue, where the data inside the B state doesn't need to impl Reflect.

#

Hmm. Or maybe not.

#

This doesn't seem to work:

#[derive(Default)]
struct B;

#[derive(Reflect, FromReflect)]
enum X {
    #[reflect(ignore)]
    A(B),
}

It requires B to implement Reflect.

#

But this works for structs:

#[derive(Default)]
struct B;

#[derive(Reflect, FromReflect)]
struct X {
    #[reflect(ignore)]
    b: B,
}
knotty berry
#

You might repost this to #reflection-dev