vexide_core/
competition.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! Utilities for getting competition control state.

extern crate alloc;

use alloc::boxed::Box;
use core::{
    cell::UnsafeCell,
    future::{Future, IntoFuture},
    marker::{PhantomData, PhantomPinned},
    ops::ControlFlow,
    pin::{pin, Pin},
    task::{self, Poll},
};

use bitflags::bitflags;
use futures_core::Stream;
use pin_project::pin_project;
use vex_sdk::vexCompetitionStatus;

bitflags! {
    /// The status bits returned by [`vex_sdk::vexCompetitionStatus`].
    #[derive(Debug, Clone, Copy, Eq, PartialEq)]
    pub struct CompetitionStatus: u32 {
        /// Robot is disabled by field control.
        const DISABLED = 1 << 0;

        /// Robot is in autonomous mode.
        const AUTONOMOUS = 1 << 1;

        /// Robot is connected to competition control (either competition switch or field control).
        const CONNECTED = 1 << 2;

        /// Robot is connected to field control (NOT competition switch)
        const SYSTEM = 1 << 3;
    }
}

/// Represents a possible mode that robots can be set in during the competition lifecycle.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompetitionMode {
    /// The Disabled competition mode.
    ///
    /// When in disabled mode, voltage commands to motors are disabled. Motors are forcibly
    /// locked to the "coast" brake mode and cannot be moved.
    ///
    /// Robots may be placed into disabled mode at any point in the competition after
    /// connecting, but are typically disabled before the autonomous period, between
    /// autonomous and opcontrol periods, and following the opcontrol period of a match.
    Disabled,

    /// The Autonomous competition mode.
    ///
    /// When in autonomous mode, all motors and sensors may be accessed, however user
    /// input from controller buttons and joysticks is not available to be read.
    ///
    /// Robots may be placed into autonomous mode at any point in the competition after
    /// connecting, but are typically placed into this mode at the start of a match.
    Autonomous,

    /// The Driver Control (i.e. opcontrol) competition mode.
    ///
    /// When in opcontrol mode, all device access is available including access to
    /// controller joystick values for reading user-input from drive team members.
    ///
    /// Robots may be placed into opcontrol mode at any point in the competition after
    /// connecting, but are typically placed into this mode following the autonomous
    /// period.
    Driver,
}

/// Represents a type of system used to control competition state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompetitionSystem {
    /// Competition state is controlled by a VEX Field Controller.
    FieldControl,

    /// Competition state is controlled by a VEXnet competition switch.
    CompetitionSwitch,
}

impl CompetitionStatus {
    /// Checks if the robot is connected to a competition control system.
    #[must_use]
    pub const fn is_connected(&self) -> bool {
        self.contains(CompetitionStatus::CONNECTED)
    }

    /// Returns the current competition mode, or phase from these status flags.
    #[must_use]
    pub const fn mode(&self) -> CompetitionMode {
        if self.contains(Self::DISABLED) {
            CompetitionMode::Disabled
        } else if self.contains(Self::AUTONOMOUS) {
            CompetitionMode::Autonomous
        } else {
            CompetitionMode::Driver
        }
    }

    /// Returns the type of system currently controlling the robot's competition state, or [`None`] if the robot
    /// is not tethered to a competition controller.
    #[must_use]
    pub const fn system(&self) -> Option<CompetitionSystem> {
        if self.contains(CompetitionStatus::CONNECTED) {
            if self.contains(Self::SYSTEM) {
                Some(CompetitionSystem::FieldControl)
            } else {
                Some(CompetitionSystem::CompetitionSwitch)
            }
        } else {
            None
        }
    }
}

/// Returns the current competition status flags.
#[must_use]
pub fn status() -> CompetitionStatus {
    CompetitionStatus::from_bits_retain(unsafe { vexCompetitionStatus() })
}

