Skip to content

Commit

Permalink
Derive resource everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Jun 13, 2022
1 parent cc0b179 commit 1ec9b0c
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 21 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_app/src/ci_testing.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::{app::AppExit, App};
use serde::Deserialize;

use bevy_ecs::prelude::Resource;
use bevy_utils::tracing::info;

/// A configuration struct for automated CI testing.
///
/// It gets used when the `bevy_ci_testing` feature is enabled to automatically
/// exit a Bevy app when run through the CI. This is needed because otherwise
/// Bevy apps would be stuck in the game loop and wouldn't allow the CI to progress.
#[derive(Deserialize)]
#[derive(Deserialize, Resource)]
pub struct CiTestingConfig {
/// The number of frames after which Bevy should exit.
pub exit_after: Option<u32>,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
plugin::Plugin,
};
use bevy_ecs::event::{Events, ManualEventReader};
use bevy_ecs::prelude::Resource;
use bevy_utils::{Duration, Instant};

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -34,7 +35,7 @@ impl Default for RunMode {
/// The configuration information for the [`ScheduleRunnerPlugin`].
///
/// It gets added as a [`Resource`](bevy_ecs::system::Resource) inside of the [`ScheduleRunnerPlugin`].
#[derive(Copy, Clone, Default)]
#[derive(Copy, Clone, Default, Resource)]
pub struct ScheduleRunnerSettings {
/// Determines whether the [`Schedule`](bevy_ecs::schedule::Schedule) is run once or repeatedly.
pub run_mode: RunMode,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/examples/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {
}

// This struct will be used as a Resource keeping track of the total amount of spawned entities
#[derive(Debug)]
#[derive(Debug, Resource)]
struct EntityCounter {
pub value: i32,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/examples/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
}

// Counter resource to be increased and read by systems
#[derive(Debug)]
#[derive(Debug, Resource)]
struct Counter {
pub value: i32,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Types for declaring and storing [`Component`]s.
use crate::{
pub use crate::{
change_detection::MAX_CHANGE_AGE,
storage::{SparseSetIndex, Storages},
system::Resource,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Event handling types.
use crate as bevy_ecs;
use crate::system::{Local, Res, ResMut, SystemParam};
use crate::system::{Local, Res, ResMut, SystemParam, Resource};
use bevy_utils::tracing::trace;
use std::ops::{Deref, DerefMut};
use std::{
Expand Down Expand Up @@ -128,7 +128,7 @@ struct EventInstance<E: Event> {
/// [Example usage.](https://github.com/bevyengine/bevy/blob/latest/examples/ecs/event.rs)
/// [Example usage standalone.](https://github.com/bevyengine/bevy/blob/latest/bevy_ecs/examples/events.rs)
///
#[derive(Debug)]
#[derive(Debug, Resource)]
pub struct Events<E: Event> {
/// Holds the oldest still active events.
/// Note that a.start_event_count + a.len() should always === events_b.start_event_count.
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_ecs/src/schedule/executor_parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,12 @@ mod tests {

use crate as bevy_ecs;
use crate::component::Component;
use crate::system::Resource;

#[derive(Component)]
struct W<T>(T);
#[derive(Resource)]
struct Counter(usize);

fn receive_events(world: &World) -> Vec<SchedulingEvent> {
let mut events = Vec::new();
Expand Down Expand Up @@ -355,8 +359,8 @@ mod tests {
fn resources() {
let mut world = World::new();
world.insert_resource(0usize);
fn wants_mut(_: ResMut<usize>) {}
fn wants_ref(_: Res<usize>) {}
fn wants_mut(_: ResMut<Counter>) {}
fn wants_ref(_: Res<Counter>) {}
let mut stage = SystemStage::parallel()
.with_system(wants_mut)
.with_system(wants_mut);
Expand Down
13 changes: 7 additions & 6 deletions crates/bevy_ecs/src/schedule/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,12 @@ mod tests {

use crate as bevy_ecs;
use crate::component::Component;
use crate::system::Resource;

#[derive(Component)]
struct W<T>(T);
#[derive(Resource)]
struct R(usize);

fn make_exclusive(tag: usize) -> impl FnMut(&mut World) {
move |world| world.resource_mut::<Vec<usize>>().push(tag)
Expand Down Expand Up @@ -1606,7 +1610,7 @@ mod tests {
}

fn empty() {}
fn resource(_: ResMut<usize>) {}
fn resource(_: ResMut<R>) {}
fn component(_: Query<&mut W<f32>>) {}

let mut world = World::new();
Expand Down Expand Up @@ -1976,15 +1980,12 @@ mod tests {

#[test]
fn archetype_update_single_executor() {
fn query_count_system(
mut entity_count: ResMut<usize>,
query: Query<crate::entity::Entity>,
) {
fn query_count_system(mut entity_count: ResMut<R>, query: Query<crate::entity::Entity>) {
*entity_count = query.iter().count();
}

let mut world = World::new();
world.insert_resource(0_usize);
world.insert_resource(R(0));
let mut stage = SystemStage::single(query_count_system);

let entity = world.spawn().insert_bundle(()).id();
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use crate::{
RunCriteriaDescriptor, RunCriteriaDescriptorCoercion, RunCriteriaLabel, ShouldRun,
SystemSet,
},
system::{In, IntoChainSystem, Local, Res, ResMut},
system::{In, IntoChainSystem, Local, Res, ResMut, Resource},
};
use std::{
any::TypeId,
fmt::{self, Debug},
hash::Hash,
};
// Required for derive macros
use crate as bevy_ecs;

pub trait StateData: Send + Sync + Clone + Eq + Debug + Hash + 'static {}
impl<T> StateData for T where T: Send + Sync + Clone + Eq + Debug + Hash + 'static {}
Expand All @@ -21,7 +23,7 @@ impl<T> StateData for T where T: Send + Sync + Clone + Eq + Debug + Hash + 'stat
/// * Pop removes the current state, and unpauses the last paused state
/// * Set replaces the active state with a new one
/// * Replace unwinds the state stack, and replaces the entire stack with a single new state
#[derive(Debug)]
#[derive(Debug, Resource)]
pub struct State<T: StateData> {
transition: Option<StateTransition<T>>,
/// The current states in the stack.
Expand Down
17 changes: 13 additions & 4 deletions crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ mod tests {
schedule::{Schedule, Stage, SystemStage},
system::{
Commands, IntoExclusiveSystem, IntoSystem, Local, NonSend, NonSendMut, ParamSet, Query,
RemovedComponents, Res, ResMut, System, SystemState,
RemovedComponents, Res, ResMut, Resource, System, SystemState,
},
world::{FromWorld, World},
};
Expand Down Expand Up @@ -244,10 +244,19 @@ mod tests {

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

#[derive(Resource)]
struct Flipper(bool);

#[derive(Resource)]
struct Added(usize);

#[derive(Resource)]
struct Changed(usize);

fn incr_e_on_flip(
value: Res<bool>,
value: Res<Flipper>,
mut changed: ResMut<Changed>,
mut added: ResMut<Added>,
) {
Expand All @@ -261,7 +270,7 @@ mod tests {
}

let mut world = World::default();
world.insert_resource(false);
world.insert_resource(Flipper(false));
world.insert_resource(Added(0));
world.insert_resource(Changed(0));

Expand Down Expand Up @@ -406,7 +415,7 @@ mod tests {
run_system(&mut world, sys);
}

#[derive(Default)]
#[derive(Default, Resource)]
struct BufferRes {
_buffer: Vec<u8>,
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/src/system/system_param.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use crate::change_detection::{NonSendMut, ResMut};
pub use bevy_ecs_macros::Resource;
use crate::{
archetype::{Archetype, Archetypes},
bundle::Bundles,
Expand Down

0 comments on commit 1ec9b0c

Please sign in to comment.