Skip to content

Commit 69d7da9

Browse files
authored
Let init_non_send_resource require FromWorld instead of Default (#13779)
# Objective - Let `init_non_send_resource` take `FromWorld` values again, not only `Default` - This reverts an unintended breaking change introduced in #9202 ## Solution - The resource initialized with `init_non_send_resource` requires `FromWorld` again
1 parent 3478d15 commit 69d7da9

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

crates/bevy_app/src/app.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,12 @@ impl App {
424424
self
425425
}
426426

427-
/// Inserts the [`!Send`](Send) resource into the app, initialized with its default value,
428-
/// if there is no existing instance of `R`.
429-
pub fn init_non_send_resource<R: 'static + Default>(&mut self) -> &mut Self {
427+
/// Inserts the [`!Send`](Send) resource into the app if there is no existing instance of `R`.
428+
///
429+
/// `R` must implement [`FromWorld`].
430+
/// If `R` implements [`Default`], [`FromWorld`] will be automatically implemented and
431+
/// initialize the [`Resource`] with [`Default::default`].
432+
pub fn init_non_send_resource<R: 'static + FromWorld>(&mut self) -> &mut Self {
430433
self.world_mut().init_non_send_resource::<R>();
431434
self
432435
}
@@ -916,8 +919,11 @@ impl Termination for AppExit {
916919

917920
#[cfg(test)]
918921
mod tests {
922+
use std::sync::Mutex;
919923
use std::{marker::PhantomData, mem};
920924

925+
use bevy_ecs::prelude::{Resource, World};
926+
use bevy_ecs::world::FromWorld;
921927
use bevy_ecs::{event::EventWriter, schedule::ScheduleLabel, system::Commands};
922928

923929
use crate::{App, AppExit, Plugin, Update};
@@ -1178,4 +1184,31 @@ mod tests {
11781184
// it's nice they're so small let's keep it that way.
11791185
assert_eq!(mem::size_of::<AppExit>(), mem::size_of::<u8>());
11801186
}
1187+
1188+
#[test]
1189+
fn initializing_resources_from_world() {
1190+
#[derive(Resource)]
1191+
struct TestResource;
1192+
impl FromWorld for TestResource {
1193+
fn from_world(_world: &mut World) -> Self {
1194+
TestResource
1195+
}
1196+
}
1197+
1198+
#[derive(Resource)]
1199+
struct NonSendTestResource {
1200+
_marker: PhantomData<Mutex<()>>,
1201+
}
1202+
impl FromWorld for NonSendTestResource {
1203+
fn from_world(_world: &mut World) -> Self {
1204+
NonSendTestResource {
1205+
_marker: PhantomData,
1206+
}
1207+
}
1208+
}
1209+
1210+
App::new()
1211+
.init_non_send_resource::<NonSendTestResource>()
1212+
.init_resource::<TestResource>();
1213+
}
11811214
}

0 commit comments

Comments
 (0)