Repository: bevyengine/bevy
File: crates/bevy_ecs/src/world/entity_ref.rs (Rust)

Code before:
    /// # let mut world = World::default();
    /// let mut entity = world.spawn((X(0), Y(0))).into_mutable();
    ///
    /// // This gives the `FilteredEntityMut` access to `&mut A` and `&mut B`.
    /// let mut query = QueryBuilder::<FilteredEntityMut>::new(&mut world)
    ///     .data::<(&mut X, &mut Y)>()
    ///     .build();
    ///
    /// let mut filtered_entity: FilteredEntityMut = query.single_mut(&mut world).unwrap();
    ///
    /// // Get mutable access to two components at once
    /// // SAFETY: We don't take any other references to `X` from this entity
    /// let mut x = unsafe { filtered_entity.get_mut_unchecked::<X>() }.unwrap();
    /// // SAFETY: We don't take any other references to `Y` from this entity
    /// let mut y = unsafe { filtered_entity.get_mut_unchecked::<Y>() }.unwrap();
    /// *x = X(1);
    /// *y = Y(1);
    /// ```
    ///
    /// # Safety
    ///
    /// No other references to the same component may exist at the same time as the returned reference.
    ///
    /// # See also
    ///
    /// - [`get_mut`](Self::get_mut) for the safe version.
    #[inline]
    pub unsafe fn get_mut_unchecked<T: Component<Mutability = Mutable>>(
        &self,
    ) -> Option<Mut<'_, T>> {
        let id = self
            .entity
            .world()
            .components()
            .get_valid_id(TypeId::of::<T>())?;
        self.access
            .has_component_write(id)
            // SAFETY: We have write access and we promise to not create other references to the same component
            .then(|| unsafe { self.entity.get_mut() })
            .flatten()
    }

    /// Consumes self and gets mutable access to the component of type `T`
    /// with the world `'w` lifetime for the current entity.
    /// Returns `None` if the entity does not have a component of type `T`.
    #[inline]
    pub fn into_mut<T: Component<Mutability = Mutable>>(self) -> Option<Mut<'w, T>> {
        // SAFETY:
        // - We have write access
        // - The bound `T: Component<Mutability = Mutable>` ensures the component is mutable
        unsafe { self.into_mut_assume_mutable() }

Review comment (suggestion):
This is copied from `get_mut`, right?  I'd be inclined to try to share the implementation by changing `get_mut` to return `unsafe { self.get_mut_unchecked() }`, and similarly for `get_mut_by_id`.  

Code after:
    ///     .data::<(&mut X, &mut Y)>()
    ///     .build();
    ///
    /// let mut filtered_entity: FilteredEntityMut = query.single_mut(&mut world).unwrap();
    ///
    /// // Get mutable access to two components at once
    /// // SAFETY: We don't take any other references to `X` from this entity
    /// let mut x = unsafe { filtered_entity.get_mut_unchecked::<X>() }.unwrap();
    /// // SAFETY: We don't take any other references to `Y` from this entity
    /// let mut y = unsafe { filtered_entity.get_mut_unchecked::<Y>() }.unwrap();
    /// *x = X(1);
    /// *y = Y(1);
    /// ```
    ///
    /// # Safety
    ///
    /// No other references to the same component may exist at the same time as the returned reference.
    ///
    /// # See also
    ///
    /// - [`get_mut`](Self::get_mut) for the safe version.
    #[inline]
    pub unsafe fn get_mut_unchecked<T: Component<Mutability = Mutable>>(
        &self,
    ) -> Option<Mut<'_, T>> {
        let id = self
            .entity
            .world()
            .components()
            .get_valid_id(TypeId::of::<T>())?;
        self.access
            .has_component_write(id)
            // SAFETY: We have permission to access the component mutable
            // and we promise to not create other references to the same component
            .then(|| unsafe { self.entity.get_mut() })
            .flatten()
    }

    /// Consumes self and gets mutable access to the component of type `T`
    /// with the world `'w` lifetime for the current entity.
    /// Returns `None` if the entity does not have a component of type `T`.
    #[inline]
    pub fn into_mut<T: Component<Mutability = Mutable>>(self) -> Option<Mut<'w, T>> {
        // SAFETY:
        // - We have write access
        // - The bound `T: Component<Mutability = Mutable>` ensures the component is mutable
        unsafe { self.into_mut_assume_mutable() }
    }

    /// Consumes self and gets mutable access to the component of type `T`
    /// with the world `'w` lifetime for the current entity.