From 68899b38b3aad06e7dac88d1771962a202ae62c6 Mon Sep 17 00:00:00 2001 From: Andre Popovitch Date: Mon, 17 May 2021 11:15:58 -0700 Subject: [PATCH] Impl AsRef+AsMut for Res, ResMut, and Mut --- crates/bevy_ecs/src/system/system_param.rs | 21 +++++++++++++++++++++ crates/bevy_ecs/src/world/pointer.rs | 14 ++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 5dce8e4b2e477..fda3684e6e056 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -207,6 +207,13 @@ impl<'w, T: Component> Deref for Res<'w, T> { } } +impl<'w, T: Component> AsRef for Res<'w, T> { + #[inline] + fn as_ref(&self) -> &T { + self.deref() + } +} + /// The [`SystemParamState`] of [`Res`]. pub struct ResState { component_id: ComponentId, @@ -367,6 +374,20 @@ impl<'w, T: Component> DerefMut for ResMut<'w, T> { } } +impl<'w, T: Component> AsRef for ResMut<'w, T> { + #[inline] + fn as_ref(&self) -> &T { + self.deref() + } +} + +impl<'w, T: Component> AsMut for ResMut<'w, T> { + #[inline] + fn as_mut(&mut self) -> &mut T { + self.deref_mut() + } +} + /// The [`SystemParamState`] of [`ResMut`]. pub struct ResMutState { component_id: ComponentId, diff --git a/crates/bevy_ecs/src/world/pointer.rs b/crates/bevy_ecs/src/world/pointer.rs index 695b2eb75096d..68d3f5d753715 100644 --- a/crates/bevy_ecs/src/world/pointer.rs +++ b/crates/bevy_ecs/src/world/pointer.rs @@ -39,6 +39,20 @@ impl<'a, T: core::fmt::Debug> core::fmt::Debug for Mut<'a, T> { } } +impl<'w, T> AsRef for Mut<'w, T> { + #[inline] + fn as_ref(&self) -> &T { + self.deref() + } +} + +impl<'w, T> AsMut for Mut<'w, T> { + #[inline] + fn as_mut(&mut self) -> &mut T { + self.deref_mut() + } +} + impl<'w, T> Mut<'w, T> { /// Returns true if (and only if) this component been added since the last execution of this /// system.