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 14, 2022
1 parent eb506fa commit 24d7c09
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 268 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
85 changes: 47 additions & 38 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod tests {
component::{Component, ComponentId},
entity::Entity,
query::{Added, ChangeTrackers, Changed, FilteredAccess, With, Without, WorldQuery},
system::Resource,
world::{Mut, World},
};
use bevy_tasks::{ComputeTaskPool, TaskPool};
Expand All @@ -68,7 +69,7 @@ mod tests {
},
};

#[derive(Component, Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Component, Resource, Debug, PartialEq, Eq, Clone, Copy)]
struct A(usize);
#[derive(Component, Debug, PartialEq, Eq, Clone, Copy)]
struct B(usize);
Expand Down Expand Up @@ -1002,78 +1003,86 @@ mod tests {

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

#[derive(Resource, PartialEq, Debug)]
struct Num(i32);

#[derive(Resource, PartialEq, Debug)]
struct BigNum(u64);

let mut world = World::default();
assert!(world.get_resource::<i32>().is_none());
assert!(!world.contains_resource::<i32>());
assert!(!world.is_resource_added::<i32>());
assert!(!world.is_resource_changed::<i32>());
assert!(world.get_resource::<Num>().is_none());
assert!(!world.contains_resource::<Num>());
assert!(!world.is_resource_added::<Num>());
assert!(!world.is_resource_changed::<Num>());

world.insert_resource(123);
world.insert_resource(Num(123));
let resource_id = world
.components()
.get_resource_id(TypeId::of::<i32>())
.get_resource_id(TypeId::of::<Num>())
.unwrap();
let archetype_component_id = world
.archetypes()
.resource()
.get_archetype_component_id(resource_id)
.unwrap();

assert_eq!(*world.resource::<i32>(), 123);
assert!(world.contains_resource::<i32>());
assert!(world.is_resource_added::<i32>());
assert!(world.is_resource_changed::<i32>());
assert_eq!(world.resource::<Num>().0, 123);
assert!(world.contains_resource::<Num>());
assert!(world.is_resource_added::<Num>());
assert!(world.is_resource_changed::<Num>());

world.insert_resource(456u64);
assert_eq!(*world.resource::<u64>(), 456u64);
world.insert_resource(BigNum(456));
assert_eq!(world.resource::<BigNum>().0, 456u64);

world.insert_resource(789u64);
assert_eq!(*world.resource::<u64>(), 789);
world.insert_resource(BigNum(789));
assert_eq!(world.resource::<BigNum>().0, 789);

{
let mut value = world.resource_mut::<u64>();
assert_eq!(*value, 789);
*value = 10;
let mut value = world.resource_mut::<BigNum>();
assert_eq!(value.0, 789);
value.0 = 10;
}

assert_eq!(
world.resource::<u64>(),
&10,
world.resource::<BigNum>().0,
10,
"resource changes are preserved"
);

assert_eq!(
world.remove_resource::<u64>(),
Some(10),
world.remove_resource::<BigNum>(),
Some(BigNum(10)),
"removed resource has the correct value"
);
assert_eq!(
world.get_resource::<u64>(),
world.get_resource::<BigNum>(),
None,
"removed resource no longer exists"
);
assert_eq!(
world.remove_resource::<u64>(),
world.remove_resource::<BigNum>(),
None,
"double remove returns nothing"
);

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

assert_eq!(
world.get_resource::<i32>(),
Some(&123),
world.get_resource::<Num>(),
Some(&Num(123)),
"other resources are unaffected"
);

let current_resource_id = world
.components()
.get_resource_id(TypeId::of::<i32>())
.get_resource_id(TypeId::of::<Num>())
.unwrap();
assert_eq!(
resource_id, current_resource_id,
Expand Down Expand Up @@ -1119,7 +1128,7 @@ mod tests {
assert_eq!(
e.get::<A>(),
None,
"i32 is in the removed bundle, so it should not exist"
"Num is in the removed bundle, so it should not exist"
);
assert_eq!(
e.get::<B>(),
Expand Down Expand Up @@ -1324,12 +1333,12 @@ mod tests {
#[test]
fn resource_scope() {
let mut world = World::default();
world.insert_resource::<i32>(0);
world.resource_scope(|world: &mut World, mut value: Mut<i32>| {
*value += 1;
assert!(!world.contains_resource::<i32>());
world.insert_resource(A(0));
world.resource_scope(|world: &mut World, mut value: Mut<A>| {
value.0 += 1;
assert!(!world.contains_resource::<A>());
});
assert_eq!(*world.resource::<i32>(), 1);
assert_eq!(world.resource::<A>().0, 1);
}

#[test]
Expand Down Expand Up @@ -1366,7 +1375,7 @@ mod tests {
fn clear_entities() {
let mut world = World::default();

world.insert_resource::<i32>(0);
world.insert_resource(A(0));
world.spawn().insert(A(1));
world.spawn().insert(SparseStored(1));

Expand Down Expand Up @@ -1395,7 +1404,7 @@ mod tests {
"world should not have any entities"
);
assert_eq!(
*world.resource::<i32>(),
world.resource::<A>().0,
0,
"world should still contain resources"
);
Expand Down
10 changes: 7 additions & 3 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, Default)]
struct Counter(usize);

fn receive_events(world: &World) -> Vec<SchedulingEvent> {
let mut events = Vec::new();
Expand Down Expand Up @@ -354,9 +358,9 @@ mod tests {
#[test]
fn resources() {
let mut world = World::new();
world.insert_resource(0usize);
fn wants_mut(_: ResMut<usize>) {}
fn wants_ref(_: Res<usize>) {}
world.init_resource::<Counter>();
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
Loading

0 comments on commit 24d7c09

Please sign in to comment.