use std::time::Duration;
#[derive(Clone, Copy, Debug)]
pub struct StepSchedule {
pub hz: f64,
pub missed_tick: MissedTick,
}
impl StepSchedule {
pub const fn hz(hz: f64) -> Self {
StepSchedule {
hz,
missed_tick: MissedTick::Collapse,
}
}
pub fn period(&self) -> Duration {
std::cmp::max(
Duration::from_secs_f64(1.0 / self.hz),
Duration::from_nanos(1),
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MissedTick {
Collapse,
CatchUp,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::__private::surface::{ComponentBoundSurface, TypedIoSurface, WorldAuthoritySurface};
use crate::prelude::*;
#[test]
fn step_schedule_period_never_rounds_to_zero() {
assert_eq!(StepSchedule::hz(f64::MAX).period(), Duration::from_nanos(1));
}
#[phoxal::simulator(id = "marker-simulator")]
struct MarkerSimulator;
impl Participant for MarkerSimulator {
async fn setup(
&self,
_ctx: &mut SetupContext<Self>,
_config: Self::Config,
) -> Result<(Self::State, Self::Api)> {
Ok(((), ()))
}
}
#[phoxal::brain]
struct MarkerBrain;
impl Participant for MarkerBrain {
async fn setup(
&self,
_ctx: &mut SetupContext<Self>,
_config: Self::Config,
) -> Result<(Self::State, Self::Api)> {
Ok(((), ()))
}
}
#[test]
fn kind_macros_emit_their_markers() {
fn assert_simulator<T: WorldAuthoritySurface + ComponentBoundSurface + TypedIoSurface>() {}
assert_simulator::<MarkerSimulator>();
}
#[test]
fn the_brain_marker_is_checked_and_schedulable_only() {
fn assert_checked_schedulable<T: TypedIoSurface + crate::__private::SchedulableSurface>() {}
assert_checked_schedulable::<MarkerBrain>();
assert_eq!(
<MarkerBrain as crate::__private::ParticipantSpec>::ID,
"brain"
);
assert_eq!(
<MarkerBrain as crate::__private::ParticipantSpec>::KIND,
crate::participant::metadata::ParticipantKind::Brain
);
}
}