Skip to content

Cannot reflect struct with Option fields #4054

@inodentry

Description

@inodentry

Bevy cannot derive Reflect on structs that contain Option<T> fields, even though T is Reflect.

This compiles:

#[derive(Component, Reflect)]
struct MyComponent {
    sz: Size<Val>,
}

(Size<Val> is Reflect)

This does not:

#[derive(Component, Reflect)]
struct MyComponent {
    sz: Option<Size<Val>>,
}
error[E0277]: the trait bound `bevy::prelude::Size<bevy::prelude::Val>: bevy::reflect::erased_serde::private::serde::Serialize` is not satisfied
 --> src/main.rs:3:21
  |
3 | #[derive(Component, Reflect)]
  |                     ^^^^^^^ the trait `bevy::reflect::erased_serde::private::serde::Serialize` is not implemented for `bevy::prelude::Size<bevy::prelude::Val>`
  |
  = note: required because of the requirements on the impl of `bevy::prelude::Reflect` for `std::option::Option<bevy::prelude::Size<bevy::prelude::Val>>`
  = note: required for the cast to the object type `dyn bevy::prelude::Reflect`
  = note: this error originates in the derive macro `Reflect` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `for<'de> bevy::prelude::Size<bevy::prelude::Val>: Deserialize<'de>` is not satisfied
 --> src/main.rs:3:21
  |
3 | #[derive(Component, Reflect)]
  |                     ^^^^^^^ the trait `for<'de> Deserialize<'de>` is not implemented for `bevy::prelude::Size<bevy::prelude::Val>`
  |
  = note: required because of the requirements on the impl of `bevy::prelude::Reflect` for `std::option::Option<bevy::prelude::Size<bevy::prelude::Val>>`
  = note: required for the cast to the object type `dyn bevy::prelude::Reflect`
  = note: this error originates in the derive macro `Reflect` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `bevy::prelude::Size<bevy::prelude::Val>: bevy::reflect::erased_serde::private::serde::Serialize` is not satisfied
 --> src/main.rs:3:21
  |
3 | #[derive(Component, Reflect)]
  |                     ^^^^^^^ the trait `bevy::reflect::erased_serde::private::serde::Serialize` is not implemented for `bevy::prelude::Size<bevy::prelude::Val>`
  |
  = note: required because of the requirements on the impl of `bevy::prelude::Reflect` for `std::option::Option<bevy::prelude::Size<bevy::prelude::Val>>`
  = note: required for the cast to the object type `(dyn bevy::prelude::Reflect + 'static)`
  = note: this error originates in the derive macro `Reflect` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `for<'de> bevy::prelude::Size<bevy::prelude::Val>: Deserialize<'de>` is not satisfied
 --> src/main.rs:3:21
  |
3 | #[derive(Component, Reflect)]
  |                     ^^^^^^^ the trait `for<'de> Deserialize<'de>` is not implemented for `bevy::prelude::Size<bevy::prelude::Val>`
  |
  = note: required because of the requirements on the impl of `bevy::prelude::Reflect` for `std::option::Option<bevy::prelude::Size<bevy::prelude::Val>>`
  = note: required for the cast to the object type `(dyn bevy::prelude::Reflect + 'static)`
  = note: this error originates in the derive macro `Reflect` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: the method `clone_value` exists for enum `std::option::Option<bevy::prelude::Size<bevy::prelude::Val>>`, but its trait bounds were not satisfied
   --> src/main.rs:3:21
    |
3   | #[derive(Component, Reflect)]
    |                     ^^^^^^^ method cannot be called on `std::option::Option<bevy::prelude::Size<bevy::prelude::Val>>` due to unsatisfied trait bounds
    |
   ::: /home/iyes/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:515:1
    |
515 | pub enum Option<T> {
    | ------------------ doesn't satisfy `_: bevy::prelude::Reflect`
    |
   ::: /home/iyes/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_math-0.6.0/src/geometry.rs:8:1
    |
8   | pub struct Size<T: Reflect + PartialEq = f32> {
    | ---------------------------------------------
    | |
    | doesn't satisfy `_: Deserialize<'de>`
    | doesn't satisfy `_: bevy::reflect::erased_serde::private::serde::Serialize`
    |
note: the following trait bounds were not satisfied because of the requirements of the implementation of `bevy::prelude::Reflect` for `_`:
      `bevy::prelude::Size<bevy::prelude::Val>: bevy::reflect::erased_serde::private::serde::Serialize`
      `bevy::prelude::Size<bevy::prelude::Val>: Deserialize<'de>`
   --> src/main.rs:3:21
    |
3   | #[derive(Component, Reflect)]
    |                     ^^^^^^^
4   | struct MyTest {
    |        ^^^^^^
    = note: this error originates in the derive macro `Reflect` (in Nightly builds, run with -Z macro-backtrace for more info)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ReflectionRuntime information about typesC-BugAn unexpected or incorrect behaviorC-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions