# schedule.rs - source
Source of the Rust file `maplibre/src/schedule.rs`.

    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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315

    use std::collections::HashMap;

    use downcast_rs::{impl_downcast, Downcast};

    use crate::{
        context::MapContext,
        define_label,
        tcs::system::{stage::SystemStage, IntoSystemContainer},
    };

    pub struct NopStage;

    impl Stage for NopStage {
        fn run(&mut self, _context: &mut MapContext) {}
    }

    #[macro_export]
    macro_rules! multi_stage {
        ($multi_stage:ident, $($stage:ident: $stage_ty:ty),*) => {
            pub struct $multi_stage {
                $($stage: $stage_ty),*
            }

            impl Stage for $multi_stage {
                fn run(&mut self, context: &mut $crate::context::MapContext) {
                     $(self.$stage.run(context);)*
                }
            }

            impl Default for $multi_stage {
                fn default() -> Self {
                    $multi_stage {
                         $($stage: <$stage_ty>::default()),*
                    }
                }
            }
        };
    }

    pub struct MultiStage<const I: usize, S>
    where
        S: Stage,
    {
        stages: [S; I],
    }

    impl<const I: usize, S> MultiStage<I, S>
    where
        S: Stage,
    {
        pub fn new(stages: [S; I]) -> Self {
            Self { stages }
        }
    }

    impl<const I: usize, S> Stage for MultiStage<I, S>
    where
        S: Stage,
    {
        fn run(&mut self, context: &mut MapContext) {
            for stage in self.stages.iter_mut() {
                stage.run(context)
            }
        }
    }

    define_label!(StageLabel);
    pub(crate) type BoxedStageLabel = Box<dyn StageLabel>;

    pub trait Stage: Downcast {
        /// Runs the stage; this happens once per update.
        /// Implementors must initialize all of their state before running the first time.
        fn run(&mut self, context: &mut MapContext);
    }

    impl_downcast!(Stage);

    /// A container of [`Stage`]s set to be run in a linear order.
    ///
    /// Since `Schedule` implements the [`Stage`] trait, it can be inserted into another schedule.
    /// In this way, the properties of the child schedule can be set differently from the parent.
    /// For example, it can be set to run only once during app execution, while the parent schedule
    /// runs indefinitely.
    #[derive(Default)]
    pub struct Schedule {
        stages: HashMap<BoxedStageLabel, Box<dyn Stage>>,
        stage_order: Vec<BoxedStageLabel>,
    }

    impl Schedule {
        /// Adds the given `stage` at the last position of the schedule.
        ///
        /// # Example
        ///
        /// ```
        /// # use maplibre::schedule::{Schedule, NopStage};
        /// #
        /// # let mut schedule = Schedule::default();
        /// schedule.add_stage("my_stage", NopStage);
        /// ```
        pub fn add_stage<S: Stage>(&mut self, label: impl StageLabel, stage: S) -> &mut Self {
            let label: Box<dyn StageLabel> = Box::new(label);
            self.stage_order.push(label.clone());
            let prev = self.stages.insert(label.clone(), Box::new(stage));
            assert!(prev.is_none(), "Stage already exists: {label:?}.");
            self
        }

        pub fn remove_stage(&mut self, label: impl StageLabel) -> &mut Self {
            let remove: Box<dyn StageLabel> = Box::new(label);
            self.stages.remove(&remove).expect("stage not found");
            self.stage_order.retain(|label| label != &remove);
            self
        }

        /// Adds the given `stage` immediately after the `target` stage.
        ///
        /// # Example
        ///
        /// ```
        /// # use maplibre::schedule::{Schedule, NopStage};
        /// #
        /// # let mut schedule = Schedule::default();
        /// # schedule.add_stage("target_stage", NopStage);
        /// schedule.add_stage_after("target_stage", "my_stage", NopStage);
        /// ```
        pub fn add_stage_after<S: Stage>(
            &mut self,
            target: impl StageLabel,
            label: impl StageLabel,
            stage: S,
        ) -> &mut Self {
            let label: Box<dyn StageLabel> = Box::new(label);
            let target = &target as &dyn StageLabel;
            let target_index = self
                .stage_order
                .iter()
                .enumerate()
                .find(|(_i, stage_label)| &***stage_label == target)
                .map(|(i, _)| i)
                .unwrap_or_else(|| panic!("Target stage does not exist: {target:?}."));

            self.stage_order.insert(target_index + 1, label.clone());
            let prev = self.stages.insert(label.clone(), Box::new(stage));
            assert!(prev.is_none(), "Stage already exists: {label:?}.");
            self
        }

        /// Adds the given `stage` immediately before the `target` stage.
        ///
        /// # Example
        ///
        /// ```
        /// # use maplibre::schedule::{Schedule, NopStage};
        /// #
        /// # let mut schedule = Schedule::default();
        /// # schedule.add_stage("target_stage", NopStage);
        /// #
        /// schedule.add_stage_before("target_stage", "my_stage", NopStage);
        /// ```
        pub fn add_stage_before<S: Stage>(
            &mut self,
            target: impl StageLabel,
            label: impl StageLabel,
            stage: S,
        ) -> &mut Self {
            let label: Box<dyn StageLabel> = Box::new(label);
            let target = &target as &dyn StageLabel;
            let target_index = self
                .stage_order
                .iter()
                .enumerate()
                .find(|(_i, stage_label)| &***stage_label == target)
                .map(|(i, _)| i)
                .unwrap_or_else(|| panic!("Target stage does not exist: {target:?}."));

            self.stage_order.insert(target_index, label.clone());
            let prev = self.stages.insert(label.clone(), Box::new(stage));
            assert!(prev.is_none(), "Stage already exists: {label:?}.");
            self
        }

        /// Fetches the [`Stage`] of type `T` marked with `label`, then executes the provided
        /// `func` passing the fetched stage to it as an argument.
        ///
        /// The `func` argument should be a function or a closure that accepts a mutable reference
        /// to a struct implementing `Stage` and returns the same type. That means that it should
        /// also assume that the stage has already been fetched successfully.
        ///
        /// # Example
        ///
        /// ```
        /// # use maplibre::schedule::{Schedule, NopStage};
        /// # let mut schedule = Schedule::default();
        ///
        /// # schedule.add_stage("my_stage", NopStage);
        /// #
        /// schedule.stage("my_stage", |stage: &mut NopStage| {
        ///     // modify stage
        ///     stage
        /// });
        /// ```
        ///
        /// # Panics
        ///
        /// Panics if `label` refers to a non-existing stage, or if it's not of type `T`.
        pub fn stage<T: Stage, F: FnOnce(&mut T) -> &mut T>(
            &mut self,
            label: impl StageLabel,
            func: F,
        ) -> &mut Self {
            let stage = self.get_stage_mut::<T>(&label).unwrap_or_else(move || {
                panic!("stage '{label:?}' does not exist or is the wrong type")
            });
            func(stage);
            self
        }

        /// Returns a shared reference to the stage identified by `label`, if it exists.
        ///
        /// If the requested stage does not exist, `None` is returned instead.
        ///
        /// # Example
        ///
        /// ```
        /// # use maplibre::schedule::{Schedule, NopStage};
        /// #
        /// # let mut schedule = Schedule::default();
        /// # schedule.add_stage("my_stage", NopStage);
        /// #
        /// let stage = schedule.get_stage::<NopStage>(&"my_stage").unwrap();
        /// ```
        pub fn get_stage<T: Stage>(&self, label: &dyn StageLabel) -> Option<&T> {
            self.stages
                .get(label)
                .and_then(|stage| stage.downcast_ref::<T>())
        }

        /// Returns a unique, mutable reference to the stage identified by `label`, if it exists.
        ///
        /// If the requested stage does not exist, `None` is returned instead.
        ///
        /// # Example
        ///
        /// ```
        /// # use maplibre::schedule::{Schedule, NopStage};
        /// #
        /// # let mut schedule = Schedule::default();
        /// # schedule.add_stage("my_stage", NopStage);
        /// #
        /// let stage = schedule.get_stage_mut::<NopStage>(&"my_stage").unwrap();
        /// ```
        pub fn get_stage_mut<T: Stage>(&mut self, label: &dyn StageLabel) -> Option<&mut T> {
            self.stages
                .get_mut(label)
                .and_then(|stage| stage.downcast_mut::<T>())
        }

        /// Executes each [`Stage`] contained in the schedule, one at a time.
        pub fn run_once(&mut self, context: &mut MapContext) {
            for label in &self.stage_order {
                #[cfg(feature = "trace")]
                let _stage_span = tracing::info_span!("stage", name = ?label).entered();
                let stage = self.stages.get_mut(label).unwrap(); // TODO: Remove unwrap
                stage.run(context);
            }
        }

        pub fn clear(&mut self) {
            self.stage_order.clear();
            self.stages.clear();
        }

        /// Iterates over all of schedule's stages and their labels, in execution order.
        pub fn iter_stages(&self) -> impl Iterator<Item = (&dyn StageLabel, &dyn Stage)> {
            self.stage_order
                .iter()
                .map(move |label| (&**label, &*self.stages[label]))
        }

        /// Adds a system to the [`Stage`] identified by `stage_label`.
        ///
        /// # Examples
        ///
        /// ```
        /// # use maplibre::context::MapContext;
        /// # use maplibre::tcs::system::stage::SystemStage;
        /// # use maplibre::schedule::{Schedule, NopStage};
        /// #
        /// # let mut schedule = Schedule::default();
        /// # schedule.add_stage("my_stage", SystemStage::default());
        /// # fn my_system(context: &mut MapContext) {}
        /// #
        /// schedule.add_system_to_stage("my_stage", my_system);
        /// ```
        pub fn add_system_to_stage(
            &mut self,
            stage_label: impl StageLabel,
            system: impl IntoSystemContainer,
        ) -> &mut Self {
            let stage = self
                .get_stage_mut::<SystemStage>(&stage_label)
                .unwrap_or_else(move || {
                    panic!("Stage '{stage_label:?}' does not exist or is not a SystemStage")
                });
            stage.add_system(system);
            self
        }
    }

    impl Stage for Schedule {
        fn run(&mut self, context: &mut MapContext) {
            self.run_once(context);
        }
    }