shadow_rs/core/work/
event_queue.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
use std::cmp::Reverse;
use std::collections::binary_heap::BinaryHeap;

use shadow_shim_helper_rs::emulated_time::EmulatedTime;

use super::event::Event;

/// A queue of [`Event`]s ordered by their times.
#[derive(Debug)]
pub struct EventQueue {
    queue: BinaryHeap<Reverse<PanickingOrd<Event>>>,
    last_popped_event_time: EmulatedTime,
}

impl EventQueue {
    pub fn new() -> Self {
        Self {
            queue: BinaryHeap::new(),
            last_popped_event_time: EmulatedTime::SIMULATION_START,
        }
    }

    /// Push a new [`Event`] on to the queue.
    ///
    /// Will panic if two events are pushed that have no relative order
    /// (`event_a.partial_cmp(&event_b) == None`). Will be non-deterministic if two events are
    /// pushed that are equal (`event_a == event_b`).
    ///
    /// Will panic if the event time is earlier than the last popped event time (time moves
    /// backward).
    pub fn push(&mut self, event: Event) {
        // make sure time never moves backward
        assert!(event.time() >= self.last_popped_event_time);

        self.queue.push(Reverse(event.into()));
    }

    /// Pop the earliest [`Event`] from the queue.
    pub fn pop(&mut self) -> Option<Event> {
        let event = self.queue.pop().map(|x| x.0.into_inner());

        // make sure time never moves backward
        if let Some(ref event) = event {
            assert!(event.time() >= self.last_popped_event_time);
            self.last_popped_event_time = event.time();
        }

        event
    }

    /// The time of the next [`Event`] (the time of the earliest event in the queue).
    pub fn next_event_time(&self) -> Option<EmulatedTime> {
        self.queue.peek().map(|x| x.0.time())
    }
}

impl Default for EventQueue {
    fn default() -> Self {
        Self::new()
    }
}

/// A wrapper type that implements [`Ord`] for types that implement [`PartialOrd`]. If the two
/// objects cannot be compared (`PartialOrd::partial_cmp` returns `None`), the comparison will
/// panic.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
struct PanickingOrd<T: PartialOrd + Eq>(T);

impl<T: PartialOrd + Eq> PanickingOrd<T> {
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl<T: PartialOrd + Eq> std::convert::From<T> for PanickingOrd<T> {
    fn from(x: T) -> Self {
        PanickingOrd(x)
    }
}

impl<T: PartialOrd + Eq> PartialOrd for PanickingOrd<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: PartialOrd + Eq> Ord for PanickingOrd<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.partial_cmp(&other.0).unwrap()
    }
}

impl<T: PartialOrd + Eq> std::ops::Deref for PanickingOrd<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: PartialOrd + Eq> std::ops::DerefMut for PanickingOrd<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}