Skip to content

Reflect derive with generic trait bounds #12987

@Veritius

Description

@Veritius

What problem does this solve or what need does it fill?

For a simple struct with no generic trait bounds, like so, the reflect derive works.

#[derive(Reflect)]
struct MyStruct;

For a struct with one or more generic trait bounds, like so, the reflect derive does not work.

#[derive(Reflect)]
struct MyStruct<T>(PhantomData<T>);

This is because T isn't bound to be Reflect, which makes sense.
Adding a Reflect bound to T would fix this use case.

However, if T may or may not be Reflect, the overall struct cannot derive Reflect.
Normally this could be solved with impl<T: Reflect> Reflect for MyStruct<T>, except that Reflect is a very dangerous and unsafe to implement trait: #[derive(Reflect)] is the "correct" way to implement Reflect.

There should be some way to derive Reflect with additional bounds.

What solution would you like?

Something like an attribute:

#[derive(Reflect)]
#[reflect(where = "T") // Only implements Reflect when `T` is reflect.
struct MyStruct<T>(PhantomData<T>);

This could be used multiple times.

#[derive(Reflect)])
#[reflect(where = "T", where = "Z")]
struct MyStruct<T, Z>(PhantomData<(T, Z)>);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ReflectionRuntime information about typesC-FeatureA new feature, making something new possible

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions