#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![forbid(unsafe_code)]
#![doc(
html_logo_url = "https://bevyengine.org/assets/icon.png",
html_favicon_url = "https://bevyengine.org/assets/icon.png"
)]
extern crate alloc;
mod name;
#[cfg(feature = "serialize")]
mod serde;
mod task_pool_options;
use bevy_ecs::system::Resource;
pub use name::*;
pub use task_pool_options::*;
pub mod prelude {
#[doc(hidden)]
pub use crate::{
FrameCountPlugin, Name, NameOrEntity, TaskPoolOptions, TaskPoolPlugin,
TypeRegistrationPlugin,
};
}
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use core::marker::PhantomData;
#[cfg(not(target_arch = "wasm32"))]
use bevy_tasks::tick_global_task_pools_on_main_thread;
#[derive(Default)]
pub struct TypeRegistrationPlugin;
impl Plugin for TypeRegistrationPlugin {
#[cfg_attr(not(feature = "bevy_reflect"), allow(unused_variables))]
fn build(&self, app: &mut App) {
#[cfg(feature = "bevy_reflect")]
app.register_type::<Name>();
}
}
#[derive(Default)]
pub struct TaskPoolPlugin {
pub task_pool_options: TaskPoolOptions,
}
impl Plugin for TaskPoolPlugin {
fn build(&self, _app: &mut App) {
self.task_pool_options.create_default_pools();
#[cfg(not(target_arch = "wasm32"))]
_app.add_systems(Last, tick_global_task_pools);
}
}
pub struct NonSendMarker(PhantomData<*mut ()>);
#[cfg(not(target_arch = "wasm32"))]
fn tick_global_task_pools(_main_thread_marker: Option<NonSend<NonSendMarker>>) {
tick_global_task_pools_on_main_thread();
}
#[derive(Debug, Default, Resource, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FrameCount(pub u32);
#[derive(Default)]
pub struct FrameCountPlugin;
impl Plugin for FrameCountPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<FrameCount>();
app.add_systems(Last, update_frame_count);
}
}
pub fn update_frame_count(mut frame_count: ResMut<FrameCount>) {
frame_count.0 = frame_count.0.wrapping_add(1);
}
#[cfg(test)]
mod tests {
use super::*;
use bevy_tasks::prelude::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool};
#[test]
fn runs_spawn_local_tasks() {
let mut app = App::new();
app.add_plugins((TaskPoolPlugin::default(), TypeRegistrationPlugin));
let (async_tx, async_rx) = crossbeam_channel::unbounded();
AsyncComputeTaskPool::get()
.spawn_local(async move {
async_tx.send(()).unwrap();
})
.detach();
let (compute_tx, compute_rx) = crossbeam_channel::unbounded();
ComputeTaskPool::get()
.spawn_local(async move {
compute_tx.send(()).unwrap();
})
.detach();
let (io_tx, io_rx) = crossbeam_channel::unbounded();
IoTaskPool::get()
.spawn_local(async move {
io_tx.send(()).unwrap();
})
.detach();
app.run();
async_rx.try_recv().unwrap();
compute_rx.try_recv().unwrap();
io_rx.try_recv().unwrap();
}
#[test]
fn frame_counter_update() {
let mut app = App::new();
app.add_plugins((
TaskPoolPlugin::default(),
TypeRegistrationPlugin,
FrameCountPlugin,
));
app.update();
let frame_count = app.world().resource::<FrameCount>();
assert_eq!(1, frame_count.0);
}
}