use std::any::TypeId;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::sync::atomic::{AtomicUsize, Ordering};
pub use bevy_ecs_macros::{ScheduleLabel, SystemSet};
use bevy_utils::define_boxed_label;
use bevy_utils::label::DynHash;
use crate::system::{
ExclusiveSystemParamFunction, IsExclusiveFunctionSystem, IsFunctionSystem, SystemParamFunction,
};
define_boxed_label!(ScheduleLabel);
pub type BoxedSystemSet = Box<dyn SystemSet>;
pub type BoxedScheduleLabel = Box<dyn ScheduleLabel>;
pub trait SystemSet: DynHash + Debug + Send + Sync + 'static {
fn system_type(&self) -> Option<TypeId> {
None
}
fn is_anonymous(&self) -> bool {
false
}
fn dyn_clone(&self) -> Box<dyn SystemSet>;
}
impl PartialEq for dyn SystemSet {
fn eq(&self, other: &Self) -> bool {
self.dyn_eq(other.as_dyn_eq())
}
}
impl Eq for dyn SystemSet {}
impl Hash for dyn SystemSet {
fn hash<H: Hasher>(&self, state: &mut H) {
self.dyn_hash(state);
}
}
impl Clone for Box<dyn SystemSet> {
fn clone(&self) -> Self {
self.dyn_clone()
}
}
pub struct SystemTypeSet<T: 'static>(PhantomData<fn() -> T>);
impl<T: 'static> SystemTypeSet<T> {
pub(crate) fn new() -> Self {
Self(PhantomData)
}
}
impl<T> Debug for SystemTypeSet<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("SystemTypeSet")
.field(&std::any::type_name::<T>())
.finish()
}
}
impl<T> Hash for SystemTypeSet<T> {
fn hash<H: Hasher>(&self, _state: &mut H) {
}
}
impl<T> Clone for SystemTypeSet<T> {
fn clone(&self) -> Self {
Self(PhantomData)
}
}
impl<T> Copy for SystemTypeSet<T> {}
impl<T> PartialEq for SystemTypeSet<T> {
#[inline]
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl<T> Eq for SystemTypeSet<T> {}
impl<T> SystemSet for SystemTypeSet<T> {
fn system_type(&self) -> Option<TypeId> {
Some(TypeId::of::<T>())
}
fn dyn_clone(&self) -> Box<dyn SystemSet> {
Box::new(*self)
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct AnonymousSet(usize);
static NEXT_ANONYMOUS_SET_ID: AtomicUsize = AtomicUsize::new(0);
impl AnonymousSet {
pub(crate) fn new() -> Self {
Self(NEXT_ANONYMOUS_SET_ID.fetch_add(1, Ordering::Relaxed))
}
}
impl SystemSet for AnonymousSet {
fn is_anonymous(&self) -> bool {
true
}
fn dyn_clone(&self) -> Box<dyn SystemSet> {
Box::new(*self)
}
}
pub trait IntoSystemSet<Marker>: Sized {
type Set: SystemSet;
fn into_system_set(self) -> Self::Set;
}
impl<S: SystemSet> IntoSystemSet<()> for S {
type Set = Self;
#[inline]
fn into_system_set(self) -> Self::Set {
self
}
}
impl<Marker, F> IntoSystemSet<(IsFunctionSystem, Marker)> for F
where
F: SystemParamFunction<Marker>,
{
type Set = SystemTypeSet<Self>;
#[inline]
fn into_system_set(self) -> Self::Set {
SystemTypeSet::new()
}
}
impl<Marker, F> IntoSystemSet<(IsExclusiveFunctionSystem, Marker)> for F
where
F: ExclusiveSystemParamFunction<Marker>,
{
type Set = SystemTypeSet<Self>;
#[inline]
fn into_system_set(self) -> Self::Set {
SystemTypeSet::new()
}
}
#[cfg(test)]
mod tests {
use crate::{
schedule::{tests::ResMut, Schedule},
system::Resource,
};
use super::*;
#[test]
fn test_boxed_label() {
use crate::{self as bevy_ecs, world::World};
#[derive(Resource)]
struct Flag(bool);
#[derive(ScheduleLabel, Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
struct A;
let mut world = World::new();
let mut schedule = Schedule::new();
schedule.add_systems(|mut flag: ResMut<Flag>| flag.0 = true);
world.add_schedule(schedule, A);
let boxed: Box<dyn ScheduleLabel> = Box::new(A);
world.insert_resource(Flag(false));
world.run_schedule(&boxed);
assert!(world.resource::<Flag>().0);
world.insert_resource(Flag(false));
world.run_schedule(boxed);
assert!(world.resource::<Flag>().0);
}
}