From d171fe47fb182d8bb3662750239137676513d814 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 1 Jul 2024 18:19:04 +0200 Subject: [PATCH] cargo clippy + pr feedbacks --- src/geometry/collider.rs | 2 +- .../systemparams/rapier_context_access.rs | 39 +++++++------- src/plugin/plugin.rs | 22 +++++--- src/plugin/systems/collider.rs | 5 +- src/plugin/systems/joint.rs | 1 - .../systems/multiple_rapier_contexts.rs | 5 +- src/plugin/systems/remove.rs | 51 ++++++++----------- 7 files changed, 61 insertions(+), 64 deletions(-) diff --git a/src/geometry/collider.rs b/src/geometry/collider.rs index bffb844f..93f74629 100644 --- a/src/geometry/collider.rs +++ b/src/geometry/collider.rs @@ -3,7 +3,7 @@ use std::fmt; #[cfg(all(feature = "dim3", feature = "async-collider"))] use {crate::geometry::VHACDParameters, bevy::utils::HashMap}; -use bevy::{prelude::*, reflect::impl_reflect}; +use bevy::prelude::*; use bevy::utils::HashSet; use rapier::geometry::Shape; diff --git a/src/plugin/context/systemparams/rapier_context_access.rs b/src/plugin/context/systemparams/rapier_context_access.rs index e3e88af1..6d8b9de8 100644 --- a/src/plugin/context/systemparams/rapier_context_access.rs +++ b/src/plugin/context/systemparams/rapier_context_access.rs @@ -5,11 +5,11 @@ use std::ops::{Deref, DerefMut}; use super::super::{DefaultRapierContext, RapierContext, RapierContextEntityLink}; /// Utility [`SystemParam`] to easily access the default world [`RapierContext`] immutably #[derive(SystemParam)] -pub struct DefaultRapierContextAccess<'w, 's> { - rapier_context: Query<'w, 's, &'static RapierContext, With>, +pub struct DefaultRapierContextAccess<'w, 's, T: Component = DefaultRapierContext> { + rapier_context: Query<'w, 's, &'static RapierContext, With>, } -impl<'w, 's> DefaultRapierContextAccess<'w, 's> { +impl<'w, 's, T: Component> DefaultRapierContextAccess<'w, 's, T> { /// Use this method if you only have one world. pub fn single(&'_ self) -> &RapierContext { self.rapier_context.single() @@ -26,11 +26,11 @@ impl<'w, 's> Deref for DefaultRapierContextAccess<'w, 's> { /// Utility [`SystemParam`] to easily access the default world [`RapierContext`] mutably #[derive(SystemParam)] -pub struct DefaultRapierContextAccessMut<'w, 's> { - rapier_context: Query<'w, 's, &'static mut RapierContext, With>, +pub struct DefaultRapierContextAccessMut<'w, 's, T: Component = DefaultRapierContext> { + rapier_context: Query<'w, 's, &'static mut RapierContext, With>, } -impl<'w, 's> Deref for DefaultRapierContextAccessMut<'w, 's> { +impl<'w, 's, T: Component> Deref for DefaultRapierContextAccessMut<'w, 's, T> { type Target = RapierContext; fn deref(&self) -> &Self::Target { @@ -48,10 +48,16 @@ impl<'w, 's> DerefMut for DefaultRapierContextAccessMut<'w, 's> { /// Utility [`SystemParam`] to easily access any [`RapierContext`] immutably #[derive(SystemParam)] pub struct RapierContextAccess<'w, 's> { - rapier_context: Query<'w, 's, &'static RapierContext>, + /// Query used to retrieve a [`RapierContext`]. + /// It's helpful to iterate over every rapier contexts, + /// or get a handle over a specific context, for example through: + /// - a marker component such as [`DefaultRapierContext`] + /// - a [`RapierContextEntityLink`]. See [context](RapierContextAccess::context) + pub rapier_context: Query<'w, 's, &'static RapierContext>, } impl<'w, 's> RapierContextAccess<'w, 's> { + /// Retrieves the rapier context responsible for the entity owning the given [`RapierContextEntityLink`]. pub fn context(&self, link: RapierContextEntityLink) -> &'_ RapierContext { self.rapier_context .get(link.0) @@ -70,10 +76,16 @@ impl<'w, 's> Deref for RapierContextAccess<'w, 's> { /// Utility [`SystemParam`] to easily access any [`RapierContext`] mutably #[derive(SystemParam)] pub struct RapierContextAccessMut<'w, 's> { + /// Query used to retrieve a [`RapierContext`]. + /// It's helpful to iterate over every rapier contexts, + /// or get a handle over a specific context, for example through: + /// - a marker component such as [`DefaultRapierContext`] + /// - a [`RapierContextEntityLink`]. See [context](RapierContextAccess::context) pub rapier_context: Query<'w, 's, &'static mut RapierContext>, } impl<'w, 's> RapierContextAccessMut<'w, 's> { + /// Retrieves the rapier context responsible for the entity owning the given [`RapierContextEntityLink`]. pub fn context(&mut self, link: RapierContextEntityLink) -> &'_ mut RapierContext { self.rapier_context .get_mut(link.0) @@ -81,16 +93,3 @@ impl<'w, 's> RapierContextAccessMut<'w, 's> { .into_inner() } } - -pub fn try_retrieve_context<'a>( - link: Option<&RapierContextEntityLink>, - context: &Query>, -) -> Result { - link.map_or_else( - || { - let context_entity = context.iter().next().unwrap(); - Err(context_entity) - }, - |link| Ok(link.0), - ) -} diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index 349ef644..b58ab001 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -42,11 +42,14 @@ where /// likely always be 1.0 in 3D. In 2D, this is useful to specify a "pixels-per-meter" /// conversion ratio. pub fn with_length_unit(mut self, length_unit: f32) -> Self { - self.default_world_setup = RapierContextInitialization::InitializeDefaultRapierContext { - length_unit: length_unit, - }; + self.default_world_setup = + RapierContextInitialization::InitializeDefaultRapierContext { length_unit }; self } + + /// Specifies a default world initialization strategy. + /// + /// The default is to initialize a [`RapierContext`] with a length unit of 1. pub fn with_default_world( mut self, default_world_initialization: RapierContextInitialization, @@ -106,9 +109,8 @@ where .in_set(RapierTransformPropagateSet), systems::on_add_entity_with_parent, systems::on_change_world, - apply_deferred, + // systems::sync_removals, - apply_deferred, #[cfg(all(feature = "dim3", feature = "async-collider"))] systems::init_async_scene_colliders, #[cfg(all(feature = "dim3", feature = "async-collider"))] @@ -116,10 +118,8 @@ where systems::init_rigid_bodies, systems::init_colliders, systems::init_joints, - apply_deferred, //systems::sync_removals, // Run this here so the following systems do not have a 1 frame delay. - apply_deferred, systems::apply_scale, systems::apply_collider_user_changes, systems::apply_rigid_body_user_changes, @@ -292,8 +292,14 @@ where /// and as long as any [`RapierContextEntityLink`] has a reference to its [`RapierContext`]. #[derive(Resource, Debug, Reflect, Clone)] pub enum RapierContextInitialization { + /// [`RapierPhysicsPlugin`] will not spawn any entity containing [`RapierContext`] automatically. NoAutomaticRapierContext, - InitializeDefaultRapierContext { length_unit: f32 }, + /// [`RapierPhysicsPlugin`] will spawn an entity containing a [`RapierContext`] + /// automatically during [`PreStartup`], with the [`DefaultRapierContext`] marker component. + InitializeDefaultRapierContext { + /// See [`IntegrationParameters::length_unit`] + length_unit: f32, + }, } impl Default for RapierContextInitialization { diff --git a/src/plugin/systems/collider.rs b/src/plugin/systems/collider.rs index 38c2022a..e581adde 100644 --- a/src/plugin/systems/collider.rs +++ b/src/plugin/systems/collider.rs @@ -1,6 +1,6 @@ use crate::dynamics::ReadMassProperties; use crate::geometry::Collider; -use crate::plugin::context::systemparams::{try_retrieve_context, RapierEntity}; +use crate::plugin::context::systemparams::RapierEntity; use crate::plugin::context::RapierContextEntityLink; use crate::plugin::{RapierConfiguration, RapierContext, RapierContextAccessMut}; use crate::prelude::{ @@ -142,11 +142,10 @@ pub fn apply_collider_user_changes( ) { for (rapier_entity, handle, transform) in changed_collider_transforms.iter() { let context = context.context(*rapier_entity.rapier_context_link); - let config = config.get(rapier_entity.rapier_context_link.0).unwrap(); if context.collider_parent(rapier_entity.entity).is_some() { let (_, collider_position) = collider_offset( rapier_entity.entity, - &context, + context, &parent_query, &transform_query, ); diff --git a/src/plugin/systems/joint.rs b/src/plugin/systems/joint.rs index c361c1b6..5538bc1e 100644 --- a/src/plugin/systems/joint.rs +++ b/src/plugin/systems/joint.rs @@ -2,7 +2,6 @@ use crate::dynamics::ImpulseJoint; use crate::dynamics::MultibodyJoint; use crate::dynamics::RapierImpulseJointHandle; use crate::dynamics::RapierMultibodyJointHandle; -use crate::plugin::context::systemparams::try_retrieve_context; use crate::plugin::context::RapierContextEntityLink; use crate::plugin::RapierContext; use crate::plugin::RapierContextAccessMut; diff --git a/src/plugin/systems/multiple_rapier_contexts.rs b/src/plugin/systems/multiple_rapier_contexts.rs index aaa29a3c..19d6259f 100644 --- a/src/plugin/systems/multiple_rapier_contexts.rs +++ b/src/plugin/systems/multiple_rapier_contexts.rs @@ -11,7 +11,10 @@ use bevy::prelude::*; /// /// If this fails to happen, weirdness will ensue. pub fn on_add_entity_with_parent( - q_add_entity_without_parent: Query<(Entity, &Parent), Changed>, + q_add_entity_without_parent: Query< + (Entity, &Parent), + (With, Changed), + >, q_parent: Query<&Parent>, q_physics_world: Query<&RapierContextEntityLink>, mut commands: Commands, diff --git a/src/plugin/systems/remove.rs b/src/plugin/systems/remove.rs index b1c4d0a2..f1ca65b0 100644 --- a/src/plugin/systems/remove.rs +++ b/src/plugin/systems/remove.rs @@ -8,7 +8,6 @@ use crate::geometry::Collider; use crate::geometry::ColliderDisabled; use crate::geometry::RapierColliderHandle; use crate::plugin::context::systemparams::RapierContextAccessMut; -use crate::plugin::context::RapierContextEntityLink; use crate::plugin::RapierContext; use crate::prelude::MassModifiedEvent; use crate::prelude::RigidBodyDisabled; @@ -20,7 +19,7 @@ use bevy::prelude::*; /// despawn). pub fn sync_removals( mut commands: Commands, - mut context: RapierContextAccessMut, + mut context_accessor: RapierContextAccessMut, mut removed_bodies: RemovedComponents, mut removed_colliders: RemovedComponents, mut removed_impulse_joints: RemovedComponents, @@ -43,9 +42,9 @@ pub fn sync_removals( * Rigid-bodies removal detection. */ for entity in removed_bodies.read() { - let Some((mut context, handle)) = - find_context(&mut context, |context| context.entity2body.remove(&entity)) - else { + let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + context.entity2body.remove(&entity) + }) else { continue; }; let context = &mut *context; @@ -62,9 +61,9 @@ pub fn sync_removals( } for entity in orphan_bodies.iter() { - if let Some((mut context, handle)) = - find_context(&mut context, |context| context.entity2body.remove(&entity)) - { + if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { + context.entity2body.remove(&entity) + }) { let context = &mut *context; let _ = context.last_body_transform_set.remove(&handle); context.bodies.remove( @@ -83,7 +82,7 @@ pub fn sync_removals( * Collider removal detection. */ for entity in removed_colliders.read() { - let Some((mut context, handle)) = find_context(&mut context, |context| { + let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2collider.remove(&entity) }) else { continue; @@ -100,7 +99,7 @@ pub fn sync_removals( } for entity in orphan_colliders.iter() { - if let Some((mut context, handle)) = find_context(&mut context, |context| { + if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2collider.remove(&entity) }) { let context = &mut *context; @@ -120,7 +119,7 @@ pub fn sync_removals( * Impulse joint removal detection. */ for entity in removed_impulse_joints.read() { - let Some((mut context, handle)) = find_context(&mut context, |context| { + let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2impulse_joint.remove(&entity) }) else { continue; @@ -130,7 +129,7 @@ pub fn sync_removals( } for entity in orphan_impulse_joints.iter() { - if let Some((mut context, handle)) = find_context(&mut context, |context| { + if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2impulse_joint.remove(&entity) }) { let context = &mut *context; @@ -143,7 +142,7 @@ pub fn sync_removals( * Multibody joint removal detection. */ for entity in removed_multibody_joints.read() { - let Some((mut context, handle)) = find_context(&mut context, |context| { + let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2multibody_joint.remove(&entity) }) else { continue; @@ -153,7 +152,7 @@ pub fn sync_removals( } for entity in orphan_multibody_joints.iter() { - if let Some((mut context, handle)) = find_context(&mut context, |context| { + if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2multibody_joint.remove(&entity) }) { let context = &mut *context; @@ -168,7 +167,7 @@ pub fn sync_removals( * Marker components removal detection. */ for entity in removed_sensors.read() { - if let Some((mut context, handle)) = find_context(&mut context, |context| { + if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2collider.get(&entity).copied() }) { if let Some(co) = context.colliders.get_mut(handle) { @@ -178,7 +177,7 @@ pub fn sync_removals( } for entity in removed_colliders_disabled.read() { - if let Some((mut context, handle)) = find_context(&mut context, |context| { + if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2collider.get(&entity).copied() }) { if let Some(co) = context.colliders.get_mut(handle) { @@ -188,7 +187,7 @@ pub fn sync_removals( } for entity in removed_rigid_body_disabled.read() { - if let Some((mut context, handle)) = find_context(&mut context, |context| { + if let Some((mut context, handle)) = find_context(&mut context_accessor, |context| { context.entity2body.get(&entity).copied() }) { if let Some(rb) = context.bodies.get_mut(handle) { @@ -201,19 +200,11 @@ pub fn sync_removals( } fn find_context<'a, T>( - context: &'a mut RapierContextAccessMut, + context_accessor: &'a mut RapierContextAccessMut, item_finder: impl Fn(&mut RapierContext) -> Option, ) -> Option<(Mut<'a, RapierContext>, T)> { - let finder = item_finder; - let context = { - let mut result = None; - for mut context in context.rapier_context.iter_mut() { - if let Some(handle) = finder(&mut *context) { - result = Some((context, handle)); - break; - } - } - result - }; - context + context_accessor + .rapier_context + .iter_mut() + .find_map(|mut context| item_finder(&mut context).map(|handle| (context, handle))) }