diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 369d9ecfab486..355e1796c20a2 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -3,7 +3,11 @@ use crate::{component::ComponentTicks, system::Resource}; #[cfg(feature = "bevy_reflect")] use bevy_reflect::Reflect; -use std::ops::{Deref, DerefMut}; +use std::ops::{ + Add, AddAssign, BitAnd, BitOr, BitOrAssign, BitXor, Deref, DerefMut, Div, DivAssign, Index, + IndexMut, Mul, MulAssign, Neg, Not, RangeBounds, Rem, RemAssign, Shl, ShlAssign, Shr, + ShrAssign, Sub, SubAssign, +}; /// Types that implement reliable change detection. /// @@ -184,6 +188,241 @@ pub struct Mut<'a, T> { pub(crate) ticks: Ticks<'a>, } +impl<'a, T, U, V> Add for Mut<'a, T> +where + T: Add + Copy, +{ + type Output = V; + + fn add(self, rhs: U) -> Self::Output { + *self.value + rhs + } +} + +impl<'a, T, U> AddAssign for Mut<'a, T> +where + T: AddAssign, +{ + fn add_assign(&mut self, rhs: U) { + *self.value += rhs; + } +} + +impl<'a, T, U, V> BitAnd for Mut<'a, T> +where + T: BitAnd + Copy, +{ + type Output = V; + + fn bitand(self, rhs: U) -> Self::Output { + *self & rhs + } +} + +impl<'a, T, U, V> BitOr for Mut<'a, T> +where + T: BitOr + Copy, +{ + type Output = V; + + fn bitor(self, rhs: U) -> Self::Output { + *self | rhs + } +} + +impl<'a, T, U> BitOrAssign for Mut<'a, T> +where + T: BitOrAssign, +{ + fn bitor_assign(&mut self, rhs: U) { + *self.value |= rhs; + } +} + +impl<'a, T, U, V> BitXor for Mut<'a, T> +where + T: BitXor + Copy, +{ + type Output = V; + + fn bitxor(self, rhs: U) -> Self::Output { + *self ^ rhs + } +} + +impl<'a, T, U> Div for Mut<'a, T> +where + T: Div + Copy, +{ + type Output = T; + + fn div(self, rhs: U) -> Self::Output { + *self.value / rhs + } +} + +impl<'a, T, U> DivAssign for Mut<'a, T> +where + T: DivAssign, +{ + fn div_assign(&mut self, rhs: U) { + *self.value /= rhs; + } +} + +impl<'a, T, U> Mul for Mut<'a, T> +where + T: Mul + Copy, +{ + type Output = T; + + fn mul(self, rhs: U) -> Self::Output { + *self.value * rhs + } +} + +impl<'a, T, U> MulAssign for Mut<'a, T> +where + T: MulAssign, +{ + fn mul_assign(&mut self, rhs: U) { + *self.value *= rhs; + } +} + +impl<'a, T> RangeBounds for Mut<'a, T> +where + T: RangeBounds, +{ + fn start_bound(&self) -> std::ops::Bound<&T> { + self.as_ref().start_bound() + } + + fn end_bound(&self) -> std::ops::Bound<&T> { + self.as_ref().end_bound() + } +} + +impl<'a, T, U> Rem for Mut<'a, T> +where + T: Rem + Copy, +{ + type Output = U; + fn rem(self, rhs: T) -> Self::Output { + *self % rhs + } +} + +impl<'a, T, U> RemAssign for Mut<'a, T> +where + T: RemAssign, +{ + fn rem_assign(&mut self, rhs: U) { + *self.value %= rhs; + } +} + +impl<'a, T, U> Shl for Mut<'a, T> +where + T: Shl + Copy, +{ + type Output = U; + fn shl(self, rhs: T) -> Self::Output { + *self << rhs + } +} + +impl<'a, T, U> ShlAssign for Mut<'a, T> +where + T: ShlAssign, +{ + fn shl_assign(&mut self, rhs: U) { + *self.value <<= rhs; + } +} + +impl<'a, T, U> Shr for Mut<'a, T> +where + T: Shr + Copy, +{ + type Output = U; + fn shr(self, rhs: T) -> Self::Output { + *self >> rhs + } +} + +impl<'a, T, U> ShrAssign for Mut<'a, T> +where + T: ShrAssign, +{ + fn shr_assign(&mut self, rhs: U) { + *self.value >>= rhs; + } +} + +impl<'a, T, U> SubAssign for Mut<'a, T> +where + T: SubAssign, +{ + fn sub_assign(&mut self, rhs: U) { + *self.value -= rhs; + } +} + +impl<'a, T, U> Sub for Mut<'a, T> +where + T: Sub, + T: Copy, +{ + type Output = T; + + fn sub(self, rhs: U) -> Self::Output { + *self.value - rhs + } +} + +impl<'a, T, U, V> Index for Mut<'a, T> +where + T: Index, +{ + type Output = V; + + fn index(&self, index: U) -> &Self::Output { + &self.as_ref()[index] + } +} + +impl<'a, T, U, V> IndexMut for Mut<'a, T> +where + T: IndexMut, +{ + fn index_mut(&mut self, index: U) -> &mut Self::Output { + &mut self.as_mut()[index] + } +} + +impl<'a, T, U> Neg for Mut<'a, T> +where + T: Neg + Copy, +{ + type Output = U; + + fn neg(self) -> Self::Output { + -*self + } +} + +impl<'a, T, U> Not for Mut<'a, T> +where + T: Not + Copy, +{ + type Output = U; + + fn not(self) -> Self::Output { + !*self + } +} + change_detection_impl!(Mut<'a, T>, T,); impl_into_inner!(Mut<'a, T>, T,); impl_debug!(Mut<'a, T>,);