/// Checks if the robot is connected to a competition control system.
#[must_use]
pub fn is_connected() -> bool {
    status().is_connected()
}

/// Returns the type of system currently controlling the robot's competition state, or [`None`] if the robot
/// is not tethered to a competition controller.
#[must_use]
pub fn system() -> Option<CompetitionSystem> {
    status().system()
}

/// Returns the current competition mode, or phase.
#[must_use]
pub fn mode() -> CompetitionMode {
    status().mode()
}

/// A stream of updates to the competition status.
///
/// See [`updates`] for more information.
pub struct CompetitionUpdates {
    last_status: Option<CompetitionStatus>,
}

impl Stream for CompetitionUpdates {
    type Item = CompetitionStatus;

    fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
        let current = status();

        // TODO: This should probably be done on a timer in the reactor.
        cx.waker().wake_by_ref();

        if self.last_status == Some(current) {
            Poll::Pending
        } else {
            self.get_mut().last_status = Some(current);
            Poll::Ready(Some(current))
        }
    }
}

impl CompetitionUpdates {
    /// Returns the last status update.
    ///
    /// This is slightly more efficient than calling [`status`] as it does not require another poll,
    /// however, it can be out of date if the stream has not been polled recently.
    pub fn last(&self) -> CompetitionStatus {
        self.last_status.unwrap_or_else(status)
    }
}

/// Gets a stream of updates to the competition status.
///
/// Yields the current status when first polled, and thereafter whenever the status changes.
#[must_use]
pub const fn updates() -> CompetitionUpdates {
    CompetitionUpdates { last_status: None }
}

/// A future which delegates to different futures depending on the current competition mode.
/// I.e., a tiny async runtime specifically for writing competition programs.
#[pin_project]
pub struct CompetitionRuntime<
    Shared: 'static,
    Return,
    MkConnected,
    MkDisconnected,
    MkDisabled,
    MkAutonomous,
    MkDriver,
