krabmaga/engine/
priority.rs

1use std::cmp::{Eq, Ordering};
2use std::fmt;
3
4#[derive(Clone)]
5/// Struct to define the priority inside the schedule, two fields of f32
6pub struct Priority {
7    pub time: f32,
8    pub ordering: i32,
9}
10
11impl Priority {
12    /// create a new instance of Priority
13    pub fn new(the_time: f32, the_ordering: i32) -> Priority {
14        Priority {
15            time: the_time,
16            ordering: the_ordering,
17        }
18    }
19}
20
21impl Ord for Priority {
22    /// compare two Priority objects and return an Ordering
23    fn cmp(&self, other: &Priority) -> Ordering {
24        if self.time < other.time {
25            return Ordering::Greater;
26        }
27        if self.time > other.time {
28            return Ordering::Less;
29        }
30        if self.ordering < other.ordering {
31            return Ordering::Greater;
32        }
33        if self.ordering > other.ordering {
34            return Ordering::Less;
35        }
36        Ordering::Equal
37    }
38}
39
40impl PartialOrd for Priority {
41    fn partial_cmp(&self, other: &Priority) -> Option<Ordering> {
42        Some(self.cmp(other))
43    }
44}
45
46impl Eq for Priority {}
47
48impl PartialEq for Priority {
49    fn eq(&self, other: &Priority) -> bool {
50        self.ordering == other.ordering && self.time == other.time
51    }
52}
53
54impl fmt::Display for Priority {
55    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56        write!(f, "{} {}", self.time, self.ordering)
57    }
58}