bevy/crates
Gino Valente f96cd758cd
bevy_reflect: Opt-out attribute for TypePath (#9140)
# Objective

Fixes #9094

## Solution

Takes a bit from
[this](https://github.com/bevyengine/bevy/issues/9094#issuecomment-1629333851)
comment as well as a
[comment](https://discord.com/channels/691052431525675048/1002362493634629796/1128024873260810271)
from @soqb.

This allows users to opt-out of the `TypePath` implementation that is
automatically generated by the `Reflect` derive macro, allowing custom
`TypePath` implementations.

```rust
#[derive(Reflect)]
#[reflect(type_path = false)]
struct Foo<T> {
    #[reflect(ignore)]
    _marker: PhantomData<T>,
}

struct NotTypePath;

impl<T: 'static> TypePath for Foo<T> {
    fn type_path() -> &'static str {
        std::any::type_name::<Self>()
    }

    fn short_type_path() -> &'static str {
        static CELL: GenericTypePathCell = GenericTypePathCell::new();
        CELL.get_or_insert::<Self, _>(|| {
            bevy_utils::get_short_name(std::any::type_name::<Self>())
        })
    }

    fn crate_name() -> Option<&'static str> {
        Some("my_crate")
    }

    fn module_path() -> Option<&'static str> {
        Some("my_crate::foo")
    }

    fn type_ident() -> Option<&'static str> {
        Some("Foo")
    }
}

// Can use `TypePath`
let _ = <Foo<NotTypePath> as TypePath>::type_path();

// Can register the type
let mut registry = TypeRegistry::default();
registry.register::<Foo<NotTypePath>>();
```

#### Type Path Stability

The stability of type paths mainly come into play during serialization.
If a type is moved between builds, an unstable type path may become
invalid.

Users that opt-out of `TypePath` and rely on something like
`std::any::type_name` as in the example above, should be aware that this
solution removes the stability guarantees. Deserialization thus expects
that type to never move. If it does, then the serialized type paths will
need to be updated accordingly.

If a user depends on stability, they will need to implement that
stability logic manually (probably by looking at the expanded output of
a typical `Reflect`/`TypePath` derive). This could be difficult for type
parameters that don't/can't implement `TypePath`, and will need to make
heavy use of string parsing and manipulation to achieve the same effect
(alternatively, they can choose to simply exclude any type parameter
that doesn't implement `TypePath`).

---

## Changelog

- Added the `#[reflect(type_path = false)]` attribute to opt out of the
`TypePath` impl when deriving `Reflect`

---------

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2023-08-10 00:37:56 +00:00
..
bevy_a11y Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_animation Simplify parallel iteration methods (#8854) 2023-07-23 11:09:24 +00:00
bevy_app Add track_caller to App::add_plugins (#9174) 2023-07-23 01:02:20 +00:00
bevy_asset Improve Mesh documentation (#9061) 2023-07-31 18:55:42 +00:00
bevy_audio update documentation on AudioSink (#9332) 2023-08-03 12:46:44 +00:00
bevy_core fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
bevy_core_pipeline Extend the default render range of 2D camera (#9310) 2023-07-31 19:28:20 +00:00
bevy_derive Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_diagnostic Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_dylib Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_dynamic_plugin Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_ecs Fix ambiguous_with breaking run conditions (#9253) 2023-08-03 07:53:20 +00:00
bevy_ecs_compile_fail_tests Resolve clippy issues for rust 1.70.0 (#8738) 2023-06-01 21:05:05 +00:00
bevy_encase_derive Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_gilrs Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_gizmos Clarify immediate mode in Gizmos documentation (#9183) 2023-07-31 19:14:52 +00:00
bevy_gltf Improve Mesh documentation (#9061) 2023-07-31 18:55:42 +00:00
bevy_hierarchy Prevent setting parent as itself (#8980) 2023-08-07 23:00:48 +00:00
bevy_input Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_internal fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
bevy_log Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_macro_utils Add some more helpful errors to BevyManifest when it doesn't find Cargo.toml (#9207) 2023-07-19 12:05:04 +00:00
bevy_macros_compile_fail_tests bevy_derive: Add #[deref] attribute (#8552) 2023-05-16 18:29:09 +00:00
bevy_math Add reflect impls to IRect and URect (#9191) 2023-07-23 01:02:00 +00:00
bevy_mikktspace Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_pbr Work around naga/wgpu WGSL instance_index -> GLSL gl_InstanceID bug on WebGL2 (#9383) 2023-08-09 18:38:45 +00:00
bevy_ptr Put #[repr(transparent)] attr to bevy_ptr types (#9068) 2023-07-14 18:55:15 +00:00
bevy_reflect bevy_reflect: Opt-out attribute for TypePath (#9140) 2023-08-10 00:37:56 +00:00
bevy_reflect_compile_fail_tests fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
bevy_render Update Camera's Frustum only when its GlobalTransform or CameraProjection changed (#9092) 2023-08-10 00:06:27 +00:00
bevy_scene Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_sprite Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_tasks Fix doc warning in bevy_tasks (#9348) 2023-08-05 13:53:05 +00:00
bevy_text Improve font size related docs (#9320) 2023-08-03 07:48:11 +00:00
bevy_time Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_transform Simplify parallel iteration methods (#8854) 2023-07-23 11:09:24 +00:00
bevy_ui Change the default for the measure_func field of ContentSize to None. (#9346) 2023-08-07 23:06:40 +00:00
bevy_utils Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_window improve documentation relating to WindowPlugin and Window (#9173) 2023-07-30 15:46:16 +00:00
bevy_winit Change WinitPlugin defaults to limit game update rate when window is not visible (#7611) 2023-08-02 23:41:23 +00:00