Skip to content

Commit

Permalink
Let init_non_send_resource require FromWorld instead of Default (#13779)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
NiklasEi authored and mockersf committed Jun 10, 2024
1 parent 621cd23 commit 91db570
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,12 @@ impl App {
self
}

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

#[cfg(test)]
mod tests {
use std::sync::Mutex;
use std::{marker::PhantomData, mem};

use bevy_ecs::prelude::{Resource, World};
use bevy_ecs::world::FromWorld;
use bevy_ecs::{event::EventWriter, schedule::ScheduleLabel, system::Commands};

use crate::{App, AppExit, Plugin, Update};
Expand Down Expand Up @@ -1178,4 +1184,31 @@ mod tests {
// it's nice they're so small let's keep it that way.
assert_eq!(mem::size_of::<AppExit>(), mem::size_of::<u8>());
}

#[test]
fn initializing_resources_from_world() {
#[derive(Resource)]
struct TestResource;
impl FromWorld for TestResource {
fn from_world(_world: &mut World) -> Self {
TestResource
}
}

#[derive(Resource)]
struct NonSendTestResource {
_marker: PhantomData<Mutex<()>>,
}
impl FromWorld for NonSendTestResource {
fn from_world(_world: &mut World) -> Self {
NonSendTestResource {
_marker: PhantomData,
}
}
}

App::new()
.init_non_send_resource::<NonSendTestResource>()
.init_resource::<TestResource>();
}
}

0 comments on commit 91db570

Please sign in to comment.