1use core::{
4 cmp::{Ord, Ordering, PartialOrd},
5 hash, mem,
6 ops::{Deref, DerefMut},
7};
8
9#[derive(Debug)]
12#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
13#[repr(u8)]
14pub enum ArchivedResult<T, E> {
15 Ok(T),
17 Err(E),
19}
20
21impl<T, E> ArchivedResult<T, E> {
22 #[inline]
24 pub const fn is_ok(&self) -> bool {
25 matches!(self, ArchivedResult::Ok(_))
26 }
27
28 #[inline]
30 pub const fn is_err(&self) -> bool {
31 matches!(self, ArchivedResult::Err(_))
32 }
33
34 #[inline]
36 pub fn as_ref(&self) -> Result<&T, &E> {
37 match self {
38 ArchivedResult::Ok(value) => Ok(value),
39 ArchivedResult::Err(err) => Err(err),
40 }
41 }
42
43 #[inline]
45 pub fn as_mut(&mut self) -> Result<&mut T, &mut E> {
46 match self {
47 ArchivedResult::Ok(value) => Ok(value),
48 ArchivedResult::Err(err) => Err(err),
49 }
50 }
51
52 #[inline]
56 pub fn iter(&self) -> Iter<'_, T> {
57 Iter {
58 inner: self.as_ref().ok(),
59 }
60 }
61
62 #[inline]
66 pub fn iter_mut(&mut self) -> IterMut<'_, T> {
67 IterMut {
68 inner: self.as_mut().ok(),
69 }
70 }
71}
72
73impl<T: Deref, E> ArchivedResult<T, E> {
74 #[inline]
79 pub fn as_deref(&self) -> Result<&<T as Deref>::Target, &E> {
80 match self {
81 ArchivedResult::Ok(value) => Ok(value.deref()),
82 ArchivedResult::Err(err) => Err(err),
83 }
84 }
85}
86
87impl<T: DerefMut, E> ArchivedResult<T, E> {
88 #[inline]
93 pub fn as_deref_mut(&mut self) -> Result<&mut <T as Deref>::Target, &mut E> {
94 match self {
95 ArchivedResult::Ok(value) => Ok(value.deref_mut()),
96 ArchivedResult::Err(err) => Err(err),
97 }
98 }
99}
100
101pub struct Iter<'a, T> {
107 inner: Option<&'a T>,
108}
109
110impl<'a, T> Iterator for Iter<'a, T> {
111 type Item = &'a T;
112
113 fn next(&mut self) -> Option<Self::Item> {
114 let mut result = None;
115 mem::swap(&mut self.inner, &mut result);
116 result
117 }
118}
119
120impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
121 fn next_back(&mut self) -> Option<Self::Item> {
122 self.next()
123 }
124}
125
126pub struct IterMut<'a, T> {
132 inner: Option<&'a mut T>,
133}
134
135impl<'a, T> Iterator for IterMut<'a, T> {
136 type Item = &'a mut T;
137
138 fn next(&mut self) -> Option<Self::Item> {
139 let mut result = None;
140 mem::swap(&mut self.inner, &mut result);
141 result
142 }
143}
144
145impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
146 fn next_back(&mut self) -> Option<Self::Item> {
147 self.next()
148 }
149}
150
151impl<T: Eq, E: Eq> Eq for ArchivedResult<T, E> {}
152
153impl<T: hash::Hash, E: hash::Hash> hash::Hash for ArchivedResult<T, E> {
154 #[inline]
155 fn hash<H: hash::Hasher>(&self, state: &mut H) {
156 self.as_ref().hash(state)
157 }
158}
159
160impl<T: Ord, E: Ord> Ord for ArchivedResult<T, E> {
161 #[inline]
162 fn cmp(&self, other: &Self) -> Ordering {
163 self.as_ref().cmp(&other.as_ref())
164 }
165}
166
167impl<T: PartialEq, E: PartialEq> PartialEq for ArchivedResult<T, E> {
168 #[inline]
169 fn eq(&self, other: &Self) -> bool {
170 self.as_ref().eq(&other.as_ref())
171 }
172}
173
174impl<T: PartialOrd, E: PartialOrd> PartialOrd for ArchivedResult<T, E> {
175 #[inline]
176 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
177 self.as_ref().partial_cmp(&other.as_ref())
178 }
179}
180
181impl<T, U: PartialEq<T>, E, F: PartialEq<E>> PartialEq<Result<T, E>> for ArchivedResult<U, F> {
182 #[inline]
183 fn eq(&self, other: &Result<T, E>) -> bool {
184 match self {
185 ArchivedResult::Ok(self_value) => {
186 if let Ok(other_value) = other {
187 self_value.eq(other_value)
188 } else {
189 false
190 }
191 }
192 ArchivedResult::Err(self_err) => {
193 if let Err(other_err) = other {
194 self_err.eq(other_err)
195 } else {
196 false
197 }
198 }
199 }
200 }
201}
202
203impl<T: PartialEq<U>, U, E: PartialEq<F>, F> PartialEq<ArchivedResult<T, E>> for Result<U, F> {
204 #[inline]
205 fn eq(&self, other: &ArchivedResult<T, E>) -> bool {
206 other.eq(self)
207 }
208}