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

Code before:
    /// Creates a new [`FilteredResources`].
    /// # Safety
    /// It is the callers responsibility to ensure that nothing else may access the any resources in the `world` in a way that conflicts with `access`.
    pub(crate) unsafe fn new(
        world: UnsafeWorldCell<'w>,
        access: &'s Access<ComponentId>,
        last_run: Tick,
        this_run: Tick,
    ) -> Self {
        Self {
            world,
            access,
            last_run,
            this_run,
        }
    }

    /// Returns a reference to the underlying [`Access`].
    pub fn access(&self) -> &Access<ComponentId> {
        self.access
    }

    /// Returns `true` if the `FilteredResources` has access to the given resource.
    /// Note that [`Self::get()`] may still return `Err` if the resource does not exist.
    pub fn has_read<R: Resource>(&self) -> bool {
        let component_id = self.world.components().valid_resource_id::<R>();
        component_id.is_some_and(|component_id| self.access.has_resource_read(component_id))
    }

    /// Gets a reference to the resource of the given type if it exists and the `FilteredResources` has access to it.
    pub fn get<R: Resource>(&self) -> Result<Ref<'w, R>, ResourceFetchError> {
        let component_id = self
            .world
            .components()
            .valid_resource_id::<R>()
            .ok_or(ResourceFetchError::NotRegistered)?;
        if !self.access.has_resource_read(component_id) {
            return Err(ResourceFetchError::NoResourceAccess(component_id));
        }

        // SAFETY: We have read access to this resource
        let (value, ticks, caller) = unsafe { self.world.get_resource_with_ticks(component_id) }
            .ok_or(ResourceFetchError::DoesNotExist(component_id))?;

        Ok(Ref {
            // SAFETY: `component_id` was obtained from the type ID of `R`.
            value: unsafe { value.deref() },
            // SAFETY: We have read access to the resource, so no mutable reference can exist.
            ticks: unsafe { Ticks::from_tick_cells(ticks, self.last_run, self.this_run) },
            // SAFETY: We have read access to the resource, so no mutable reference can exist.
            changed_by: unsafe { caller.map(|caller| caller.deref()) },

Review comment (suggestion):
To make sure I understand the reasoning on most of these: The component must be valid before it can actually be inserted.  So if we're trying to access component data, then we can ignore queued component ids, since even with the ID we'd never find any data.  

I don't think that argument applies to `FilteredResources::has_read`, `FilteredResourcesMut::has_read`, or `FilteredResourcesMut::has_write`.  It's possible to grant access to this by component id, in which case it may have access to a component that's only queued.  And I don't think these methods are called much, if at all, so it should be fine if they have slightly worse perf.  

But it's not a big deal either way, since a user who wants the other behavior can always just get the ID and check the `access` themselves.  

Code after:
    /// Creates a new [`FilteredResources`].
    /// # Safety
    /// It is the callers responsibility to ensure that nothing else may access the any resources in the `world` in a way that conflicts with `access`.
    pub(crate) unsafe fn new(
        world: UnsafeWorldCell<'w>,
        access: &'s Access<ComponentId>,
        last_run: Tick,
        this_run: Tick,
    ) -> Self {
        Self {
            world,
            access,
            last_run,
            this_run,
        }
    }

    /// Returns a reference to the underlying [`Access`].
    pub fn access(&self) -> &Access<ComponentId> {
        self.access
    }

    /// Returns `true` if the `FilteredResources` has access to the given resource.
    /// Note that [`Self::get()`] may still return `Err` if the resource does not exist.
    pub fn has_read<R: Resource>(&self) -> bool {
        let component_id = self.world.components().resource_id::<R>();
        component_id.is_some_and(|component_id| self.access.has_resource_read(component_id))
    }

    /// Gets a reference to the resource of the given type if it exists and the `FilteredResources` has access to it.
    pub fn get<R: Resource>(&self) -> Result<Ref<'w, R>, ResourceFetchError> {
        let component_id = self
            .world
            .components()
            .valid_resource_id::<R>()
            .ok_or(ResourceFetchError::NotRegistered)?;
        if !self.access.has_resource_read(component_id) {
            return Err(ResourceFetchError::NoResourceAccess(component_id));
        }

        // SAFETY: We have read access to this resource
        let (value, ticks, caller) = unsafe { self.world.get_resource_with_ticks(component_id) }
            .ok_or(ResourceFetchError::DoesNotExist(component_id))?;

        Ok(Ref {
            // SAFETY: `component_id` was obtained from the type ID of `R`.
            value: unsafe { value.deref() },
            // SAFETY: We have read access to the resource, so no mutable reference can exist.
            ticks: unsafe { Ticks::from_tick_cells(ticks, self.last_run, self.this_run) },
            // SAFETY: We have read access to the resource, so no mutable reference can exist.
            changed_by: unsafe { caller.map(|caller| caller.deref()) },