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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]

//! # or
//!
//! A generalized Result.
//!

/// A generalized Result, just a two-variant enum.
///
/// Much of the functionality of Result and Option is not redundantly
/// provided here. An Option is always just a few characters away through
/// the `a` and `b` methods, which you should combine with `as_ref` and
/// `as_mut` to get the full spectrum of provided functionality.
#[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)]
pub enum Or<A, B> {
    /// One variant
    A(A),
    /// Another variant
    B(B)
}

impl<A, B> Or<A, B> {
    /// Returns true if the `Or` is `A`
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use or::Or;
    /// let x: Or<i32, ()> = Or::A(545);
    /// assert!(x.is_a());
    ///
    /// let y: Or<(), i32> = Or::B(2);
    /// assert!(!y.is_a());
    /// ```
    pub fn is_a(&self) -> bool {
        self.as_ref().a().is_some()
    }

    /// Returns true if the `Or` is `B`
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use or::Or;
    /// let x: Or<i32, ()> = Or::A(545);
    /// assert!(!x.is_b());
    ///
    /// let y: Or<(), i32> = Or::B(2);
    /// assert!(y.is_b());
    /// ```
    pub fn is_b(&self) -> bool {
        self.as_ref().b().is_some()
    }

    /// Converts form `Or<A, B>` to `Option<A>`
    ///
    /// This method consumes `self` and discards `B`, if any.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use or::Or;
    /// let x: Or<i32, ()> = Or::A(545);
    /// assert_eq!(x.a(), Some(545));
    ///
    /// let y: Or<(), i32> = Or::B(2);
    /// assert!(y.a().is_none());
    /// ```
    pub fn a(self) -> Option<A> {
        match self {
            Or::A(a) => Some(a),
            _ => None
        }
    }

    /// Converts form `Or<A, B>` to `Option<B>`
    ///
    /// This method consumes `self` and discards `A`, if any.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use or::Or;
    /// let x: Or<i32, ()> = Or::A(545);
    /// assert!(x.b().is_none());
    ///
    /// let y: Or<(), i32> = Or::B(2);
    /// assert_eq!(y.b(), Some(2));
    /// ```
    pub fn b(self) -> Option<B> {
        match self {
            Or::B(b) => Some(b),
            _ => None
        }
    }

    /// Convert from `&Or<A, B>` to `Or<&A, &B>`
    ///
    /// The returned `Or` contains references into the existing
    /// `Or`, which is left in place.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use or::Or;
    /// let x: Or<String, ()> = Or::A("hello".to_string());
    /// assert_eq!(&**x.as_ref().a().unwrap(), "hello");
    ///
    /// let y: Or<(), i32> = Or::B(2);
    /// assert!(y.as_ref().a().is_none());
    /// ```
    pub fn as_ref(&self) -> Or<&A, &B> {
        match *self {
            Or::A(ref a) => Or::A(a),
            Or::B(ref b) => Or::B(b),
        }
    }

    /// Convert from `&mut Or<A, B>` to `Or<&mut A, &mut B>`
    ///
    /// The returned `Or` contains references into the existing
    /// `Or`, which is left in place.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use or::Or;
    /// let mut x: Or<String, ()> = Or::A("hello".to_string());
    /// x.as_mut().a().map(|s| s.push_str(" world!"));
    ///
    /// assert_eq!(&**x.as_ref().a().unwrap(), "hello world!");
    /// ```
    pub fn as_mut(&mut self) -> Or<&mut A, &mut B> {
        match *self {
            Or::A(ref mut a) => Or::A(a),
            Or::B(ref mut b) => Or::B(b),
        }
    }

    /// Convert from `Or<A, B>` to `Or<B, A>`
    ///
    /// Consumes `self` and returns a new `Or`
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use or::Or;
    /// let x: Or<(), i32> = Or::A(73).swap();
    ///
    /// assert_eq!(x.b().unwrap(), 73);
    /// ```
    pub fn swap(self) -> Or<B, A> {
        match self {
            Or::A(b) => Or::B(b),
            Or::B(a) => Or::A(a)
        }
    }
}