> where
    MkConnected:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDisconnected:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDisabled:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkAutonomous:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDriver:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
{
    // Functions to generate tasks for each mode.
    mk_connected: MkConnected,
    mk_disconnected: MkDisconnected,
    mk_disabled: MkDisabled,
    mk_autonomous: MkAutonomous,
    mk_driver: MkDriver,

    /// A stream of updates to the competition status.
    #[pin]
    updates: CompetitionUpdates,

    /// The current status bits of the competition.
    status: CompetitionStatus,

    /// The current phase of the competition runtime.
    phase: CompetitionRuntimePhase,

    /// The task currently running, or [`None`] if no task is running.
    ///
    /// SAFETY:
    /// - The `'static` lifetime is a lie to the compiler, it actually borrows `self.shared`.
    ///   Therefore, tasks MUST NOT move their `&'static mut` references, or else they will
    ///   still be around when we call a `mk_*` function with a new mutable reference to it.
    ///   We rely on lifetime parametricity of the `mk_*` functions for this (see the HRTBs above).
    /// - This field MUST come before `shared`, as struct fields are dropped in declaration order.
    #[allow(clippy::type_complexity)]
    task: Option<Pin<Box<dyn Future<Output = ControlFlow<Return>> + 'static>>>,

    /// A cell containing the data shared between all tasks.
    ///
    /// SAFETY: This field MUST NOT be mutated while a task is running, as tasks may still hold
    ///         references to it. This is enforced to owners of this struct by the `Pin`,
    ///         but we have no such guardrails, as we cannot project the pin to it without
    ///         creating an invalid `Pin<&mut Shared>` before possibly (legally) moving it
    ///         during task creation.
    shared: UnsafeCell<Shared>,

    /// Keep `self.shared` in place while `self.task` references it.
    _pin: PhantomPinned,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CompetitionRuntimePhase {
    Initial,
    Disconnected,
    Connected,
    Mode(CompetitionMode),
}

impl<Shared, Return, MkConnected, MkDisconnected, MkDisabled, MkAutonomous, MkDriver> Future
    for CompetitionRuntime<
        Shared,
        Return,
        MkConnected,
        MkDisconnected,
        MkDisabled,
        MkAutonomous,
        MkDriver,
    >
where
    MkConnected:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDisconnected:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDisabled:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkAutonomous:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDriver:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
{
    type Output = Return;

    fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
        let mut this = self.as_mut().project();

        let old_phase = *this.phase;

        // Poll any updates to competition status.
        match this.updates.as_mut().poll_next(cx) {
            Poll::Ready(Some(new_status)) => {
                let old_status = *this.status;

                // Connected and Disconnected should not be interrupted by other changes.
                if *this.phase != CompetitionRuntimePhase::Connected
                    && *this.phase != CompetitionRuntimePhase::Disconnected
                {
                    // Decide which phase we're in based on the status update.
                    *this.phase = if !old_status.is_connected() && new_status.is_connected() {
                        CompetitionRuntimePhase::Connected
                    } else if old_status.is_connected() && !new_status.is_connected() {
                        CompetitionRuntimePhase::Disconnected
                    } else {
                        CompetitionRuntimePhase::Mode(new_status.mode())
                    };
                }

                *this.status = new_status;
            }
            Poll::Ready(None) => unreachable!(),
            _ => {}
        }

        if let Some(Poll::Ready(res)) = this.task.as_mut().map(|task| task.as_mut().poll(cx)) {
            // If a task says to break out of the competition lifecycle, then we pass the return value up.
            if let ControlFlow::Break(val) = res {
                return Poll::Ready(val);
            }

            // Reset the current task to nothing, since we're done with the previous task.
            *this.task = None;

            match *this.phase {
                // Transition into the current mode if we previously ran the connected/disconnected task and it completed.
                CompetitionRuntimePhase::Connected | CompetitionRuntimePhase::Disconnected => {
                    *this.phase = CompetitionRuntimePhase::Mode(this.updates.last().mode());
                }
                _ => {}
            }
        }

        // We're now in a different competition phase, so we need to start a new task.
        if old_phase != *this.phase {
            // SAFETY: Before we make a new `&mut Shared`, we ensure that the existing task is dropped.
            //         Note that although this would not normally ensure that the reference is dropped,
            //         because the task could move it elsewhere, this is not the case here, because
            //         the task generator functions (and therefore their returned futures) are valid for
            //         any _arbitrarily small_ lifetime `'t`. Therefore, they are unable to move it elsewhere
            //         without proving that the reference will be destroyed before the task returns.
            drop(this.task.take());
            let shared = unsafe { &mut *this.shared.get() };

            // Create a new task based on the new competition phase.
            *this.task = match *this.phase {
                CompetitionRuntimePhase::Initial => None,
                CompetitionRuntimePhase::Disconnected => Some((this.mk_disconnected)(shared)),
                CompetitionRuntimePhase::Connected => Some((this.mk_connected)(shared)),
                CompetitionRuntimePhase::Mode(CompetitionMode::Disabled) => {
                    Some((this.mk_disabled)(shared))
                }
                CompetitionRuntimePhase::Mode(CompetitionMode::Autonomous) => {
                    Some((this.mk_autonomous)(shared))
                }
                CompetitionRuntimePhase::Mode(CompetitionMode::Driver) => {
                    Some((this.mk_driver)(shared))
                }
            };
        }

        Poll::Pending
    }
}

