pub struct PrepareScratch<T: Default> { /* private fields */ }Expand description
Per-elevator scratch storage, keyed by EntityId.
Custom strategies use PrepareScratch<T> to hold per-car state that
is computed in prepare_car and read in rank. Drop entries from
notify_removed so an elevator leaving the group doesn’t leak.
§Example
use elevator_core::dispatch::{
DispatchStrategy, PrepareScratch, RankContext, ElevatorGroup, DispatchManifest,
};
use elevator_core::entity::EntityId;
use elevator_core::world::World;
#[derive(Default)]
struct CarStats { idle_for: f64 }
#[derive(Default)]
struct IdleAware {
stats: PrepareScratch<CarStats>,
}
impl DispatchStrategy for IdleAware {
fn prepare_car(
&mut self,
car: EntityId,
_car_position: f64,
_group: &ElevatorGroup,
_manifest: &DispatchManifest,
_world: &World,
) {
self.stats.entry(car).idle_for += 1.0;
}
fn rank(&self, ctx: &RankContext<'_>) -> Option<f64> {
let idle = self.stats.get(ctx.car).map_or(0.0, |s| s.idle_for);
Some((ctx.car_position() - ctx.stop_position()).abs() - 0.01 * idle)
}
fn notify_removed(&mut self, eid: EntityId) {
self.stats.remove(eid);
}
}Implementations§
Source§impl<T: Default> PrepareScratch<T>
impl<T: Default> PrepareScratch<T>
Sourcepub fn get_mut(&mut self, eid: EntityId) -> Option<&mut T>
pub fn get_mut(&mut self, eid: EntityId) -> Option<&mut T>
Mutable access to eid’s scratch slot, if it exists.
Sourcepub fn entry(&mut self, eid: EntityId) -> &mut T
pub fn entry(&mut self, eid: EntityId) -> &mut T
Mutable access to eid’s scratch slot, inserting T::default()
if absent. Mirrors HashMap::entry(...).or_default() but takes
the typed EntityId directly and returns &mut T so callers can
chain field updates (scratch.entry(car).field = …).
Intended for use inside prepare_car, where the framework
guarantees the elevator exists. Calling this from other
&mut self hooks (e.g. pre_dispatch) on an eid that has
not yet been through prepare_car will silently insert a
T::default() entry; prefer get or
get_mut there.
Sourcepub fn iter(&self) -> impl Iterator<Item = (EntityId, &T)> + '_
pub fn iter(&self) -> impl Iterator<Item = (EntityId, &T)> + '_
Iterate (EntityId, &T) for every populated slot.
Useful from pre_dispatch for fleet-wide scans (aging, decay,
tick-bound counters) without keeping a side-channel
Vec<EntityId> to track the populated set.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (EntityId, &mut T)> + '_
pub fn iter_mut(&mut self) -> impl Iterator<Item = (EntityId, &mut T)> + '_
Iterate (EntityId, &mut T) for every populated slot.
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut T> + '_
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut T> + '_
Iterate &mut T over every populated slot.
Sourcepub fn insert(&mut self, eid: EntityId, value: T) -> Option<T>
pub fn insert(&mut self, eid: EntityId, value: T) -> Option<T>
Replace the scratch value for eid, returning the previous value
if any.
Trait Implementations§
Source§impl<T: Clone + Default> Clone for PrepareScratch<T>
impl<T: Clone + Default> Clone for PrepareScratch<T>
Source§fn clone(&self) -> PrepareScratch<T>
fn clone(&self) -> PrepareScratch<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more