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
//! A quantity that has a certain value that indicates missing or invalid data.
//!
//! Semantically this is no different than the `Option` type provided by the standard library.
//! However, that type uses an enum, which takes extra space as opposed to using a special value
//! to indicate missing or invalid data.

/// Defines the numeric value used to indicate missing data.
pub trait MissingData<T>: Default
where
    T: PartialEq + Copy,
{
    /// The value that indicates this quantity is invalid or missing.
    const MISSING: T;
}

/// A newtype to wrap a type and implement the `MissingData` trait.
#[derive(Clone, Copy, Debug)]
pub struct OptionVal<T: PartialEq + Copy + MissingData<T>> {
    value: T,
}

impl<T> OptionVal<T>
where
    T: PartialEq + Copy + MissingData<T>,
{
    /// Convert it to an option. The into trait should cover this, but the compiler has troubles
    /// inferring the type, so this function is provided. The Into trait is still useful in contexts
    /// where the compiler can infer the type correctly, like the body of this function.
    #[inline]
    pub fn as_option(self) -> Option<T> {
        self.into()
    }

    /// Despite its name, this method does not panic, it just returns the interior value even if
    /// that value is the 'Missing Data' flag value.
    #[inline]
    pub fn unwrap(self) -> T {
        self.value
    }
}

impl<T> Into<Option<T>> for OptionVal<T>
where
    T: PartialEq + Copy + MissingData<T>,
{
    #[inline]
    fn into(self) -> Option<T> {
        if self.value == T::MISSING {
            None
        } else {
            Some(self.value)
        }
    }
}

impl<T> Default for OptionVal<T>
where
    T: PartialEq + Copy + MissingData<T>,
{
    #[inline]
    fn default() -> Self {
        OptionVal::from(T::MISSING)
    }
}

impl<T> From<Option<T>> for OptionVal<T>
where
    T: PartialEq + Copy + MissingData<T>,
{
    #[inline]
    fn from(src: Option<T>) -> OptionVal<T> {
        if let Some(val) = src {
            OptionVal::from(val)
        } else {
            OptionVal::from(T::MISSING)
        }
    }
}

impl<T> From<T> for OptionVal<T>
where
    T: PartialEq + Copy + MissingData<T>,
{
    #[inline]
    fn from(src: T) -> OptionVal<T> {
        OptionVal { value: src }
    }
}

impl MissingData<f64> for f64 {
    const MISSING: f64 = -9999.0;
}

impl MissingData<i32> for i32 {
    const MISSING: i32 = -9999;
}