shadow_shim_helper_rs/
option.rs
1use vasi::VirtualAddressSpaceIndependent;
2
3#[derive(
4 Copy,
5 Clone,
6 Debug,
7 Default,
8 PartialEq,
9 Eq,
10 PartialOrd,
11 Ord,
12 Hash,
13 VirtualAddressSpaceIndependent,
14)]
15#[repr(C)]
16pub enum FfiOption<T> {
17 #[default]
18 None,
19 Some(T),
20}
21
22impl<T> FfiOption<T> {
23 pub fn unwrap(self) -> T {
24 match self {
25 Self::Some(x) => x,
26 Self::None => panic!("called `FfiOption::unwrap()` on a `None` value"),
27 }
28 }
29
30 pub fn unwrap_or(self, default: T) -> T {
31 match self {
32 Self::Some(x) => x,
33 Self::None => default,
34 }
35 }
36
37 pub fn take(&mut self) -> Self {
38 let mut other = Self::None;
39 std::mem::swap(self, &mut other);
40 other
41 }
42
43 pub fn replace(&mut self, value: T) -> Self {
44 let mut other = Self::Some(value);
45 std::mem::swap(self, &mut other);
46 other
47 }
48
49 pub fn as_ref(&self) -> FfiOption<&T> {
50 match self {
51 Self::Some(x) => FfiOption::Some(x),
52 Self::None => FfiOption::None,
53 }
54 }
55
56 pub fn as_mut(&mut self) -> FfiOption<&mut T> {
57 match self {
58 Self::Some(x) => FfiOption::Some(x),
59 Self::None => FfiOption::None,
60 }
61 }
62}
63
64impl<T> From<Option<T>> for FfiOption<T> {
65 fn from(x: Option<T>) -> Self {
66 match x {
67 Some(x) => Self::Some(x),
68 None => Self::None,
69 }
70 }
71}