impl<Shared, Return>
    CompetitionRuntime<
        Shared,
        Return,
        DefaultMk<Shared, Return>,
        DefaultMk<Shared, Return>,
        DefaultMk<Shared, Return>,
        DefaultMk<Shared, Return>,
        DefaultMk<Shared, Return>,
    >
{
    /// Create a typed builder for a competition runtime with the given `shared` data.
    /// The default tasks simply do nothing, so you do not need to supply them if you don't want to.
    pub const fn builder(shared: Shared) -> CompetitionBuilder<Shared, Return> {
        fn default_mk<Shared, Return>(
            _: &mut Shared,
        ) -> Pin<Box<dyn Future<Output = ControlFlow<Return>>>> {
            Box::pin(async { ControlFlow::Continue(()) })
        }

        CompetitionBuilder {
            shared,
            mk_connected: default_mk,
            mk_disconnected: default_mk,
            mk_disabled: default_mk,
            mk_autonomous: default_mk,
            mk_driver: default_mk,
            _return: PhantomData,
        }
    }
}

type DefaultMk<Shared, Return> =
    for<'t> fn(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>;

/// A typed builder for [`CompetitionRuntime`] instances.
pub struct CompetitionBuilder<
    Shared,
    Return,
    MkConnected = DefaultMk<Shared, Return>,
    MkDisconnected = DefaultMk<Shared, Return>,
    MkDisabled = DefaultMk<Shared, Return>,
    MkAutonomous = DefaultMk<Shared, Return>,
    MkDriver = DefaultMk<Shared, Return>,
> {
    shared: Shared,

    mk_connected: MkConnected,
    mk_disconnected: MkDisconnected,
    mk_disabled: MkDisabled,
    mk_autonomous: MkAutonomous,
    mk_driver: MkDriver,

    // We're invariant in the return type.
    _return: PhantomData<fn(Return) -> Return>,
}

impl<Shared, Return, MkConnected, MkDisconnected, MkDisabled, MkAutonomous, MkDriver>
    CompetitionBuilder<
        Shared,
        Return,
        MkConnected,
        MkDisconnected,
        MkDisabled,
        MkAutonomous,
        MkDriver,
    >
where
    MkConnected:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDisconnected:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDisabled:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkAutonomous:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDriver:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
{
    /// Finish the builder, returning a [`CompetitionRuntime`] instance.
    pub fn finish(
        self,
    ) -> CompetitionRuntime<
        Shared,
        Return,
        MkConnected,
        MkDisconnected,
        MkDisabled,
        MkAutonomous,
        MkDriver,
    > {
        CompetitionRuntime {
            mk_connected: self.mk_connected,
            mk_disconnected: self.mk_disconnected,
            mk_disabled: self.mk_disabled,
            mk_autonomous: self.mk_autonomous,
            mk_driver: self.mk_driver,
            status: status(),
            updates: updates(),
            phase: CompetitionRuntimePhase::Initial,
            task: None,
            shared: UnsafeCell::new(self.shared),
            _pin: PhantomPinned,
        }
    }
}

impl<Shared: 'static, Return, MkConnected, MkDisconnected, MkDisabled, MkAutonomous, MkDriver>
    IntoFuture
    for CompetitionBuilder<
        Shared,
        Return,
        MkConnected,
        MkDisconnected,
        MkDisabled,
        MkAutonomous,
        MkDriver,
    >
where
    MkConnected:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDisconnected:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDisabled:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkAutonomous:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
    MkDriver:
        for<'t> FnMut(&'t mut Shared) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 't>>,
{
    type Output = Return;

    type IntoFuture = CompetitionRuntime<
        Shared,
        Return,
        MkConnected,
        MkDisconnected,
        MkDisabled,
        MkAutonomous,
        MkDriver,
    >;

    fn into_future(self) -> Self::IntoFuture {
        self.finish()
    }
}

