Skip to content

Commit 24d7c09

Browse files
Derive resource everywhere
1 parent eb506fa commit 24d7c09

16 files changed

+341
-268
lines changed

crates/bevy_app/src/ci_testing.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use crate::{app::AppExit, App};
22
use serde::Deserialize;
33

4+
use bevy_ecs::prelude::Resource;
45
use bevy_utils::tracing::info;
56

67
/// A configuration struct for automated CI testing.
78
///
89
/// It gets used when the `bevy_ci_testing` feature is enabled to automatically
910
/// exit a Bevy app when run through the CI. This is needed because otherwise
1011
/// Bevy apps would be stuck in the game loop and wouldn't allow the CI to progress.
11-
#[derive(Deserialize)]
12+
#[derive(Deserialize, Resource)]
1213
pub struct CiTestingConfig {
1314
/// The number of frames after which Bevy should exit.
1415
pub exit_after: Option<u32>,

crates/bevy_app/src/schedule_runner.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
plugin::Plugin,
44
};
55
use bevy_ecs::event::{Events, ManualEventReader};
6+
use bevy_ecs::prelude::Resource;
67
use bevy_utils::{Duration, Instant};
78

89
#[cfg(target_arch = "wasm32")]
@@ -34,7 +35,7 @@ impl Default for RunMode {
3435
/// The configuration information for the [`ScheduleRunnerPlugin`].
3536
///
3637
/// It gets added as a [`Resource`](bevy_ecs::system::Resource) inside of the [`ScheduleRunnerPlugin`].
37-
#[derive(Copy, Clone, Default)]
38+
#[derive(Copy, Clone, Default, Resource)]
3839
pub struct ScheduleRunnerSettings {
3940
/// Determines whether the [`Schedule`](bevy_ecs::schedule::Schedule) is run once or repeatedly.
4041
pub run_mode: RunMode,

crates/bevy_ecs/examples/change_detection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() {
4040
}
4141

4242
// This struct will be used as a Resource keeping track of the total amount of spawned entities
43-
#[derive(Debug)]
43+
#[derive(Debug, Resource)]
4444
struct EntityCounter {
4545
pub value: i32,
4646
}

crates/bevy_ecs/examples/resources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn main() {
2727
}
2828

2929
// Counter resource to be increased and read by systems
30-
#[derive(Debug)]
30+
#[derive(Debug, Resource)]
3131
struct Counter {
3232
pub value: i32,
3333
}

crates/bevy_ecs/src/component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Types for declaring and storing [`Component`]s.
22
3-
use crate::{
3+
pub use crate::{
44
change_detection::MAX_CHANGE_AGE,
55
storage::{SparseSetIndex, Storages},
66
system::Resource,

crates/bevy_ecs/src/event.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Event handling types.
22
33
use crate as bevy_ecs;
4-
use crate::system::{Local, Res, ResMut, SystemParam};
4+
use crate::system::{Local, Res, ResMut, SystemParam, Resource};
55
use bevy_utils::tracing::trace;
66
use std::ops::{Deref, DerefMut};
77
use std::{
@@ -128,7 +128,7 @@ struct EventInstance<E: Event> {
128128
/// [Example usage.](https://github.com/bevyengine/bevy/blob/latest/examples/ecs/event.rs)
129129
/// [Example usage standalone.](https://github.com/bevyengine/bevy/blob/latest/bevy_ecs/examples/events.rs)
130130
///
131-
#[derive(Debug)]
131+
#[derive(Debug, Resource)]
132132
pub struct Events<E: Event> {
133133
/// Holds the oldest still active events.
134134
/// Note that a.start_event_count + a.len() should always === events_b.start_event_count.

crates/bevy_ecs/src/lib.rs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mod tests {
5757
component::{Component, ComponentId},
5858
entity::Entity,
5959
query::{Added, ChangeTrackers, Changed, FilteredAccess, With, Without, WorldQuery},
60+
system::Resource,
6061
world::{Mut, World},
6162
};
6263
use bevy_tasks::{ComputeTaskPool, TaskPool};
@@ -68,7 +69,7 @@ mod tests {
6869
},
6970
};
7071

71-
#[derive(Component, Debug, PartialEq, Eq, Clone, Copy)]
72+
#[derive(Component, Resource, Debug, PartialEq, Eq, Clone, Copy)]
7273
struct A(usize);
7374
#[derive(Component, Debug, PartialEq, Eq, Clone, Copy)]
7475
struct B(usize);
@@ -1002,78 +1003,86 @@ mod tests {
10021003

10031004
#[test]
10041005
fn resource() {
1006+
use crate::system::Resource;
1007+
1008+
#[derive(Resource, PartialEq, Debug)]
1009+
struct Num(i32);
1010+
1011+
#[derive(Resource, PartialEq, Debug)]
1012+
struct BigNum(u64);
1013+
10051014
let mut world = World::default();
1006-
assert!(world.get_resource::<i32>().is_none());
1007-
assert!(!world.contains_resource::<i32>());
1008-
assert!(!world.is_resource_added::<i32>());
1009-
assert!(!world.is_resource_changed::<i32>());
1015+
assert!(world.get_resource::<Num>().is_none());
1016+
assert!(!world.contains_resource::<Num>());
1017+
assert!(!world.is_resource_added::<Num>());
1018+
assert!(!world.is_resource_changed::<Num>());
10101019

1011-
world.insert_resource(123);
1020+
world.insert_resource(Num(123));
10121021
let resource_id = world
10131022
.components()
1014-
.get_resource_id(TypeId::of::<i32>())
1023+
.get_resource_id(TypeId::of::<Num>())
10151024
.unwrap();
10161025
let archetype_component_id = world
10171026
.archetypes()
10181027
.resource()
10191028
.get_archetype_component_id(resource_id)
10201029
.unwrap();
10211030

1022-
assert_eq!(*world.resource::<i32>(), 123);
1023-
assert!(world.contains_resource::<i32>());
1024-
assert!(world.is_resource_added::<i32>());
1025-
assert!(world.is_resource_changed::<i32>());
1031+
assert_eq!(world.resource::<Num>().0, 123);
1032+
assert!(world.contains_resource::<Num>());
1033+
assert!(world.is_resource_added::<Num>());
1034+
assert!(world.is_resource_changed::<Num>());
10261035

1027-
world.insert_resource(456u64);
1028-
assert_eq!(*world.resource::<u64>(), 456u64);
1036+
world.insert_resource(BigNum(456));
1037+
assert_eq!(world.resource::<BigNum>().0, 456u64);
10291038

1030-
world.insert_resource(789u64);
1031-
assert_eq!(*world.resource::<u64>(), 789);
1039+
world.insert_resource(BigNum(789));
1040+
assert_eq!(world.resource::<BigNum>().0, 789);
10321041

10331042
{
1034-
let mut value = world.resource_mut::<u64>();
1035-
assert_eq!(*value, 789);
1036-
*value = 10;
1043+
let mut value = world.resource_mut::<BigNum>();
1044+
assert_eq!(value.0, 789);
1045+
value.0 = 10;
10371046
}
10381047

10391048
assert_eq!(
1040-
world.resource::<u64>(),
1041-
&10,
1049+
world.resource::<BigNum>().0,
1050+
10,
10421051
"resource changes are preserved"
10431052
);
10441053

10451054
assert_eq!(
1046-
world.remove_resource::<u64>(),
1047-
Some(10),
1055+
world.remove_resource::<BigNum>(),
1056+
Some(BigNum(10)),
10481057
"removed resource has the correct value"
10491058
);
10501059
assert_eq!(
1051-
world.get_resource::<u64>(),
1060+
world.get_resource::<BigNum>(),
10521061
None,
10531062
"removed resource no longer exists"
10541063
);
10551064
assert_eq!(
1056-
world.remove_resource::<u64>(),
1065+
world.remove_resource::<BigNum>(),
10571066
None,
10581067
"double remove returns nothing"
10591068
);
10601069

1061-
world.insert_resource(1u64);
1070+
world.insert_resource(BigNum(1));
10621071
assert_eq!(
1063-
world.get_resource::<u64>(),
1064-
Some(&1u64),
1072+
world.get_resource::<BigNum>(),
1073+
Some(&BigNum(1)),
10651074
"re-inserting resources works"
10661075
);
10671076

10681077
assert_eq!(
1069-
world.get_resource::<i32>(),
1070-
Some(&123),
1078+
world.get_resource::<Num>(),
1079+
Some(&Num(123)),
10711080
"other resources are unaffected"
10721081
);
10731082

10741083
let current_resource_id = world
10751084
.components()
1076-
.get_resource_id(TypeId::of::<i32>())
1085+
.get_resource_id(TypeId::of::<Num>())
10771086
.unwrap();
10781087
assert_eq!(
10791088
resource_id, current_resource_id,
@@ -1119,7 +1128,7 @@ mod tests {
11191128
assert_eq!(
11201129
e.get::<A>(),
11211130
None,
1122-
"i32 is in the removed bundle, so it should not exist"
1131+
"Num is in the removed bundle, so it should not exist"
11231132
);
11241133
assert_eq!(
11251134
e.get::<B>(),
@@ -1324,12 +1333,12 @@ mod tests {
13241333
#[test]
13251334
fn resource_scope() {
13261335
let mut world = World::default();
1327-
world.insert_resource::<i32>(0);
1328-
world.resource_scope(|world: &mut World, mut value: Mut<i32>| {
1329-
*value += 1;
1330-
assert!(!world.contains_resource::<i32>());
1336+
world.insert_resource(A(0));
1337+
world.resource_scope(|world: &mut World, mut value: Mut<A>| {
1338+
value.0 += 1;
1339+
assert!(!world.contains_resource::<A>());
13311340
});
1332-
assert_eq!(*world.resource::<i32>(), 1);
1341+
assert_eq!(world.resource::<A>().0, 1);
13331342
}
13341343

13351344
#[test]
@@ -1366,7 +1375,7 @@ mod tests {
13661375
fn clear_entities() {
13671376
let mut world = World::default();
13681377

1369-
world.insert_resource::<i32>(0);
1378+
world.insert_resource(A(0));
13701379
world.spawn().insert(A(1));
13711380
world.spawn().insert(SparseStored(1));
13721381

@@ -1395,7 +1404,7 @@ mod tests {
13951404
"world should not have any entities"
13961405
);
13971406
assert_eq!(
1398-
*world.resource::<i32>(),
1407+
world.resource::<A>().0,
13991408
0,
14001409
"world should still contain resources"
14011410
);

crates/bevy_ecs/src/schedule/executor_parallel.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,12 @@ mod tests {
324324

325325
use crate as bevy_ecs;
326326
use crate::component::Component;
327+
use crate::system::Resource;
328+
327329
#[derive(Component)]
328330
struct W<T>(T);
331+
#[derive(Resource, Default)]
332+
struct Counter(usize);
329333

330334
fn receive_events(world: &World) -> Vec<SchedulingEvent> {
331335
let mut events = Vec::new();
@@ -354,9 +358,9 @@ mod tests {
354358
#[test]
355359
fn resources() {
356360
let mut world = World::new();
357-
world.insert_resource(0usize);
358-
fn wants_mut(_: ResMut<usize>) {}
359-
fn wants_ref(_: Res<usize>) {}
361+
world.init_resource::<Counter>();
362+
fn wants_mut(_: ResMut<Counter>) {}
363+
fn wants_ref(_: Res<Counter>) {}
360364
let mut stage = SystemStage::parallel()
361365
.with_system(wants_mut)
362366
.with_system(wants_mut);

0 commit comments

Comments
 (0)