Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions crates/bevy_ecs/src/reflect/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
change_detection::Mut,
component::ComponentId,
resource::Resource,
world::{unsafe_world_cell::UnsafeWorldCell, World},
world::{unsafe_world_cell::UnsafeWorldCell, FilteredResources, FilteredResourcesMut, World},
};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};

Expand Down Expand Up @@ -52,7 +52,9 @@ pub struct ReflectResourceFns {
/// Function pointer implementing [`ReflectResource::remove()`].
pub remove: fn(&mut World),
/// Function pointer implementing [`ReflectResource::reflect()`].
pub reflect: fn(&World) -> Option<&dyn Reflect>,
pub reflect: for<'w> fn(FilteredResources<'w, '_>) -> Option<&'w dyn Reflect>,
/// Function pointer implementing [`ReflectResource::reflect_mut()`].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cool if this could be a static assertion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what you want asserted? I was trying to copy the style of the existing comments, and I think that's meant to be read as "ReflectResource::reflect_mut() invokes this function pointer".

pub reflect_mut: for<'w> fn(FilteredResourcesMut<'w, '_>) -> Option<Mut<'w, dyn Reflect>>,
/// Function pointer implementing [`ReflectResource::reflect_unchecked_mut()`].
///
/// # Safety
Expand Down Expand Up @@ -111,14 +113,23 @@ impl ReflectResource {
}

/// Gets the value of this [`Resource`] type from the world as a reflected reference.
pub fn reflect<'a>(&self, world: &'a World) -> Option<&'a dyn Reflect> {
(self.0.reflect)(world)
///
/// Note that [`&World`](World) is a valid type for `resources`.
pub fn reflect<'w, 's>(
&self,
resources: impl Into<FilteredResources<'w, 's>>,
) -> Option<&'w dyn Reflect> {
(self.0.reflect)(resources.into())
}

/// Gets the value of this [`Resource`] type from the world as a mutable reflected reference.
pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option<Mut<'a, dyn Reflect>> {
// SAFETY: unique world access
unsafe { (self.0.reflect_unchecked_mut)(world.as_unsafe_world_cell()) }
///
/// Note that [`&mut World`](World) is a valid type for `resources`.
pub fn reflect_mut<'w, 's>(
&self,
resources: impl Into<FilteredResourcesMut<'w, 's>>,
) -> Option<Mut<'w, dyn Reflect>> {
(self.0.reflect_mut)(resources.into())
}

/// # Safety
Expand Down Expand Up @@ -212,7 +223,12 @@ impl<R: Resource + FromReflect + TypePath> FromType<R> for ReflectResource {
remove: |world| {
world.remove_resource::<R>();
},
reflect: |world| world.get_resource::<R>().map(|res| res as &dyn Reflect),
reflect: |world| world.get::<R>().map(|res| res.into_inner() as &dyn Reflect),
reflect_mut: |world| {
world
.into_mut::<R>()
.map(|res| res.map_unchanged(|value| value as &mut dyn Reflect))
},
reflect_unchecked_mut: |world| {
// SAFETY: all usages of `reflect_unchecked_mut` guarantee that there is either a single mutable
// reference or multiple immutable ones alive at any given point
Expand Down
36 changes: 34 additions & 2 deletions crates/bevy_ecs/src/system/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,9 +715,11 @@ mod tests {
use crate::{
entity::Entities,
prelude::{Component, Query},
reflect::ReflectResource,
system::{Local, RunSystemOnce},
};
use alloc::vec;
use bevy_reflect::{FromType, Reflect, ReflectRef};

use super::*;

Expand All @@ -730,8 +732,11 @@ mod tests {
#[derive(Component)]
struct C;

#[derive(Resource, Default)]
struct R;
#[derive(Resource, Default, Reflect)]
#[reflect(Resource)]
struct R {
foo: usize,
}

fn local_system(local: Local<u64>) -> u64 {
*local
Expand Down Expand Up @@ -1071,4 +1076,31 @@ mod tests {
.build_state(&mut world)
.build_system(|_r: ResMut<R>, _fr: FilteredResourcesMut| {});
}

#[test]
fn filtered_resource_reflect() {
let mut world = World::new();
world.insert_resource(R { foo: 7 });

let system = (FilteredResourcesParamBuilder::new(|builder| {
builder.add_read::<R>();
}),)
.build_state(&mut world)
.build_system(|res: FilteredResources| {
let reflect_resource = <ReflectResource as FromType<R>>::from_type();
let ReflectRef::Struct(reflect_struct) =
reflect_resource.reflect(res).unwrap().reflect_ref()
else {
panic!()
};
*reflect_struct
.field("foo")
.unwrap()
.try_downcast_ref::<usize>()
.unwrap()
});

let output = world.run_system_once(system).unwrap();
assert_eq!(output, 7);
}
}
Loading