impl<Shared, Return, MkDisconnected, MkDisabled, MkAutonomous, MkDriver>
    CompetitionBuilder<
        Shared,
        Return,
        DefaultMk<Shared, Return>,
        MkDisconnected,
        MkDisabled,
        MkAutonomous,
        MkDriver,
    >
{
    /// Use the given function to create a task that runs when the robot is connected to a competition system.
    /// This task will run until termination before any other tasks (disconnected, disabled, autonomous, driver) are run.
    pub fn on_connect<Mk>(
        self,
        mk_connected: Mk,
    ) -> CompetitionBuilder<Shared, Return, Mk, MkDisconnected, MkDisabled, MkAutonomous, MkDriver>
    where
        Mk: for<'s> FnMut(
            &'s mut Shared,
        ) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 's>>,
    {
        CompetitionBuilder {
            shared: self.shared,
            mk_connected,
            mk_disconnected: self.mk_disconnected,
            mk_disabled: self.mk_disabled,
            mk_autonomous: self.mk_autonomous,
            mk_driver: self.mk_driver,
            _return: self._return,
        }
    }
}

impl<Shared, Return, MkConnected, MkDisabled, MkAutonomous, MkDriver>
    CompetitionBuilder<
        Shared,
        Return,
        MkConnected,
        DefaultMk<Shared, Return>,
        MkDisabled,
        MkAutonomous,
        MkDriver,
    >
{
    /// Use the given function to create a task that runs when the robot is disconnected from a competition system.
    /// This task will run until termination before any other tasks (connected, disabled, autonomous, driver) are run.
    pub fn on_disconnect<Mk>(
        self,
        mk_disconnected: Mk,
    ) -> CompetitionBuilder<Shared, Return, MkConnected, Mk, MkDisabled, MkAutonomous, MkDriver>
    where
        Mk: for<'s> FnMut(
            &'s mut Shared,
        ) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 's>>,
    {
        CompetitionBuilder {
            shared: self.shared,
            mk_connected: self.mk_connected,
            mk_disconnected,
            mk_disabled: self.mk_disabled,
            mk_autonomous: self.mk_autonomous,
            mk_driver: self.mk_driver,
            _return: self._return,
        }
    }
}

impl<Shared, Return, MkConnected, MkDisconnected, MkAutonomous, MkDriver>
    CompetitionBuilder<
        Shared,
        Return,
        MkConnected,
        MkDisconnected,
        DefaultMk<Shared, Return>,
        MkAutonomous,
        MkDriver,
    >
{
    /// Use the given function to create a task that runs while the robot is disabled.
    /// If the task terminates before the end of the disabled period, it will NOT be restarted.
    pub fn while_disabled<Mk>(
        self,
        mk_disabled: Mk,
    ) -> CompetitionBuilder<Shared, Return, MkConnected, MkDisconnected, Mk, MkAutonomous, MkDriver>
    where
        Mk: for<'s> FnMut(
            &'s mut Shared,
        ) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 's>>,
    {
        CompetitionBuilder {
            shared: self.shared,
            mk_connected: self.mk_connected,
            mk_disconnected: self.mk_disconnected,
            mk_disabled,
            mk_autonomous: self.mk_autonomous,
            mk_driver: self.mk_driver,
            _return: self._return,
        }
    }
}

impl<Shared, Return, MkConnected, MkDisconnected, MkDisabled, MkDriver>
    CompetitionBuilder<
        Shared,
        Return,
        MkConnected,
        MkDisconnected,
        MkDisabled,
        DefaultMk<Shared, Return>,
        MkDriver,
    >
{
    /// Use the given function to create a task that runs while the robot is autonomously controlled.
    /// If the task terminates before the end of the autonomous period, it will NOT be restarted.
    pub fn while_autonomous<Mk>(
        self,
        mk_autonomous: Mk,
    ) -> CompetitionBuilder<Shared, Return, MkConnected, MkDisconnected, MkDisabled, Mk, MkDriver>
    where
        Mk: for<'s> FnMut(
            &'s mut Shared,
        ) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 's>>,
    {
        CompetitionBuilder {
            shared: self.shared,
            mk_connected: self.mk_connected,
            mk_disconnected: self.mk_disconnected,
            mk_disabled: self.mk_disabled,
            mk_autonomous,
            mk_driver: self.mk_driver,
            _return: self._return,
        }
    }
}

