Skip to main content

PrepareScratch

Struct PrepareScratch 

Source
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>

Source

pub fn new() -> Self

Empty scratch.

Source

pub fn get(&self, eid: EntityId) -> Option<&T>

Read-only access to eid’s scratch slot.

Source

pub fn get_mut(&mut self, eid: EntityId) -> Option<&mut T>

Mutable access to eid’s scratch slot, if it exists.

Source

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.

Source

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.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (EntityId, &mut T)> + '_

Iterate (EntityId, &mut T) for every populated slot.

Source

pub fn values(&self) -> impl Iterator<Item = &T> + '_

Iterate &T over every populated slot.

Source

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut T> + '_

Iterate &mut T over every populated slot.

Source

pub fn insert(&mut self, eid: EntityId, value: T) -> Option<T>

Replace the scratch value for eid, returning the previous value if any.

Source

pub fn remove(&mut self, eid: EntityId) -> Option<T>

Drop the scratch entry for eid. Call from notify_removed so the scratch doesn’t outlive the elevator.

Source

pub fn clear(&mut self)

Drop every scratch entry.

Source

pub fn len(&self) -> usize

Number of scratch entries.

Source

pub fn is_empty(&self) -> bool

Whether the scratch is empty.

Trait Implementations§

Source§

impl<T: Clone + Default> Clone for PrepareScratch<T>

Source§

fn clone(&self) -> PrepareScratch<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Default> Debug for PrepareScratch<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default + Default> Default for PrepareScratch<T>

Source§

fn default() -> PrepareScratch<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for PrepareScratch<T>

§

impl<T> RefUnwindSafe for PrepareScratch<T>
where T: RefUnwindSafe,

§

impl<T> Send for PrepareScratch<T>
where T: Send,

§

impl<T> Sync for PrepareScratch<T>
where T: Sync,

§

impl<T> Unpin for PrepareScratch<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for PrepareScratch<T>

§

impl<T> UnwindSafe for PrepareScratch<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.