Skip to content

Commit

Permalink
Add macros to reduce boilerplates
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyAnkh committed Oct 10, 2024
1 parent bd0c746 commit e7b1781
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 99 deletions.
14 changes: 2 additions & 12 deletions crates/bevy_animation/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use bevy_ecs::{
system::{Res, ResMut, Resource},
};
use bevy_reflect::{prelude::ReflectDefault, Reflect, ReflectSerialize};
use bevy_utils::HashMap;
use bevy_utils::{impl_handle_wrapper, HashMap};
use derive_more::derive::{Display, Error, From};
use petgraph::{
graph::{DiGraph, NodeIndex},
Expand Down Expand Up @@ -130,17 +130,7 @@ pub struct AnimationGraph {
#[reflect(Component, Default)]
pub struct AnimationGraphHandle(pub Handle<AnimationGraph>);

impl From<AnimationGraphHandle> for AssetId<AnimationGraph> {
fn from(handle: AnimationGraphHandle) -> Self {
handle.id()
}
}

impl From<&AnimationGraphHandle> for AssetId<AnimationGraph> {
fn from(handle: &AnimationGraphHandle) -> Self {
handle.id()
}
}
impl_handle_wrapper!(AnimationGraphHandle, AnimationGraph);

/// A type alias for the `petgraph` data structure that defines the animation
/// graph.
Expand Down
19 changes: 2 additions & 17 deletions crates/bevy_pbr/src/mesh_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy_asset::{AssetId, Handle};
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_utils::impl_generic_handle_wrapper;
use derive_more::derive::From;

/// A [material](Material) for a [`Mesh3d`].
Expand Down Expand Up @@ -73,23 +74,7 @@ use derive_more::derive::From;
#[require(HasMaterial3d)]
pub struct MeshMaterial3d<M: Material>(pub Handle<M>);

impl<M: Material> Default for MeshMaterial3d<M> {
fn default() -> Self {
Self(Handle::default())
}
}

impl<M: Material> From<MeshMaterial3d<M>> for AssetId<M> {
fn from(material: MeshMaterial3d<M>) -> Self {
material.id()
}
}

impl<M: Material> From<&MeshMaterial3d<M>> for AssetId<M> {
fn from(material: &MeshMaterial3d<M>) -> Self {
material.id()
}
}
impl_generic_handle_wrapper!(MeshMaterial3d, Handle, M, Material);

/// A component that marks an entity as having a [`MeshMaterial3d`].
/// [`Mesh3d`] entities without this component are rendered with a [default material].
Expand Down
14 changes: 2 additions & 12 deletions crates/bevy_pbr/src/meshlet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ use bevy_render::{
ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_transform::components::{GlobalTransform, Transform};
use bevy_utils::tracing::error;
use bevy_utils::{impl_handle_wrapper, tracing::error};
use derive_more::From;

const MESHLET_BINDINGS_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(1325134235233421);
Expand Down Expand Up @@ -295,17 +295,7 @@ impl Plugin for MeshletPlugin {
#[require(Transform, Visibility)]
pub struct MeshletMesh3d(pub Handle<MeshletMesh>);

impl From<MeshletMesh3d> for AssetId<MeshletMesh> {
fn from(mesh: MeshletMesh3d) -> Self {
mesh.id()
}
}

impl From<&MeshletMesh3d> for AssetId<MeshletMesh> {
fn from(mesh: &MeshletMesh3d) -> Self {
mesh.id()
}
}
impl_handle_wrapper!(MeshletMesh3d, MeshletMesh);

/// A component bundle for entities with a [`MeshletMesh`] and a [`Material`].
#[derive(Bundle, Clone)]
Expand Down
25 changes: 3 additions & 22 deletions crates/bevy_render/src/mesh/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_transform::components::Transform;
use bevy_utils::impl_handle_wrapper;
use derive_more::derive::From;

/// A component for rendering 2D meshes, typically with a [`MeshMaterial2d`] using a [`ColorMaterial`].
Expand Down Expand Up @@ -41,17 +42,7 @@ use derive_more::derive::From;
#[require(Transform, Visibility)]
pub struct Mesh2d(pub Handle<Mesh>);

impl From<Mesh2d> for AssetId<Mesh> {
fn from(mesh: Mesh2d) -> Self {
mesh.id()
}
}

impl From<&Mesh2d> for AssetId<Mesh> {
fn from(mesh: &Mesh2d) -> Self {
mesh.id()
}
}
impl_handle_wrapper!(Mesh2d, Mesh);

/// A component for rendering 3D meshes, typically with a [`MeshMaterial3d`] using a [`StandardMaterial`].
///
Expand Down Expand Up @@ -91,14 +82,4 @@ impl From<&Mesh2d> for AssetId<Mesh> {
#[require(Transform, Visibility)]
pub struct Mesh3d(pub Handle<Mesh>);

impl From<Mesh3d> for AssetId<Mesh> {
fn from(mesh: Mesh3d) -> Self {
mesh.id()
}
}

impl From<&Mesh3d> for AssetId<Mesh> {
fn from(mesh: &Mesh3d) -> Self {
mesh.id()
}
}
impl_handle_wrapper!(Mesh3d, Mesh);
20 changes: 2 additions & 18 deletions crates/bevy_sprite/src/mesh2d/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use bevy_render::{
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_transform::components::{GlobalTransform, Transform};
use bevy_utils::tracing::error;
use bevy_utils::{impl_generic_handle_wrapper, tracing::error};
use core::{hash::Hash, marker::PhantomData};
use derive_more::derive::From;

Expand Down Expand Up @@ -215,23 +215,7 @@ pub trait Material2d: AsBindGroup + Asset + Clone + Sized {
#[require(HasMaterial2d)]
pub struct MeshMaterial2d<M: Material2d>(pub Handle<M>);

impl<M: Material2d> Default for MeshMaterial2d<M> {
fn default() -> Self {
Self(Handle::default())
}
}

impl<M: Material2d> From<MeshMaterial2d<M>> for AssetId<M> {
fn from(material: MeshMaterial2d<M>) -> Self {
material.id()
}
}

impl<M: Material2d> From<&MeshMaterial2d<M>> for AssetId<M> {
fn from(material: &MeshMaterial2d<M>) -> Self {
material.id()
}
}
impl_generic_handle_wrapper!(MeshMaterial2d, Handle, M, Material2d);

/// A component that marks an entity as having a [`MeshMaterial2d`].
/// [`Mesh2d`] entities without this component are rendered with a [default material].
Expand Down
19 changes: 2 additions & 17 deletions crates/bevy_ui/src/ui_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bevy_render::{
extract_component::ExtractComponent,
render_resource::{AsBindGroup, RenderPipelineDescriptor, ShaderRef},
};
use bevy_utils::impl_generic_handle_wrapper;
use derive_more::derive::From;

/// Materials are used alongside [`UiMaterialPlugin`](crate::UiMaterialPlugin) and [`MaterialNodeBundle`](crate::prelude::MaterialNodeBundle)
Expand Down Expand Up @@ -159,20 +160,4 @@ where
#[reflect(Component, Default)]
pub struct UiMaterialHandle<M: UiMaterial>(pub Handle<M>);

impl<M: UiMaterial> Default for UiMaterialHandle<M> {
fn default() -> Self {
Self(Handle::default())
}
}

impl<M: UiMaterial> From<UiMaterialHandle<M>> for AssetId<M> {
fn from(material: UiMaterialHandle<M>) -> Self {
material.id()
}
}

impl<M: UiMaterial> From<&UiMaterialHandle<M>> for AssetId<M> {
fn from(material: &UiMaterialHandle<M>) -> Self {
material.id()
}
}
impl_generic_handle_wrapper!(UiMaterialHandle, Handle, M, UiMaterial);
48 changes: 48 additions & 0 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,54 @@ pub fn error<E: Debug>(result: Result<(), E>) {
}
}

/// Boilerplate for handle wrapper
#[macro_export]
macro_rules! impl_handle_wrapper {
($handle_wrapper_type:ident, $asset_type:ty) => {
impl From<$handle_wrapper_type> for AssetId<$asset_type> {
fn from(handle: $handle_wrapper_type) -> Self {
handle.id()
}
}

impl From<&$handle_wrapper_type> for AssetId<$asset_type> {
fn from(handle: &$handle_wrapper_type) -> Self {
handle.id()
}
}
};
}

/// Boilerplate for generic handle wrapper
#[macro_export]
macro_rules! impl_generic_handle_wrapper {
($handle_type:ident, $asset_type:ty, $asset_type_parameter:ident, $parameter_trait:ident) => {
impl<$asset_type_parameter: $parameter_trait> Default
for $handle_type<$asset_type_parameter>
{
fn default() -> Self {
Self(Handle::default())
}
}

impl<$asset_type_parameter: $parameter_trait> From<$handle_type<$asset_type_parameter>>
for AssetId<$asset_type_parameter>
{
fn from(handle: $handle_type<$asset_type_parameter>) -> Self {
handle.id()
}
}

impl<$asset_type_parameter: $parameter_trait> From<&$handle_type<$asset_type_parameter>>
for AssetId<$asset_type_parameter>
{
fn from(handle: &$handle_type<$asset_type_parameter>) -> Self {
handle.id()
}
}
};
}

/// Like [`tracing::trace`], but conditional on cargo feature `detailed_trace`.
#[macro_export]
macro_rules! detailed_trace {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_utils/src/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[macro_export]
macro_rules! once {
($expression:expr) => {{
use ::core::sync::atomic::{AtomicBool, Ordering};
use core::sync::atomic::{AtomicBool, Ordering};

static SHOULD_FIRE: AtomicBool = AtomicBool::new(true);
if SHOULD_FIRE.swap(false, Ordering::Relaxed) {
Expand Down

0 comments on commit e7b1781

Please sign in to comment.