Coverage Report

Created: 2026-02-14 07:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rkyv-0.8.15/src/option.rs
Line
Count
Source
1
//! An archived version of `Option`.
2
3
use core::{
4
    cmp, hash, mem,
5
    ops::{Deref, DerefMut},
6
};
7
8
use crate::{seal::Seal, Portable};
9
10
/// An archived [`Option`].
11
///
12
/// It functions identically to [`Option`] but has a different internal
13
/// representation to allow for archiving.
14
#[derive(Clone, Copy, Debug, Portable)]
15
#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
16
#[repr(u8)]
17
#[rkyv(crate)]
18
pub enum ArchivedOption<T> {
19
    /// No value
20
    None,
21
    /// Some value `T`
22
    Some(T),
23
}
24
25
impl<T> ArchivedOption<T> {
26
    /// Transforms the `ArchivedOption<T>` into a `Result<T, E>`, mapping
27
    /// `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
28
0
    pub fn ok_or<E>(self, err: E) -> Result<T, E> {
29
0
        match self {
30
0
            ArchivedOption::None => Err(err),
31
0
            ArchivedOption::Some(x) => Ok(x),
32
        }
33
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::ok_or::<_>
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::ok_or::<_>
34
    /// Returns the contained [`Some`] value, consuming the `self` value.
35
0
    pub fn unwrap(self) -> T {
36
0
        match self {
37
            ArchivedOption::None => {
38
0
                panic!("called `ArchivedOption::unwrap()` on a `None` value")
39
            }
40
0
            ArchivedOption::Some(value) => value,
41
        }
42
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::unwrap
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::unwrap
43
    /// Returns the contained [`Some`] value or a provided default.
44
0
    pub fn unwrap_or(self, default: T) -> T {
45
0
        match self {
46
0
            ArchivedOption::None => default,
47
0
            ArchivedOption::Some(value) => value,
48
        }
49
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::unwrap_or
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::unwrap_or
50
    /// Returns the contained [`Some`] value or computes it from a closure.
51
0
    pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
52
0
        match self {
53
0
            ArchivedOption::None => f(),
54
0
            ArchivedOption::Some(value) => value,
55
        }
56
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::unwrap_or_else::<_>
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::unwrap_or_else::<_>
57
    /// Returns `true` if the option is a `None` value.
58
0
    pub fn is_none(&self) -> bool {
59
0
        match self {
60
0
            ArchivedOption::None => true,
61
0
            ArchivedOption::Some(_) => false,
62
        }
63
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::is_none
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::is_none
64
65
    /// Returns `true` if the option is a `Some` value.
66
0
    pub fn is_some(&self) -> bool {
67
0
        match self {
68
0
            ArchivedOption::None => false,
69
0
            ArchivedOption::Some(_) => true,
70
        }
71
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::is_some
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::is_some
72
73
    /// Converts to an `Option<&T>`.
74
0
    pub const fn as_ref(&self) -> Option<&T> {
75
0
        match self {
76
0
            ArchivedOption::None => None,
77
0
            ArchivedOption::Some(value) => Some(value),
78
        }
79
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_ref
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_ref
80
81
    /// Converts to an `Option<&mut T>`.
82
0
    pub fn as_mut(&mut self) -> Option<&mut T> {
83
0
        match self {
84
0
            ArchivedOption::None => None,
85
0
            ArchivedOption::Some(value) => Some(value),
86
        }
87
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_mut
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_mut
88
89
    /// Converts from `Seal<'_, ArchivedOption<T>>` to `Option<Seal<'_, T>>`.
90
0
    pub fn as_seal(this: Seal<'_, Self>) -> Option<Seal<'_, T>> {
91
0
        let inner = unsafe { Seal::unseal_unchecked(this) };
92
0
        inner.as_mut().map(Seal::new)
93
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_seal
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_seal
94
95
    /// Returns an iterator over the possibly-contained value.
96
0
    pub const fn iter(&self) -> Iter<&'_ T> {
97
0
        Iter {
98
0
            inner: self.as_ref(),
99
0
        }
100
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::iter
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::iter
101
102
    /// Returns an iterator over the mutable possibly-contained value.
103
0
    pub fn iter_mut(&mut self) -> Iter<&'_ mut T> {
104
0
        Iter {
105
0
            inner: self.as_mut(),
106
0
        }
107
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::iter_mut
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::iter_mut
108
109
    /// Returns an iterator over the sealed possibly-contained value.
110
0
    pub fn iter_seal(this: Seal<'_, Self>) -> Iter<Seal<'_, T>> {
111
0
        Iter {
112
0
            inner: Self::as_seal(this),
113
0
        }
114
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::iter_seal
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::iter_seal
115
116
    /// Inserts `v` into the option if it is `None`, then returns a mutable
117
    /// reference to the contained value.
118
0
    pub fn get_or_insert(&mut self, v: T) -> &mut T {
119
0
        self.get_or_insert_with(move || v)
120
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::get_or_insert
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::get_or_insert
121
122
    /// Inserts a value computed from `f` into the option if it is `None`, then
123
    /// returns a mutable reference to the contained value.
124
0
    pub fn get_or_insert_with<F: FnOnce() -> T>(&mut self, f: F) -> &mut T {
125
0
        if let ArchivedOption::Some(ref mut value) = self {
126
0
            value
127
        } else {
128
0
            *self = ArchivedOption::Some(f());
129
0
            self.as_mut().unwrap()
130
        }
131
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::get_or_insert_with::<_>
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::get_or_insert_with::<_>
132
}
133
134
impl<T: Deref> ArchivedOption<T> {
135
    /// Converts from `&ArchivedOption<T>` to `Option<&T::Target>`.
136
    ///
137
    /// Leaves the original `ArchivedOption` in-place, creating a new one with a
138
    /// reference to the original one, additionally coercing the contents
139
    /// via `Deref`.
140
0
    pub fn as_deref(&self) -> Option<&<T as Deref>::Target> {
141
0
        self.as_ref().map(|x| x.deref())
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_deref::{closure#0}
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_deref::{closure#0}
142
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_deref
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_deref
143
}
144
145
impl<T: DerefMut> ArchivedOption<T> {
146
    /// Converts from `&mut ArchivedOption<T>` to `Option<&mut T::Target>`.
147
    ///
148
    /// Leaves the original `ArchivedOption` in-place, creating a new `Option`
149
    /// with a mutable reference to the inner type's `Deref::Target` type.
150
0
    pub fn as_deref_mut(&mut self) -> Option<&mut <T as Deref>::Target> {
151
0
        self.as_mut().map(|x| x.deref_mut())
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_deref_mut::{closure#0}
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_deref_mut::{closure#0}
152
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_deref_mut
Unexecuted instantiation: <rkyv::option::ArchivedOption<_>>::as_deref_mut
153
}
154
155
impl<T: Eq> Eq for ArchivedOption<T> {}
156
157
impl<T: hash::Hash> hash::Hash for ArchivedOption<T> {
158
0
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
159
0
        self.as_ref().hash(state)
160
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::hash::Hash>::hash::<_>
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::hash::Hash>::hash::<_>
161
}
162
163
impl<T: Ord> Ord for ArchivedOption<T> {
164
0
    fn cmp(&self, other: &Self) -> cmp::Ordering {
165
0
        self.as_ref().cmp(&other.as_ref())
166
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::Ord>::cmp
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::Ord>::cmp
167
}
168
169
impl<T: PartialEq> PartialEq for ArchivedOption<T> {
170
0
    fn eq(&self, other: &Self) -> bool {
171
0
        self.as_ref().eq(&other.as_ref())
172
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::PartialEq>::eq
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::PartialEq>::eq
173
}
174
175
impl<T: PartialOrd> PartialOrd for ArchivedOption<T> {
176
0
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
177
0
        self.as_ref().partial_cmp(&other.as_ref())
178
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::PartialOrd>::partial_cmp
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::PartialOrd>::partial_cmp
179
}
180
181
impl<T, U: PartialOrd<T>> PartialOrd<Option<T>> for ArchivedOption<U> {
182
0
    fn partial_cmp(&self, other: &Option<T>) -> Option<cmp::Ordering> {
183
0
        match (self, other) {
184
0
            (ArchivedOption::None, None) => Some(cmp::Ordering::Equal),
185
0
            (ArchivedOption::None, Some(_)) => Some(cmp::Ordering::Less),
186
0
            (ArchivedOption::Some(_), None) => Some(cmp::Ordering::Greater),
187
0
            (ArchivedOption::Some(self_value), Some(other_value)) => {
188
0
                self_value.partial_cmp(other_value)
189
            }
190
        }
191
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::PartialOrd<core::option::Option<_>>>::partial_cmp
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::PartialOrd<core::option::Option<_>>>::partial_cmp
192
}
193
194
impl<T, U: PartialEq<T>> PartialEq<Option<T>> for ArchivedOption<U> {
195
0
    fn eq(&self, other: &Option<T>) -> bool {
196
0
        if let ArchivedOption::Some(self_value) = self {
197
0
            if let Some(other_value) = other {
198
0
                self_value.eq(other_value)
199
            } else {
200
0
                false
201
            }
202
        } else {
203
0
            other.is_none()
204
        }
205
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<rend::u64_le> as core::cmp::PartialEq<core::option::Option<u64>>>::eq
Unexecuted instantiation: <rkyv::option::ArchivedOption<wasmer_compiler::types::section::ArchivedSectionIndex> as core::cmp::PartialEq<core::option::Option<wasmer_compiler::types::section::SectionIndex>>>::eq
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::PartialEq<core::option::Option<_>>>::eq
Unexecuted instantiation: <rkyv::option::ArchivedOption<rend::u64_le> as core::cmp::PartialEq<core::option::Option<u64>>>::eq
Unexecuted instantiation: <rkyv::option::ArchivedOption<wasmer_compiler::types::section::ArchivedSectionIndex> as core::cmp::PartialEq<core::option::Option<wasmer_compiler::types::section::SectionIndex>>>::eq
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::cmp::PartialEq<core::option::Option<_>>>::eq
206
}
207
208
impl<T> From<T> for ArchivedOption<T> {
209
    /// Moves `val` into a new [`Some`].
210
    ///
211
    /// # Examples
212
    ///
213
    /// ```
214
    /// # use rkyv::option::ArchivedOption;
215
    /// let o: ArchivedOption<u8> = ArchivedOption::from(67);
216
    ///
217
    /// assert!(matches!(o, ArchivedOption::Some(67)));
218
    /// ```
219
0
    fn from(val: T) -> ArchivedOption<T> {
220
0
        ArchivedOption::Some(val)
221
0
    }
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::convert::From<_>>::from
Unexecuted instantiation: <rkyv::option::ArchivedOption<_> as core::convert::From<_>>::from
222
}
223
224
/// An iterator over a reference to the `Some` variant of an `ArchivedOption`.
225
///
226
/// This iterator yields one value if the `ArchivedOption` is a `Some`,
227
/// otherwise none.
228
pub struct Iter<P> {
229
    inner: Option<P>,
230
}
231
232
impl<P> Iter<P> {
233
    /// Returns an iterator over the given `Option`.
234
0
    pub fn new(inner: Option<P>) -> Self {
235
0
        Self { inner }
236
0
    }
Unexecuted instantiation: <rkyv::option::Iter<_>>::new
Unexecuted instantiation: <rkyv::option::Iter<_>>::new
237
}
238
239
impl<P> Iterator for Iter<P> {
240
    type Item = P;
241
242
0
    fn next(&mut self) -> Option<Self::Item> {
243
0
        let mut result = None;
244
0
        mem::swap(&mut self.inner, &mut result);
245
0
        result
246
0
    }
Unexecuted instantiation: <rkyv::option::Iter<_> as core::iter::traits::iterator::Iterator>::next
Unexecuted instantiation: <rkyv::option::Iter<_> as core::iter::traits::iterator::Iterator>::next
247
}
248
249
impl<P> DoubleEndedIterator for Iter<P> {
250
0
    fn next_back(&mut self) -> Option<Self::Item> {
251
0
        self.next()
252
0
    }
Unexecuted instantiation: <rkyv::option::Iter<_> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back
Unexecuted instantiation: <rkyv::option::Iter<_> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back
253
}
254
255
impl<'a, T> IntoIterator for &'a ArchivedOption<T> {
256
    type Item = &'a T;
257
    type IntoIter = Iter<&'a T>;
258
259
0
    fn into_iter(self) -> Self::IntoIter {
260
0
        self.iter()
261
0
    }
Unexecuted instantiation: <&rkyv::option::ArchivedOption<_> as core::iter::traits::collect::IntoIterator>::into_iter
Unexecuted instantiation: <&rkyv::option::ArchivedOption<_> as core::iter::traits::collect::IntoIterator>::into_iter
262
}
263
264
impl<'a, T> IntoIterator for &'a mut ArchivedOption<T> {
265
    type Item = &'a mut T;
266
    type IntoIter = Iter<&'a mut T>;
267
268
0
    fn into_iter(self) -> Self::IntoIter {
269
0
        self.iter_mut()
270
0
    }
Unexecuted instantiation: <&mut rkyv::option::ArchivedOption<_> as core::iter::traits::collect::IntoIterator>::into_iter
Unexecuted instantiation: <&mut rkyv::option::ArchivedOption<_> as core::iter::traits::collect::IntoIterator>::into_iter
271
}
272
273
impl<'a, T> IntoIterator for Seal<'a, ArchivedOption<T>> {
274
    type Item = Seal<'a, T>;
275
    type IntoIter = Iter<Seal<'a, T>>;
276
277
0
    fn into_iter(self) -> Self::IntoIter {
278
0
        ArchivedOption::iter_seal(self)
279
0
    }
Unexecuted instantiation: <rkyv::seal::Seal<rkyv::option::ArchivedOption<_>> as core::iter::traits::collect::IntoIterator>::into_iter
Unexecuted instantiation: <rkyv::seal::Seal<rkyv::option::ArchivedOption<_>> as core::iter::traits::collect::IntoIterator>::into_iter
280
}
281
282
#[cfg(test)]
283
mod tests {
284
    use super::*;
285
286
    #[test]
287
    fn partial_ord_option() {
288
        use core::cmp::Ordering;
289
290
        use super::ArchivedOption;
291
292
        let a: ArchivedOption<u8> = ArchivedOption::Some(42);
293
        let b = Some(42);
294
        assert_eq!(Some(Ordering::Equal), a.partial_cmp(&b));
295
296
        let a: ArchivedOption<u8> = ArchivedOption::Some(1);
297
        let b = Some(2);
298
        assert_eq!(Some(Ordering::Less), a.partial_cmp(&b));
299
300
        let a: ArchivedOption<u8> = ArchivedOption::Some(2);
301
        let b = Some(1);
302
        assert_eq!(Some(Ordering::Greater), a.partial_cmp(&b));
303
    }
304
305
    #[test]
306
    fn into_iter() {
307
        let x: ArchivedOption<u8> = ArchivedOption::Some(1);
308
        let mut iter = IntoIterator::into_iter(&x);
309
        assert_eq!(iter.next(), Some(&1));
310
        assert_eq!(iter.next(), None);
311
312
        let x: ArchivedOption<u8> = ArchivedOption::None;
313
        let mut iter = IntoIterator::into_iter(&x);
314
        assert_eq!(iter.next(), None);
315
    }
316
}