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
use std::fmt;

/// A convenience type to explicitly mark
/// a function argument as `opt T`.
///
/// When left unspecified on Redscript side,
/// it translates to its `Default` representation.
#[derive(Default, Debug, Clone, Copy)]
pub enum Opt<T> {
    /// Value is specified and guaranteed to be non-`Default` value.
    NonDefault(T),
    /// `Default` value.
    #[default]
    Default,
}

impl<T> From<T> for Opt<T>
where
    T: Default + PartialEq,
{
    fn from(value: T) -> Self {
        if value == T::default() {
            return Opt::Default;
        }
        Opt::NonDefault(value)
    }
}

impl<T> Opt<T> {
    pub fn into_option(self) -> Option<T> {
        match self {
            Self::NonDefault(x) => Some(x),
            Self::Default => None,
        }
    }
}

impl<T> Opt<T>
where
    T: Default,
{
    pub fn unwrap_or_default(self) -> T {
        match self {
            Self::NonDefault(x) => x,
            Self::Default => T::default(),
        }
    }
}

impl<T> fmt::Display for Opt<T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NonDefault(x) => write!(f, "{}", x),
            Self::Default => write!(f, "{}", <Self as Default>::default()),
        }
    }
}

impl<T> PartialEq for Opt<T>
where
    T: Default + PartialEq,
{
    fn eq(&self, other: &Opt<T>) -> bool {
        match (self, other) {
            (Opt::NonDefault(lhs), Opt::NonDefault(rhs)) => lhs.eq(rhs),
            (Opt::NonDefault(lhs), Opt::Default) => lhs.eq(&T::default()),
            (Opt::Default, Opt::NonDefault(rhs)) => T::default().eq(rhs),
            (Opt::Default, Opt::Default) => true,
        }
    }
}

impl<T> PartialEq<T> for Opt<T>
where
    T: Default + PartialEq,
{
    fn eq(&self, other: &T) -> bool {
        match self {
            Self::NonDefault(x) if x == other => true,
            Self::Default if other == &T::default() => true,
            _ => false,
        }
    }
}

impl<T> Eq for Opt<T> where T: Eq + Default {}

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

impl<T> PartialOrd<T> for Opt<T>
where
    T: Default + PartialOrd + Ord,
{
    fn partial_cmp(&self, other: &T) -> Option<std::cmp::Ordering> {
        match (self, other) {
            (Self::NonDefault(lhs), rhs) => Some(lhs.cmp(rhs)),
            (Self::Default, rhs) => Some(T::default().cmp(rhs)),
        }
    }
}

impl<T> Ord for Opt<T>
where
    T: Default + Ord,
{
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match (self, other) {
            (Self::NonDefault(lhs), Self::NonDefault(rhs)) => lhs.cmp(rhs),
            (Self::NonDefault(lhs), Self::Default) => lhs.cmp(&T::default()),
            (Self::Default, Self::NonDefault(rhs)) => T::default().cmp(rhs),
            (Self::Default, Self::Default) => std::cmp::Ordering::Equal,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Opt;

    #[test]
    fn transitivity() {
        assert!(Opt::NonDefault(0).eq(&Opt::Default));
        assert!(Opt::<i32>::Default.eq(&Opt::NonDefault(0)));
        assert_eq!(
            Opt::NonDefault(0).cmp(&Opt::Default),
            std::cmp::Ordering::Equal
        );
        assert_eq!(
            Opt::<i32>::Default.cmp(&Opt::NonDefault(0)),
            std::cmp::Ordering::Equal
        );
    }
}