Skip to content

Commit

Permalink
Move Resource trait to its own file (#17469)
Browse files Browse the repository at this point in the history
# Objective

`bevy_ecs`'s `system` module is something of a grab bag, and *very*
large. This is particularly true for the `system_param` module, which is
more than 2k lines long!

While it could be defensible to put `Res` and `ResMut` there (lol no
they're in change_detection.rs, obviously), it doesn't make any sense to
put the `Resource` trait there. This is confusing to navigate (and
painful to work on and review).

## Solution

- Create a root level `bevy_ecs/resource.rs` module to mirror
`bevy_ecs/component.rs`
- move the `Resource` trait to that module
- move the `Resource` derive macro to that module as well (Rust really
likes when you pun on the names of the derive macro and trait and put
them in the same path)
- fix all of the imports

## Notes to reviewers

- We could probably move more stuff into here, but I wanted to keep this
PR as small as possible given the absurd level of import changes.
- This PR is ground work for my upcoming attempts to store resource data
on components (resources-as-entities). Splitting this code out will make
the work and review a bit easier, and is the sort of overdue refactor
that's good to do as part of more meaningful work.

## Testing

cargo build works!

## Migration Guide

`bevy_ecs::system::Resource` has been moved to
`bevy_ecs::resource::Resource`.
  • Loading branch information
alice-i-cecile authored Jan 21, 2025
1 parent 85eceb0 commit 44ad3bf
Show file tree
Hide file tree
Showing 110 changed files with 261 additions and 190 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_a11y/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use bevy_app::Plugin;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
prelude::{Component, Event},
resource::Resource,
schedule::SystemSet,
system::Resource,
};

/// Wrapper struct for [`accesskit::ActionRequest`]. Required to allow it to be used as an `Event`.
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_animation/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use bevy_ecs::{
component::Component,
event::EventReader,
reflect::ReflectComponent,
system::{Res, ResMut, Resource},
resource::Resource,
system::{Res, ResMut},
};
use bevy_reflect::{prelude::ReflectDefault, Reflect, ReflectSerialize};
use bevy_utils::HashMap;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,8 +1423,9 @@ mod tests {
event::{Event, EventWriter, Events},
query::With,
removal_detection::RemovedComponents,
resource::Resource,
schedule::{IntoSystemConfigs, ScheduleLabel},
system::{Commands, Query, Resource},
system::{Commands, Query},
world::{FromWorld, World},
};

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_app/src/main_schedule.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{App, Plugin};
use alloc::{vec, vec::Vec};
use bevy_ecs::{
resource::Resource,
schedule::{
ExecutorKind, InternedScheduleLabel, IntoSystemSetConfigs, Schedule, ScheduleLabel,
SystemSet,
},
system::{Local, Resource},
system::Local,
world::{Mut, World},
};

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_app/src/plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl PluginGroup for PluginGroupBuilder {

/// Facilitates the creation and configuration of a [`PluginGroup`].
///
/// Provides a build ordering to ensure that [`Plugin`]s which produce/require a [`Resource`](bevy_ecs::system::Resource)
/// Provides a build ordering to ensure that [`Plugin`]s which produce/require a [`Resource`](bevy_ecs::resource::Resource)
/// are built before/after dependent/depending [`Plugin`]s. [`Plugin`]s inside the group
/// can be disabled, enabled or reordered.
pub struct PluginGroupBuilder {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_asset/src/asset_changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ mod tests {
use bevy_ecs::{
component::Component,
event::EventWriter,
system::{Commands, IntoSystem, Local, Query, Res, ResMut, Resource},
resource::Resource,
system::{Commands, IntoSystem, Local, Query, Res, ResMut},
};
use bevy_reflect::TypePath;

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
use alloc::{sync::Arc, vec::Vec};
use bevy_ecs::{
prelude::EventWriter,
system::{Res, ResMut, Resource, SystemChangeTick},
resource::Resource,
system::{Res, ResMut, SystemChangeTick},
};
use bevy_reflect::{Reflect, TypePath};
use bevy_utils::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/embedded/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::io::{
AssetSource, AssetSourceBuilders,
};
use alloc::boxed::Box;
use bevy_ecs::system::Resource;
use bevy_ecs::resource::Resource;
use std::path::{Path, PathBuf};

#[cfg(feature = "embedded_watcher")]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloc::{
sync::Arc,
};
use atomicow::CowArc;
use bevy_ecs::system::Resource;
use bevy_ecs::resource::Resource;
use bevy_utils::HashMap;
use core::{fmt::Display, hash::Hash, time::Duration};
use thiserror::Error;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_asset/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,8 @@ impl AssetServer {
///
/// ```
/// use bevy_asset::{Assets, Handle, LoadedUntypedAsset};
/// use bevy_ecs::system::{Res, Resource};
/// use bevy_ecs::system::Res;
/// use bevy_ecs::resource::Resource;
///
/// #[derive(Resource)]
/// struct LoadingUntypedHandle(Handle<LoadedUntypedAsset>);
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/bloom/downsampling_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::{Bloom, BLOOM_SHADER_HANDLE, BLOOM_TEXTURE_FORMAT};
use crate::fullscreen_vertex_shader::fullscreen_shader_vertex_state;
use bevy_ecs::{
prelude::{Component, Entity},
system::{Commands, Query, Res, ResMut, Resource},
resource::Resource,
system::{Commands, Query, Res, ResMut},
world::{FromWorld, World},
};
use bevy_math::{Vec2, Vec4};
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/bloom/upsampling_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use super::{
use crate::fullscreen_vertex_shader::fullscreen_shader_vertex_state;
use bevy_ecs::{
prelude::{Component, Entity},
system::{Commands, Query, Res, ResMut, Resource},
resource::Resource,
system::{Commands, Query, Res, ResMut},
world::{FromWorld, World},
};
use bevy_render::{
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/dof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use bevy_ecs::{
entity::Entity,
query::{QueryItem, With},
reflect::ReflectComponent,
resource::Resource,
schedule::IntoSystemConfigs as _,
system::{lifetimeless::Read, Commands, Query, Res, ResMut, Resource},
system::{lifetimeless::Read, Commands, Query, Res, ResMut},
world::{FromWorld, World},
};
use bevy_image::BevyDefault as _;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/motion_blur/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use bevy_ecs::{
component::Component,
entity::Entity,
query::With,
system::{Commands, Query, Res, ResMut, Resource},
resource::Resource,
system::{Commands, Query, Res, ResMut},
world::FromWorld,
};
use bevy_image::BevyDefault as _;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/post_process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use bevy_ecs::{
entity::Entity,
query::{QueryItem, With},
reflect::ReflectComponent,
resource::Resource,
schedule::IntoSystemConfigs as _,
system::{lifetimeless::Read, Commands, Query, Res, ResMut, Resource},
system::{lifetimeless::Read, Commands, Query, Res, ResMut},
world::{FromWorld, World},
};
use bevy_image::{BevyDefault, Image};
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/skybox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use bevy_asset::{load_internal_asset, Handle};
use bevy_ecs::{
prelude::{Component, Entity},
query::{QueryItem, With},
resource::Resource,
schedule::IntoSystemConfigs,
system::{Commands, Query, Res, ResMut, Resource},
system::{Commands, Query, Res, ResMut},
};
use bevy_image::{BevyDefault, Image};
use bevy_math::{Mat4, Quat};
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/skybox/prepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use bevy_ecs::{
component::Component,
entity::Entity,
query::{Has, With},
system::{Commands, Query, Res, ResMut, Resource},
resource::Resource,
system::{Commands, Query, Res, ResMut},
world::{FromWorld, World},
};
use bevy_render::{
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/smaa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ use bevy_ecs::{
entity::Entity,
query::{QueryItem, With},
reflect::ReflectComponent,
resource::Resource,
schedule::IntoSystemConfigs as _,
system::{lifetimeless::Read, Commands, Query, Res, ResMut, Resource},
system::{lifetimeless::Read, Commands, Query, Res, ResMut},
world::{FromWorld, World},
};
use bevy_image::{BevyDefault, Image};
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/taa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use bevy_diagnostic::FrameCount;
use bevy_ecs::{
prelude::{require, Component, Entity, ReflectComponent},
query::{QueryItem, With},
resource::Resource,
schedule::IntoSystemConfigs,
system::{Commands, Query, Res, ResMut, Resource},
system::{Commands, Query, Res, ResMut},
world::{FromWorld, World},
};
use bevy_image::BevyDefault as _;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_dev_tools/src/fps_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use bevy_ecs::{
component::Component,
entity::Entity,
query::With,
resource::Resource,
schedule::{common_conditions::resource_changed, IntoSystemConfigs},
system::{Commands, Query, Res, Resource},
system::{Commands, Query, Res},
};
use bevy_render::view::Visibility;
use bevy_text::{Font, TextColor, TextFont, TextSpan};
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use core::{
};

use bevy_app::{App, SubApp};
use bevy_ecs::system::{Deferred, Res, Resource, SystemBuffer, SystemParam};
use bevy_ecs::resource::Resource;
use bevy_ecs::system::{Deferred, Res, SystemBuffer, SystemParam};
use bevy_platform_support::time::Instant;
use bevy_utils::{HashMap, PassHash};
use const_fnv1a_hash::fnv1a_hash_str_64;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::DiagnosticPath;
use bevy_app::prelude::*;
use bevy_ecs::system::Resource;
use bevy_ecs::resource::Resource;

/// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %)
///
Expand Down Expand Up @@ -64,7 +64,7 @@ pub mod internal {
use std::{sync::Mutex, time::Instant};

use bevy_app::{App, First, Startup, Update};
use bevy_ecs::system::Resource;
use bevy_ecs::resource::Resource;
use bevy_tasks::{available_parallelism, block_on, poll_once, AsyncComputeTaskPool, Task};
use sysinfo::{CpuRefreshKind, MemoryRefreshKind, RefreshKind, System};
use tracing::info;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn derive_resource(input: TokenStream) -> TokenStream {
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();

TokenStream::from(quote! {
impl #impl_generics #bevy_ecs_path::system::Resource for #struct_name #type_generics #where_clause {
impl #impl_generics #bevy_ecs_path::resource::Resource for #struct_name #type_generics #where_clause {
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ struct ArchetypeComponents {
///
/// [`Component`]: crate::component::Component
/// [`World`]: crate::world::World
/// [`Resource`]: crate::system::Resource
/// [`Resource`]: crate::resource::Resource
/// [many-to-many relationship]: https://en.wikipedia.org/wiki/Many-to-many_(data_model)
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ArchetypeComponentId(usize);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
component::{Tick, TickCells},
ptr::PtrMut,
system::Resource,
resource::Resource,
};
use alloc::borrow::ToOwned;
use bevy_ptr::{Ptr, UnsafeCellDeref};
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use crate::{
change_detection::MAX_CHANGE_AGE,
entity::{ComponentCloneCtx, Entity},
query::DebugCheckedUnwrap,
resource::Resource,
storage::{SparseSetIndex, SparseSets, Storages, Table, TableRow},
system::{Local, Resource, SystemParam},
system::{Local, SystemParam},
world::{DeferredWorld, FromWorld, World},
};
#[cfg(feature = "bevy_reflect")]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/event/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate as bevy_ecs;
use alloc::vec::Vec;
use bevy_ecs::{
event::{Event, EventCursor, EventId, EventInstance},
system::Resource,
resource::Resource,
};
#[cfg(feature = "track_location")]
use core::panic::Location;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/event/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_ecs::{
change_detection::{DetectChangesMut, MutUntyped},
component::{ComponentId, Tick},
event::{Event, Events},
system::Resource,
resource::Resource,
world::World,
};

Expand Down
8 changes: 5 additions & 3 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub mod query;
pub mod reflect;
pub mod relationship;
pub mod removal_detection;
pub mod resource;
pub mod result;
pub mod schedule;
pub mod storage;
Expand Down Expand Up @@ -81,6 +82,7 @@ pub mod prelude {
observer::{CloneEntityWithObserversExt, Observer, Trigger},
query::{Added, AnyOf, Changed, Has, Or, QueryBuilder, QueryState, With, Without},
removal_detection::RemovedComponents,
resource::Resource,
result::{Error, Result},
schedule::{
apply_deferred, common_conditions::*, ApplyDeferred, Condition, IntoSystemConfigs,
Expand All @@ -89,7 +91,7 @@ pub mod prelude {
system::{
Command, Commands, Deferred, EntityCommand, EntityCommands, In, InMut, InRef,
IntoSystem, Local, NonSend, NonSendMut, ParamSet, Populated, Query, ReadOnlySystem,
Res, ResMut, Resource, Single, System, SystemIn, SystemInput, SystemParamBuilder,
Res, ResMut, Single, System, SystemIn, SystemInput, SystemParamBuilder,
SystemParamFunction, WithParamWarnPolicy,
},
world::{
Expand Down Expand Up @@ -134,7 +136,7 @@ mod tests {
entity::Entity,
prelude::Or,
query::{Added, Changed, FilteredAccess, QueryFilter, With, Without},
system::Resource,
resource::Resource,
world::{EntityMut, EntityRef, Mut, World},
};
use alloc::{
Expand Down Expand Up @@ -1223,7 +1225,7 @@ mod tests {

#[test]
fn resource() {
use crate::system::Resource;
use crate::resource::Resource;

#[derive(Resource, PartialEq, Debug)]
struct Num(i32);
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/observer/entity_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ mod tests {
entity::EntityCloneBuilder,
event::Event,
observer::{CloneEntityWithObserversExt, Trigger},
system::{ResMut, Resource},
resource::Resource,
system::ResMut,
world::World,
};

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/reflect/entity_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::{
entity::Entity,
prelude::Mut,
reflect::{AppTypeRegistry, ReflectBundle, ReflectComponent},
system::{EntityCommands, Resource},
resource::Resource,
system::EntityCommands,
world::{EntityWorldMut, World},
};
use alloc::{borrow::Cow, boxed::Box};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/reflect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::{
};

use crate as bevy_ecs;
use crate::{system::Resource, world::World};
use crate::{resource::Resource, world::World};
use bevy_reflect::{
std_traits::ReflectDefault, PartialReflect, Reflect, ReflectFromReflect, TypePath,
TypeRegistry, TypeRegistryArc,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/reflect/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::{
change_detection::Mut,
component::ComponentId,
system::Resource,
resource::Resource,
world::{unsafe_world_cell::UnsafeWorldCell, World},
};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};
Expand Down
Loading

0 comments on commit 44ad3bf

Please sign in to comment.