1use std::cmp::Ordering;
12
13#[derive(Clone, Copy, PartialEq, Eq, Debug)]
14pub enum Effect {
15 Pure,
17 MayRead,
19 MayThrow,
21 MayWrite,
23 Unknown,
25}
26
27impl Effect {
28 pub fn is_pure(self) -> bool {
29 self == Effect::Pure
30 }
31
32 pub fn worst(self, other: Effect) -> Effect {
33 if self.rank() >= other.rank() {
34 self
35 } else {
36 other
37 }
38 }
39
40 fn rank(self) -> u8 {
41 match self {
42 Effect::Pure => 0,
43 Effect::MayRead => 1,
44 Effect::MayThrow => 2,
45 Effect::MayWrite => 3,
46 Effect::Unknown => 4,
47 }
48 }
49}
50
51impl PartialOrd for Effect {
52 fn partial_cmp(&self, other: &Effect) -> Option<Ordering> {
53 Some(self.cmp(other))
54 }
55}
56
57impl Ord for Effect {
58 fn cmp(&self, other: &Effect) -> Ordering {
59 self.rank().cmp(&other.rank())
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn ordering() {
69 assert!(Effect::Pure < Effect::MayRead);
70 assert!(Effect::MayRead < Effect::MayThrow);
71 assert!(Effect::MayThrow < Effect::MayWrite);
72 assert!(Effect::MayWrite < Effect::Unknown);
73 assert_eq!(Effect::Pure.worst(Effect::MayThrow), Effect::MayThrow);
74 }
75}