1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use bevy_utils::tracing::warn;
use crate::{
archetype::{Archetype, ArchetypeComponentId},
component::ComponentId,
query::Access,
world::World,
};
use std::borrow::Cow;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct SystemId(pub usize);
impl SystemId {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
SystemId(rand::random::<usize>())
}
}
pub trait System: Send + Sync + 'static {
type In;
type Out;
fn name(&self) -> Cow<'static, str>;
fn id(&self) -> SystemId;
fn new_archetype(&mut self, archetype: &Archetype);
fn component_access(&self) -> &Access<ComponentId>;
fn archetype_component_access(&self) -> &Access<ArchetypeComponentId>;
fn is_send(&self) -> bool;
unsafe fn run_unsafe(&mut self, input: Self::In, world: &World) -> Self::Out;
fn run(&mut self, input: Self::In, world: &mut World) -> Self::Out {
unsafe { self.run_unsafe(input, world) }
}
fn apply_buffers(&mut self, world: &mut World);
fn initialize(&mut self, _world: &mut World);
fn check_change_tick(&mut self, change_tick: u32);
}
pub type BoxedSystem<In = (), Out = ()> = Box<dyn System<In = In, Out = Out>>;
pub(crate) fn check_system_change_tick(
last_change_tick: &mut u32,
change_tick: u32,
system_name: &str,
) {
let tick_delta = change_tick.wrapping_sub(*last_change_tick);
const MAX_DELTA: u32 = (u32::MAX / 4) * 3;
if tick_delta > MAX_DELTA {
warn!(
"Too many intervening systems have run since the last time System '{}' was last run; it may fail to detect changes.",
system_name
);
*last_change_tick = change_tick.wrapping_sub(MAX_DELTA);
}
}