Skip to content
Closed
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
2 changes: 1 addition & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ impl App {
/// Gets read-only access to the [`Schedule`] with the provided `label` if it exists.
pub fn get_schedule(&self, label: impl ScheduleLabel) -> Option<&Schedule> {
let schedules = self.world.get_resource::<Schedules>()?;
schedules.get(&label)
schedules.into_inner().get(&label)
}

/// Gets read-write access to a [`Schedule`] with the provided `label` if it exists.
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_app/src/main_schedule.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{App, Plugin};
use bevy_ecs::{
schedule::{ExecutorKind, Schedule, ScheduleLabel},
system::{Local, Resource},
world::{Mut, World},
system::{Local, ResMut, Resource},
world::World,
};

/// The schedule that contains the app logic that is evaluated each tick of [`App::update()`].
Expand Down Expand Up @@ -147,7 +147,7 @@ impl Main {
*run_at_least_once = true;
}

world.resource_scope(|world, order: Mut<MainScheduleOrder>| {
world.resource_scope(|world, order: ResMut<MainScheduleOrder>| {
for label in &order.labels {
let _ = world.try_run_schedule(&**label);
}
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ mod tests {
const LARGE_ITERATION_COUNT: usize = 10000;

fn get<A: Asset>(world: &World, id: AssetId<A>) -> Option<&A> {
world.resource::<Assets<A>>().get(id)
world.resource::<Assets<A>>().into_inner().get(id)
}

#[derive(Resource, Default)]
Expand Down Expand Up @@ -1018,6 +1018,7 @@ mod tests {
let text = app
.world
.resource::<Assets<CoolText>>()
.into_inner()
.get(&handle)
.unwrap();
assert_eq!(text.text, hello);
Expand Down Expand Up @@ -1129,7 +1130,7 @@ mod tests {
let asset_server = world.resource::<AssetServer>();
let loaded_folders = world.resource::<Assets<LoadedFolder>>();
let cool_texts = world.resource::<Assets<CoolText>>();
for event in reader.read(events) {
for event in reader.read(&events) {
if let AssetEvent::LoadedWithDependencies { id } = event {
if *id == handle.id() {
let loaded_folder = loaded_folders.get(&handle).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
handle_type_id: TypeId::of::<Handle<A>>(),
assets_resource_type_id: TypeId::of::<Assets<A>>(),
get: |world, handle| {
let assets = world.resource::<Assets<A>>();
let assets = world.resource::<Assets<A>>().into_inner();
let asset = assets.get(&handle.typed_debug_checked());
asset.map(|asset| asset as &dyn Reflect)
},
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
assets.len()
},
ids: |world| {
let assets = world.resource::<Assets<A>>();
let assets = world.resource::<Assets<A>>().into_inner();
Box::new(assets.ids().map(|i| i.untyped()))
},
remove: |world, handle| {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ impl AssetServer {

/// A system that manages internal [`AssetServer`] events, such as finalizing asset loads.
pub fn handle_internal_asset_events(world: &mut World) {
world.resource_scope(|world, server: Mut<AssetServer>| {
world.resource_scope(|world, server: ResMut<AssetServer>| {
let mut infos = server.data.infos.write();
for event in server.data.asset_event_receiver.try_iter() {
match event {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl ViewNode for MainOpaquePass3dNode {
if let (Some(skybox_pipeline), Some(skybox_bind_group)) =
(skybox_pipeline, skybox_bind_group)
{
let pipeline_cache = world.resource::<PipelineCache>();
let pipeline_cache = world.resource::<PipelineCache>().into_inner();
if let Some(pipeline) = pipeline_cache.get_render_pipeline(skybox_pipeline.0) {
render_pass.set_render_pipeline(pipeline);
render_pass.set_bind_group(0, &skybox_bind_group.0, &[view_uniform_offset.offset]);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/tonemapping/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ impl ViewNode for TonemappingNode {
];

entries.extend(get_lut_bindings(
gpu_images,
tonemapping_luts,
&gpu_images,
&tonemapping_luts,
tonemapping,
[3, 4],
));
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ mod tests {
component::{Component, ComponentId},
entity::Entity,
query::{Added, Changed, FilteredAccess, ReadOnlyWorldQuery, With, Without},
system::Resource,
world::{EntityRef, Mut, World},
system::{ResMut, Resource},
world::{EntityRef, World},
};
use bevy_tasks::{ComputeTaskPool, TaskPool};
use std::{
Expand Down Expand Up @@ -1145,7 +1145,7 @@ mod tests {
"removed resource has the correct value"
);
assert_eq!(
world.get_resource::<BigNum>(),
world.get_resource::<BigNum>().map(|res| res.into_inner()),
None,
"removed resource no longer exists"
);
Expand All @@ -1157,13 +1157,13 @@ mod tests {

world.insert_resource(BigNum(1));
assert_eq!(
world.get_resource::<BigNum>(),
world.get_resource::<BigNum>().map(|res| res.into_inner()),
Some(&BigNum(1)),
"re-inserting resources works"
);

assert_eq!(
world.get_resource::<Num>(),
world.get_resource::<Num>().map(|res| res.into_inner()),
Some(&Num(123)),
"other resources are unaffected"
);
Expand Down Expand Up @@ -1409,7 +1409,7 @@ mod tests {
fn resource_scope() {
let mut world = World::default();
world.insert_resource(A(0));
world.resource_scope(|world: &mut World, mut value: Mut<A>| {
world.resource_scope(|world: &mut World, mut value: ResMut<A>| {
value.0 += 1;
assert!(!world.contains_resource::<A>());
});
Expand Down
7 changes: 3 additions & 4 deletions crates/bevy_ecs/src/reflect/entity_commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::Mut;
use crate::reflect::AppTypeRegistry;
use crate::system::{Command, EntityCommands, Resource};
use crate::system::{Command, EntityCommands, ResMut, Resource};
use crate::{entity::Entity, reflect::ReflectComponent, world::World};
use bevy_reflect::{Reflect, TypeRegistry};
use std::borrow::Cow;
Expand Down Expand Up @@ -234,7 +233,7 @@ pub struct InsertReflectWithRegistry<T: Resource + AsRef<TypeRegistry>> {

impl<T: Resource + AsRef<TypeRegistry>> Command for InsertReflectWithRegistry<T> {
fn apply(self, world: &mut World) {
world.resource_scope(|world, registry: Mut<T>| {
world.resource_scope(|world, registry: ResMut<T>| {
let registry: &TypeRegistry = registry.as_ref().as_ref();
insert_reflect(world, self.entity, registry, self.component);
});
Expand Down Expand Up @@ -299,7 +298,7 @@ pub struct RemoveReflectWithRegistry<T: Resource + AsRef<TypeRegistry>> {

impl<T: Resource + AsRef<TypeRegistry>> Command for RemoveReflectWithRegistry<T> {
fn apply(self, world: &mut World) {
world.resource_scope(|world, registry: Mut<T>| {
world.resource_scope(|world, registry: ResMut<T>| {
let registry: &TypeRegistry = registry.as_ref().as_ref();
remove_reflect(world, self.entity, registry, self.component_type_name);
});
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_ecs/src/reflect/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ impl<C: Resource + Reflect + FromWorld> FromType<C> for ReflectResource {
remove: |world| {
world.remove_resource::<C>();
},
reflect: |world| world.get_resource::<C>().map(|res| res as &dyn Reflect),
reflect: |world| {
world
.get_resource::<C>()
.map(|res| res.into_inner() as &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 All @@ -200,7 +204,7 @@ impl<C: Resource + Reflect + FromWorld> FromType<C> for ReflectResource {
}
},
copy: |source_world, destination_world| {
let source_resource = source_world.resource::<C>();
let source_resource = source_world.resource::<C>().into_inner();
let mut destination_resource = C::from_world(destination_world);
destination_resource.apply(source_resource);
destination_world.insert_resource(destination_resource);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl SystemMeta {
/// });
///
/// // Later, fetch the cached system state, saving on overhead
/// world.resource_scope(|world, mut cached_state: Mut<CachedSystemState>| {
/// world.resource_scope(|world, mut cached_state: ResMut<CachedSystemState>| {
/// let mut event_reader = cached_state.event_state.get_mut(world);
///
/// for events in event_reader.iter() {
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ mod tests {
impl FromWorld for Foo {
fn from_world(world: &mut World) -> Self {
Foo {
value: world.resource::<ProtoFoo>().value + 1,
value: world.resource::<ProtoFoo>().into_inner().value + 1,
}
}
}
Expand Down Expand Up @@ -1934,6 +1934,9 @@ mod tests {
);
sched.initialize(&mut world).unwrap();
sched.run(&mut world);
assert_eq!(world.get_resource(), Some(&C(3)));
assert_eq!(
world.get_resource::<C>().map(|res| res.into_inner()),
Some(&C(3))
);
}
}
30 changes: 18 additions & 12 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
removal_detection::RemovedComponentEvents,
schedule::{Schedule, ScheduleLabel, Schedules},
storage::{ResourceData, Storages},
system::Resource,
system::{Res, ResMut, Resource},
world::error::TryRunScheduleError,
};
use bevy_ptr::{OwningPtr, Ptr};
Expand Down Expand Up @@ -1221,7 +1221,7 @@ impl World {
/// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with).
#[inline]
#[track_caller]
pub fn resource<R: Resource>(&self) -> &R {
pub fn resource<R: Resource>(&self) -> Res<'_, R> {
match self.get_resource() {
Some(x) => x,
None => panic!(
Expand All @@ -1245,7 +1245,7 @@ impl World {
/// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with).
#[inline]
#[track_caller]
pub fn resource_mut<R: Resource>(&mut self) -> Mut<'_, R> {
pub fn resource_mut<R: Resource>(&mut self) -> ResMut<'_, R> {
match self.get_resource_mut() {
Some(x) => x,
None => panic!(
Expand All @@ -1260,20 +1260,20 @@ impl World {

/// Gets a reference to the resource of the given type if it exists
#[inline]
pub fn get_resource<R: Resource>(&self) -> Option<&R> {
pub fn get_resource<R: Resource>(&self) -> Option<Res<'_, R>> {
// SAFETY:
// - `as_unsafe_world_cell_readonly` gives permission to access everything immutably
// - `&self` ensures nothing in world is borrowed immutably
unsafe { self.as_unsafe_world_cell_readonly().get_resource() }
unsafe { self.as_unsafe_world_cell_readonly().get_resource::<R>() }
}

/// Gets a mutable reference to the resource of the given type if it exists
#[inline]
pub fn get_resource_mut<R: Resource>(&mut self) -> Option<Mut<'_, R>> {
pub fn get_resource_mut<R: Resource>(&mut self) -> Option<ResMut<'_, R>> {
// SAFETY:
// - `as_unsafe_world_cell` gives permission to access everything mutably
// - `&mut self` ensures nothing in world is borrowed
unsafe { self.as_unsafe_world_cell().get_resource_mut() }
unsafe { self.as_unsafe_world_cell().get_resource_mut::<R>() }
}

/// Gets a mutable reference to the resource of type `T` if it exists,
Expand All @@ -1282,7 +1282,7 @@ impl World {
pub fn get_resource_or_insert_with<R: Resource>(
&mut self,
func: impl FnOnce() -> R,
) -> Mut<'_, R> {
) -> ResMut<'_, R> {
let change_tick = self.change_tick();
let last_change_tick = self.last_change_tick();

Expand All @@ -1303,7 +1303,10 @@ impl World {
.debug_checked_unwrap()
};
// SAFETY: The underlying type of the resource is `R`.
unsafe { data.with_type::<R>() }
let Mut { value, ticks } = unsafe { data.with_type::<R>() };

// TODO: Maybe `impl<R: Resource> From<Mut<R>> for ResMut<R>`?
ResMut { value, ticks }
}

/// Gets an immutable reference to the non-send resource of the given type, if it exists.
Expand Down Expand Up @@ -1536,13 +1539,16 @@ impl World {
/// world.insert_resource(A(1));
/// let entity = world.spawn(B(1)).id();
///
/// world.resource_scope(|world, mut a: Mut<A>| {
/// world.resource_scope(|world, mut a: ResMut<A>| {
/// let b = world.get_mut::<B>(entity).unwrap();
/// a.0 += b.0;
/// });
/// assert_eq!(world.get_resource::<A>().unwrap().0, 2);
/// ```
pub fn resource_scope<R: Resource, U>(&mut self, f: impl FnOnce(&mut World, Mut<R>) -> U) -> U {
pub fn resource_scope<R: Resource, U>(
&mut self,
f: impl FnOnce(&mut World, ResMut<R>) -> U,
) -> U {
let last_change_tick = self.last_change_tick();
let change_tick = self.change_tick();

Expand All @@ -1559,7 +1565,7 @@ impl World {
// Read the value onto the stack to avoid potential mut aliasing.
// SAFETY: `ptr` was obtained from the TypeId of `R`.
let mut value = unsafe { ptr.read::<R>() };
let value_mut = Mut {
let value_mut = ResMut {
value: &mut value,
ticks: TicksMut {
added: &mut ticks.added,
Expand Down
Loading