-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a rather simple but wide change, and it involves adding a new `bevy_app_macros` crate. Let me know if there is a better way to do any of this! --- # Objective - Allow adding and accessing sub-apps by using a label instead of an index ## Solution - Migrate the bevy label implementation and derive code to the `bevy_utils` and `bevy_macro_utils` crates and then add a new `SubAppLabel` trait to the `bevy_app` crate that is used when adding or getting a sub-app from an app.
- Loading branch information
Showing
21 changed files
with
244 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "bevy_app_macros" | ||
version = "0.5.0" | ||
description = "Bevy App Macros" | ||
edition = "2018" | ||
license = "MIT OR Apache-2.0" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
bevy_macro_utils = { path = "../../bevy_macro_utils", version = "0.5.0" } | ||
|
||
syn = "1.0" | ||
quote = "1.0" | ||
proc-macro2 = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
extern crate proc_macro; | ||
|
||
use bevy_macro_utils::{derive_label, BevyManifest}; | ||
use proc_macro::TokenStream; | ||
use quote::format_ident; | ||
|
||
#[proc_macro_derive(SubAppLabel)] | ||
pub fn derive_sub_app_label(input: TokenStream) -> TokenStream { | ||
let input = syn::parse_macro_input!(input as syn::DeriveInput); | ||
let mut trait_path = bevy_app_path(); | ||
trait_path | ||
.segments | ||
.push(format_ident!("SubAppLabel").into()); | ||
derive_label(input, trait_path) | ||
} | ||
|
||
fn bevy_app_path() -> syn::Path { | ||
BevyManifest::default().get_path("bevy_app") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,15 @@ | ||
pub use bevy_ecs_macros::{AmbiguitySetLabel, RunCriteriaLabel, StageLabel, SystemLabel}; | ||
|
||
use std::{ | ||
any::Any, | ||
borrow::Cow, | ||
fmt::Debug, | ||
hash::{Hash, Hasher}, | ||
}; | ||
use bevy_utils::define_label; | ||
|
||
pub trait DynEq: Any { | ||
fn as_any(&self) -> &dyn Any; | ||
|
||
fn dyn_eq(&self, other: &dyn DynEq) -> bool; | ||
} | ||
|
||
impl<T> DynEq for T | ||
where | ||
T: Any + Eq, | ||
{ | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn dyn_eq(&self, other: &dyn DynEq) -> bool { | ||
if let Some(other) = other.as_any().downcast_ref::<T>() { | ||
return self == other; | ||
} | ||
false | ||
} | ||
} | ||
|
||
pub trait DynHash: DynEq { | ||
fn as_dyn_eq(&self) -> &dyn DynEq; | ||
|
||
fn dyn_hash(&self, state: &mut dyn Hasher); | ||
} | ||
|
||
impl<T> DynHash for T | ||
where | ||
T: DynEq + Hash, | ||
{ | ||
fn as_dyn_eq(&self) -> &dyn DynEq { | ||
self | ||
} | ||
|
||
fn dyn_hash(&self, mut state: &mut dyn Hasher) { | ||
T::hash(self, &mut state); | ||
self.type_id().hash(&mut state); | ||
} | ||
} | ||
|
||
pub trait StageLabel: DynHash + Debug + Send + Sync + 'static { | ||
#[doc(hidden)] | ||
fn dyn_clone(&self) -> Box<dyn StageLabel>; | ||
} | ||
define_label!(StageLabel); | ||
pub(crate) type BoxedStageLabel = Box<dyn StageLabel>; | ||
|
||
pub trait SystemLabel: DynHash + Debug + Send + Sync + 'static { | ||
#[doc(hidden)] | ||
fn dyn_clone(&self) -> Box<dyn SystemLabel>; | ||
} | ||
define_label!(SystemLabel); | ||
pub(crate) type BoxedSystemLabel = Box<dyn SystemLabel>; | ||
|
||
pub trait AmbiguitySetLabel: DynHash + Debug + Send + Sync + 'static { | ||
#[doc(hidden)] | ||
fn dyn_clone(&self) -> Box<dyn AmbiguitySetLabel>; | ||
} | ||
define_label!(AmbiguitySetLabel); | ||
pub(crate) type BoxedAmbiguitySetLabel = Box<dyn AmbiguitySetLabel>; | ||
|
||
pub trait RunCriteriaLabel: DynHash + Debug + Send + Sync + 'static { | ||
#[doc(hidden)] | ||
fn dyn_clone(&self) -> Box<dyn RunCriteriaLabel>; | ||
} | ||
define_label!(RunCriteriaLabel); | ||
pub(crate) type BoxedRunCriteriaLabel = Box<dyn RunCriteriaLabel>; | ||
|
||
macro_rules! impl_label { | ||
($trait_name:ident) => { | ||
impl PartialEq for dyn $trait_name { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.dyn_eq(other.as_dyn_eq()) | ||
} | ||
} | ||
|
||
impl Eq for dyn $trait_name {} | ||
|
||
impl Hash for dyn $trait_name { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
self.dyn_hash(state); | ||
} | ||
} | ||
|
||
impl Clone for Box<dyn $trait_name> { | ||
fn clone(&self) -> Self { | ||
self.dyn_clone() | ||
} | ||
} | ||
|
||
impl $trait_name for Cow<'static, str> { | ||
fn dyn_clone(&self) -> Box<dyn $trait_name> { | ||
Box::new(self.clone()) | ||
} | ||
} | ||
|
||
impl $trait_name for &'static str { | ||
fn dyn_clone(&self) -> Box<dyn $trait_name> { | ||
Box::new(<&str>::clone(self)) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_label!(StageLabel); | ||
impl_label!(SystemLabel); | ||
impl_label!(AmbiguitySetLabel); | ||
impl_label!(RunCriteriaLabel); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ keywords = ["bevy"] | |
[dependencies] | ||
cargo-manifest = "0.2.3" | ||
syn = "1.0" | ||
quote = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.