bevy/crates/bevy_render/src/extract_resource.rs
targrub c18b1a839b Prepare for upcoming rustlang by fixing upcoming clippy warnings (#6376)
# Objective

- Proactive changing of code to comply with warnings generated by beta of rustlang version of cargo clippy.

## Solution

- Code changed as recommended by `rustup update`, `rustup default beta`, `cargo run -p ci -- clippy`.
- Tested using `beta` and `stable`.  No clippy warnings in either after changes made.

---

## Changelog

- Warnings fixed were: `clippy::explicit-auto-deref` (present in 11 files), `clippy::needless-borrow` (present in 2 files), and `clippy::only-used-in-recursion` (only 1 file).
2022-10-26 19:15:15 +00:00

65 lines
2.2 KiB
Rust

use std::marker::PhantomData;
use bevy_app::{App, Plugin};
#[cfg(debug_assertions)]
use bevy_ecs::system::Local;
use bevy_ecs::system::{Commands, Res, ResMut, Resource};
pub use bevy_render_macros::ExtractResource;
use crate::{Extract, RenderApp, RenderStage};
/// Describes how a resource gets extracted for rendering.
///
/// Therefore the resource is transferred from the "main world" into the "render world"
/// in the [`RenderStage::Extract`](crate::RenderStage::Extract) step.
pub trait ExtractResource: Resource {
type Source: Resource;
/// Defines how the resource is transferred into the "render world".
fn extract_resource(source: &Self::Source) -> Self;
}
/// This plugin extracts the resources into the "render world".
///
/// Therefore it sets up the [`RenderStage::Extract`](crate::RenderStage::Extract) step
/// for the specified [`Resource`].
pub struct ExtractResourcePlugin<R: ExtractResource>(PhantomData<R>);
impl<R: ExtractResource> Default for ExtractResourcePlugin<R> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<R: ExtractResource> Plugin for ExtractResourcePlugin<R> {
fn build(&self, app: &mut App) {
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app.add_system_to_stage(RenderStage::Extract, extract_resource::<R>);
}
}
}
/// This system extracts the resource of the corresponding [`Resource`] type
pub fn extract_resource<R: ExtractResource>(
mut commands: Commands,
main_resource: Extract<Res<R::Source>>,
target_resource: Option<ResMut<R>>,
#[cfg(debug_assertions)] mut has_warned_on_remove: Local<bool>,
) {
if let Some(mut target_resource) = target_resource {
if main_resource.is_changed() {
*target_resource = R::extract_resource(&main_resource);
}
} else {
#[cfg(debug_assertions)]
if !main_resource.is_added() && !*has_warned_on_remove {
*has_warned_on_remove = true;
bevy_log::warn!(
"Removing resource {} from render world not expected, adding using `Commands`.
This may decrease performance",
std::any::type_name::<R>()
);
}
commands.insert_resource(R::extract_resource(&main_resource));
}
}