logo
  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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! An archived version of `Result`.

use core::{
    cmp::{Ord, Ordering, PartialOrd},
    hash, mem,
    ops::{Deref, DerefMut},
};

/// An archived [`Result`] that represents either success ([`Ok`](ArchivedResult::Ok)) or failure
/// ([`Err`](ArchivedResult::Err)).
#[derive(Debug)]
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[repr(u8)]
pub enum ArchivedResult<T, E> {
    /// Contains the success value
    Ok(T),
    /// Contains the error value
    Err(E),
}

impl<T, E> ArchivedResult<T, E> {
    /// Returns `true` if the result is [`Ok`](ArchivedResult::Ok).
    #[inline]
    pub const fn is_ok(&self) -> bool {
        matches!(self, ArchivedResult::Ok(_))
    }

    /// Returns `true` if the result is [`Err`](ArchivedResult::Err).
    #[inline]
    pub const fn is_err(&self) -> bool {
        matches!(self, ArchivedResult::Err(_))
    }

    /// Returns a `Result` containing the success and error values of this `ArchivedResult`.
    #[inline]
    pub fn as_ref(&self) -> Result<&T, &E> {
        match self {
            ArchivedResult::Ok(value) => Ok(value),
            ArchivedResult::Err(err) => Err(err),
        }
    }

    /// Converts from `&mut ArchivedResult<T, E>` to `Result<&mut T, &mut E>`.
    #[inline]
    pub fn as_mut(&mut self) -> Result<&mut T, &mut E> {
        match self {
            ArchivedResult::Ok(value) => Ok(value),
            ArchivedResult::Err(err) => Err(err),
        }
    }

    /// Returns an iterator over the possibly contained value.
    ///
    /// The iterator yields one value if the result is `ArchivedResult::Ok`, otherwise none.
    #[inline]
    pub fn iter(&self) -> Iter<'_, T> {
        Iter {
            inner: self.as_ref().ok(),
        }
    }

    /// Returns a mutable iterator over the possibly contained value.
    ///
    /// The iterator yields one value if the result is `ArchivedResult::Ok`, otherwise none.
    #[inline]
    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
        IterMut {
            inner: self.as_mut().ok(),
        }
    }
}

impl<T: Deref, E> ArchivedResult<T, E> {
    /// Converts from `&ArchivedResult<T, E>` to `Result<&<T as Deref>::Target, &E>`.
    ///
    /// Coerces the `Ok` variant of the original `ArchivedResult` via `Deref` and returns the new
    /// `Result`.
    #[inline]
    pub fn as_deref(&self) -> Result<&<T as Deref>::Target, &E> {
        match self {
            ArchivedResult::Ok(value) => Ok(value.deref()),
            ArchivedResult::Err(err) => Err(err),
        }
    }
}

impl<T: DerefMut, E> ArchivedResult<T, E> {
    /// Converts from `&mut ArchivedResult<T, E>` to `Result<&mut <T as Deref>::Target, &mut E>`.
    ///
    /// Coerces the `Ok` variant of the original `ArchivedResult` via `DerefMut` and returns the new
    /// `Result`.
    #[inline]
    pub fn as_deref_mut(&mut self) -> Result<&mut <T as Deref>::Target, &mut E> {
        match self {
            ArchivedResult::Ok(value) => Ok(value.deref_mut()),
            ArchivedResult::Err(err) => Err(err),
        }
    }
}

/// An iterator over a reference to the `Ok` variant of an [`ArchivedResult`].
///
/// The iterator yields one value if the result is `Ok`, otherwise none.
///
/// Created by [`ArchivedResult::iter`].
pub struct Iter<'a, T> {
    inner: Option<&'a T>,
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        let mut result = None;
        mem::swap(&mut self.inner, &mut result);
        result
    }
}

impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.next()
    }
}

/// An iterator over a mutable reference to the `Ok` variant of an [`ArchivedResult`].
///
/// The iterator yields one value if the result is `Ok`, otherwise none.
///
/// Created by [`ArchivedResult::iter_mut`].
pub struct IterMut<'a, T> {
    inner: Option<&'a mut T>,
}

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        let mut result = None;
        mem::swap(&mut self.inner, &mut result);
        result
    }
}

impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.next()
    }
}

impl<T: Eq, E: Eq> Eq for ArchivedResult<T, E> {}

impl<T: hash::Hash, E: hash::Hash> hash::Hash for ArchivedResult<T, E> {
    #[inline]
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.as_ref().hash(state)
    }
}

impl<T: Ord, E: Ord> Ord for ArchivedResult<T, E> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_ref().cmp(&other.as_ref())
    }
}

impl<T: PartialEq, E: PartialEq> PartialEq for ArchivedResult<T, E> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_ref().eq(&other.as_ref())
    }
}

impl<T: PartialOrd, E: PartialOrd> PartialOrd for ArchivedResult<T, E> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.as_ref().partial_cmp(&other.as_ref())
    }
}

impl<T, U: PartialEq<T>, E, F: PartialEq<E>> PartialEq<Result<T, E>> for ArchivedResult<U, F> {
    #[inline]
    fn eq(&self, other: &Result<T, E>) -> bool {
        match self {
            ArchivedResult::Ok(self_value) => {
                if let Ok(other_value) = other {
                    self_value.eq(other_value)
                } else {
                    false
                }
            }
            ArchivedResult::Err(self_err) => {
                if let Err(other_err) = other {
                    self_err.eq(other_err)
                } else {
                    false
                }
            }
        }
    }
}

impl<T: PartialEq<U>, U, E: PartialEq<F>, F> PartialEq<ArchivedResult<T, E>> for Result<U, F> {
    #[inline]
    fn eq(&self, other: &ArchivedResult<T, E>) -> bool {
        other.eq(self)
    }
}