impl<Shared, Return, MkConnected, MkDisconnected, MkDisabled, MkAutonomous>
    CompetitionBuilder<
        Shared,
        Return,
        MkConnected,
        MkDisconnected,
        MkDisabled,
        MkAutonomous,
        DefaultMk<Shared, Return>,
    >
{
    /// Use the given function to create a task that runs while the robot is driver controlled.
    /// If the task terminates before the end of the driver control period, it will NOT be restarted.
    pub fn while_driving<Mk>(
        self,
        mk_driver: Mk,
    ) -> CompetitionBuilder<Shared, Return, MkConnected, MkDisconnected, MkDisabled, MkAutonomous, Mk>
    where
        Mk: for<'s> FnMut(
            &'s mut Shared,
        ) -> Pin<Box<dyn Future<Output = ControlFlow<Return>> + 's>>,
    {
        CompetitionBuilder {
            shared: self.shared,
            mk_connected: self.mk_connected,
            mk_disconnected: self.mk_disconnected,
            mk_disabled: self.mk_disabled,
            mk_autonomous: self.mk_autonomous,
            mk_driver,
            _return: self._return,
        }
    }
}

/// A set of tasks to run when the competition is in a particular mode.
#[allow(async_fn_in_trait, clippy::unused_async)]
pub trait Compete: Sized {
    /// Runs when the competition system is connected.
    ///
    /// See [`CompetitionBuilder::on_connect`] for more information.
    async fn connected(&mut self) {}

    /// Runs when the competition system is disconnected.
    ///
    /// See [`CompetitionBuilder::on_disconnect`] for more information.
    async fn disconnected(&mut self) {}

    /// Runs when the robot is disabled.
    ///
    /// See [`CompetitionBuilder::while_disabled`] for more information.
    async fn disabled(&mut self) {}

    /// Runs when the robot is put into autonomous mode.
    ///
    /// See [`CompetitionBuilder::while_autonomous`] for more information.
    async fn autonomous(&mut self) {}

    /// Runs when the robot is put into driver control mode.
    ///
    /// See [`CompetitionBuilder::while_driving`] for more information.
    async fn driver(&mut self) {}
}

/// Extension methods for [`Compete`].
/// Automatically implemented for any type implementing [`Compete`].
#[allow(clippy::type_complexity)]
pub trait CompeteExt: Compete {
    /// Build a competition runtime that competes with this robot.
    fn compete(
        self,
    ) -> CompetitionRuntime<
        Self,
        !,
        impl for<'s> FnMut(&'s mut Self) -> Pin<Box<dyn Future<Output = ControlFlow<!>> + 's>>,
        impl for<'s> FnMut(&'s mut Self) -> Pin<Box<dyn Future<Output = ControlFlow<!>> + 's>>,
        impl for<'s> FnMut(&'s mut Self) -> Pin<Box<dyn Future<Output = ControlFlow<!>> + 's>>,
        impl for<'s> FnMut(&'s mut Self) -> Pin<Box<dyn Future<Output = ControlFlow<!>> + 's>>,
        impl for<'s> FnMut(&'s mut Self) -> Pin<Box<dyn Future<Output = ControlFlow<!>> + 's>>,
    > {
        #[allow(clippy::unit_arg)]
        CompetitionRuntime::builder(self)
            .on_connect(|s| Box::pin(async { ControlFlow::Continue(s.connected().await) }))
            .on_disconnect(|s| Box::pin(async { ControlFlow::Continue(s.disconnected().await) }))
            .while_disabled(|s| Box::pin(async { ControlFlow::Continue(s.disabled().await) }))
            .while_autonomous(|s| Box::pin(async { ControlFlow::Continue(s.autonomous().await) }))
            .while_driving(|s| Box::pin(async { ControlFlow::Continue(s.driver().await) }))
            .finish()
    }
}

impl<R: Compete> CompeteExt for R {}