
# Objective - Fixes #6370 - Closes #6581 ## Solution - Added the following lints to the workspace: - `std_instead_of_core` - `std_instead_of_alloc` - `alloc_instead_of_core` - Used `cargo +nightly fmt` with [item level use formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Item%5C%3A) to split all `use` statements into single items. - Used `cargo clippy --workspace --all-targets --all-features --fix --allow-dirty` to _attempt_ to resolve the new linting issues, and intervened where the lint was unable to resolve the issue automatically (usually due to needing an `extern crate alloc;` statement in a crate root). - Manually removed certain uses of `std` where negative feature gating prevented `--all-features` from finding the offending uses. - Used `cargo +nightly fmt` with [crate level use formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Crate%5C%3A) to re-merge all `use` statements matching Bevy's previous styling. - Manually fixed cases where the `fmt` tool could not re-merge `use` statements due to conditional compilation attributes. ## Testing - Ran CI locally ## Migration Guide The MSRV is now 1.81. Please update to this version or higher. ## Notes - This is a _massive_ change to try and push through, which is why I've outlined the semi-automatic steps I used to create this PR, in case this fails and someone else tries again in the future. - Making this change has no impact on user code, but does mean Bevy contributors will be warned to use `core` and `alloc` instead of `std` where possible. - This lint is a critical first step towards investigating `no_std` options for Bevy. --------- Co-authored-by: François Mockers <francois.mockers@vleue.com>
163 lines
4.9 KiB
Rust
163 lines
4.9 KiB
Rust
#![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"
|
||
)]
|
||
|
||
//! This crate provides core functionality for Bevy Engine.
|
||
|
||
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::*;
|
||
|
||
/// The core prelude.
|
||
///
|
||
/// This includes the most common types in this crate, re-exported for your convenience.
|
||
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;
|
||
|
||
/// Registration of default types to the [`TypeRegistry`](bevy_reflect::TypeRegistry) resource.
|
||
#[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>();
|
||
}
|
||
}
|
||
|
||
/// Setup of default task pools: [`AsyncComputeTaskPool`](bevy_tasks::AsyncComputeTaskPool),
|
||
/// [`ComputeTaskPool`](bevy_tasks::ComputeTaskPool), [`IoTaskPool`](bevy_tasks::IoTaskPool).
|
||
#[derive(Default)]
|
||
pub struct TaskPoolPlugin {
|
||
/// Options for the [`TaskPool`](bevy_tasks::TaskPool) created at application start.
|
||
pub task_pool_options: TaskPoolOptions,
|
||
}
|
||
|
||
impl Plugin for TaskPoolPlugin {
|
||
fn build(&self, _app: &mut App) {
|
||
// Setup the default bevy task pools
|
||
self.task_pool_options.create_default_pools();
|
||
|
||
#[cfg(not(target_arch = "wasm32"))]
|
||
_app.add_systems(Last, tick_global_task_pools);
|
||
}
|
||
}
|
||
/// A dummy type that is [`!Send`](Send), to force systems to run on the main thread.
|
||
pub struct NonSendMarker(PhantomData<*mut ()>);
|
||
|
||
/// A system used to check and advanced our task pools.
|
||
///
|
||
/// Calls [`tick_global_task_pools_on_main_thread`],
|
||
/// and uses [`NonSendMarker`] to ensure that this system runs on the main thread
|
||
#[cfg(not(target_arch = "wasm32"))]
|
||
fn tick_global_task_pools(_main_thread_marker: Option<NonSend<NonSendMarker>>) {
|
||
tick_global_task_pools_on_main_thread();
|
||
}
|
||
|
||
/// Maintains a count of frames rendered since the start of the application.
|
||
///
|
||
/// [`FrameCount`] is incremented during [`Last`], providing predictable
|
||
/// behavior: it will be 0 during the first update, 1 during the next, and so forth.
|
||
///
|
||
/// # Overflows
|
||
///
|
||
/// [`FrameCount`] will wrap to 0 after exceeding [`u32::MAX`]. Within reasonable
|
||
/// assumptions, one may exploit wrapping arithmetic to determine the number of frames
|
||
/// that have elapsed between two observations – see [`u32::wrapping_sub()`].
|
||
#[derive(Debug, Default, Resource, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||
pub struct FrameCount(pub u32);
|
||
|
||
/// Adds frame counting functionality to Apps.
|
||
#[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);
|
||
}
|
||
}
|
||
|
||
/// A system used to increment [`FrameCount`] with wrapping addition.
|
||
///
|
||
/// See [`FrameCount`] for more details.
|
||
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);
|
||
}
|
||
}
|