From 9c7dfb5c769c54b57613431727b29e842e6279c2 Mon Sep 17 00:00:00 2001 From: andriyDev Date: Fri, 31 May 2024 15:32:39 -0700 Subject: [PATCH] Replace `FromGltfRef` trait with `From`. The fewer traits the better. This does add a little bit of a weirdness where it's unclear that some components should implement this, however in those cases, users can always fallback to manually dealing with GltfRefMap. --- crates/bevy_gltf_component_refs/src/lib.rs | 27 ++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/crates/bevy_gltf_component_refs/src/lib.rs b/crates/bevy_gltf_component_refs/src/lib.rs index b953612..fb4cdf1 100644 --- a/crates/bevy_gltf_component_refs/src/lib.rs +++ b/crates/bevy_gltf_component_refs/src/lib.rs @@ -91,13 +91,16 @@ impl GltfRefMap { #[derive(Component, Reflect)] #[reflect(Component)] -struct GltfRef { +struct GltfRef { target: String, #[reflect(ignore)] _marker: PhantomData, } -impl GltfRef { +impl GltfRef +where + Entity: Into, +{ fn system( refs: Query<(Entity, &Self)>, gltf_for_entity: GltfForEntity, @@ -113,7 +116,7 @@ impl GltfRef { match ref_map.get_ref(gltf_root, &gltf_ref.target) { Some(target) => { - commands.entity(entity).insert(T::from_ref(target)); + commands.entity(entity).insert(Into::::into(target)); } None => { warn!( @@ -126,17 +129,14 @@ impl GltfRef { } } -/// Trait for creating a component for a Gltf reference. -pub trait FromGltfRef { - /// Creates `Self` given the `entity` for this reference. - fn from_ref(entity: Entity) -> Self; -} - /// Plugin for automatically converting [`GltfRef`]s into their corresponding /// `T`. -pub struct GltfRefPlugin(PhantomData); +pub struct GltfRefPlugin(PhantomData); -impl Plugin for GltfRefPlugin { +impl Plugin for GltfRefPlugin +where + Entity: Into, +{ fn build(&self, app: &mut App) { app.register_type::>().add_systems( Update, @@ -147,7 +147,10 @@ impl Plugin for GltfRefPlugin { // Manual implementation of Default for GltfRefPlugin to avoid `Default` trait // bounds on `T`. -impl Default for GltfRefPlugin { +impl Default for GltfRefPlugin +where + Entity: Into, +{ fn default() -> Self { Self(Default::default()) }