Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin_group! macro #11460

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_a11y/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub enum AccessibilitySystem {
}

/// Plugin managing non-GUI aspects of integrating with accessibility APIs.
#[derive(Default)]
pub struct AccessibilityPlugin;

impl Plugin for AccessibilityPlugin {
Expand Down
127 changes: 127 additions & 0 deletions crates/bevy_app/src/plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,133 @@ use crate::{App, AppError, Plugin};
use bevy_utils::{tracing::debug, tracing::warn, TypeIdMap};
use std::any::TypeId;

/// A macro for generating a well-documented [`PluginGroup`] from a list of [`Plugin`] paths.
///
/// Every plugin must implement the [`Default`] trait.
///
/// # Example
///
/// ```
/// # use bevy_app::*;
/// # mod velocity {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct VelocityPlugin;
/// # impl Plugin for VelocityPlugin { fn build(&self, _: &mut App) {} }
/// # }
/// # mod collision { pub mod capsule {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct CapsuleCollisionPlugin;
/// # impl Plugin for CapsuleCollisionPlugin { fn build(&self, _: &mut App) {} }
/// # } }
/// # #[derive(Default)] pub struct TickratePlugin;
/// # impl Plugin for TickratePlugin { fn build(&self, _: &mut App) {} }
/// # mod features {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct ForcePlugin;
/// # impl Plugin for ForcePlugin { fn build(&self, _: &mut App) {} }
/// # }
/// # mod web {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct WebCompatibilityPlugin;
/// # impl Plugin for WebCompatibilityPlugin { fn build(&self, _: &mut App) {} }
/// # }
/// # mod internal {
/// # use bevy_app::*;
/// # #[derive(Default)] pub struct InternalPlugin;
/// # impl Plugin for InternalPlugin { fn build(&self, _: &mut App) {} }
/// # }
/// plugin_group! {
/// PhysicsPlugins {
/// // Due to local ambiguity issues, you have to
/// // use a colon before the plugin's name.
/// :TickratePlugin,
/// // Due to local ambiguity issues, you have to
/// // use 3 colons for the last part of the path.
/// collision::capsule:::CapsuleCollisionPlugin,
/// velocity:::VelocityPlugin,
/// // Add a documented feature restriction.
/// #[cfg(feature = "external_forces")]
/// features:::ForcePlugin,
/// // You can add any attribute you want like this, but it won't be documented.
/// #[custom(cfg(target_arch = "wasm32"))]
/// web:::WebCompatibilityPlugin,
/// // You can hide plugins from documentation.
/// // NOTE: Due to macro limitations, you can only use
/// // #[doc(hidden)] for plugins at the bottom of the macro.
/// #[doc(hidden)]
/// internal:::InternalPlugin
/// }
/// }
/// ```
#[macro_export]
macro_rules! plugin_group {
{
$(#[$group_meta:meta])*
$group:ident {
$(
$(#[cfg(feature = $plugin_feature:literal)])?
$(#[custom($plugin_meta:meta)])*
$($plugin_path:ident::)* : $plugin_name:ident
doonv marked this conversation as resolved.
Show resolved Hide resolved
),*
$(
$(,)?$(
#[doc(hidden)]
$(#[cfg(feature = $hidden_plugin_feature:literal)])?
$(#[custom($hidden_plugin_meta:meta)])*
$($hidden_plugin_path:ident::)* : $hidden_plugin_name:ident
),+
)?

$(,)?
}
$($(#[doc = $post_doc:literal])+)?
} => {
$(#[$group_meta])*
///
$(#[doc = concat!(
"* [`", stringify!($plugin_name), "`](" $(, stringify!($plugin_path), "::")*, stringify!($plugin_name), ")"
$(, " - with feature `", $plugin_feature, "`")?
)])*
$(
///
$(#[doc = $post_doc])+
)?
pub struct $group;
impl $crate::PluginGroup for $group {
fn build(self) -> $crate::PluginGroupBuilder {
let mut group = $crate::PluginGroupBuilder::start::<Self>();

$(
$(#[cfg(feature = $plugin_feature)])?
$(#[$plugin_meta])*
{
const _: () = {
const fn check_default<T: Default>() {}
check_default::<$($plugin_path::)*$plugin_name>();
};

group = group.add(<$($plugin_path::)*$plugin_name>::default());
}
)*
$($(
$(#[cfg(feature = $hidden_plugin_feature)])?
$(#[$hidden_plugin_meta])*
{
const _: () = {
const fn check_default<T: Default>() {}
check_default::<$($hidden_plugin_path::)*$hidden_plugin_name>();
};

group = group.add(<$($hidden_plugin_path::)*$hidden_plugin_name>::default());
}
)+)?

group
}
}
};
}

/// Combines multiple [`Plugin`]s into a single unit.
pub trait PluginGroup: Sized {
/// Configures the [`Plugin`]s that are to be added.
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_dev_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub mod ui_debug_overlay;
///
/// Note: The third method is not recommended, as it requires you to remove the feature before
/// creating a build for release to the public.
#[derive(Default)]
pub struct DevToolsPlugin;

impl Plugin for DevToolsPlugin {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const LINE_JOINT_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(11627807
/// A [`Plugin`] that provides an immediate mode drawing api for visual debugging.
///
/// Requires to be loaded after [`PbrPlugin`](bevy_pbr::PbrPlugin) or [`SpritePlugin`](bevy_sprite::SpritePlugin).
#[derive(Default)]
pub struct GizmoPlugin;

impl Plugin for GizmoPlugin {
Expand Down
Loading