gloss_utils/
abi_stable_aliases.rs

1// For non-wasm targets, re-export the real abi_stable
2#[cfg(not(target_arch = "wasm32"))]
3pub use abi_stable::*;
4
5// For wasm target, define some standard type aliases
6#[cfg(target_arch = "wasm32")]
7pub mod std_types {
8    pub type RString = String;
9    pub type RDuration = Duration;
10
11    use std::collections::hash_map::RandomState;
12    pub type RVec<T> = Vec<T>;
13
14    //RVec has a function called from_slice that I also want defined on Vec
15    pub trait FromSliceExt<T> {
16        fn from_slice(slice: &[T]) -> Self
17        where
18            T: Clone;
19    }
20    impl<T> FromSliceExt<T> for RVec<T> {
21        fn from_slice(slice: &[T]) -> Self
22        where
23            T: Clone,
24        {
25            Vec::from(slice)
26        }
27    }
28
29    pub type RStr<'a> = &'a str;
30
31    //RStr has a function called from_str that I also want defined on str
32    pub trait FromStrExt<'a> {
33        fn from_str(string: &'a str) -> Self;
34    }
35
36    impl<'a> FromStrExt<'a> for RStr<'a> {
37        fn from_str(string: &'a str) -> Self {
38            string // Just returns the string as it is
39        }
40    }
41
42    // pub type RHashMap<K, V> = std::collections::HashMap<K, V>;
43    pub type RHashMap<K, V, S = RandomState> = std::collections::HashMap<K, V, S>;
44
45    #[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
46    pub struct Tuple2<A, B>(pub A, pub B);
47
48    pub mod map {
49        pub use std::collections::hash_map::Entry as REntry;
50    }
51
52    #[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
53    #[repr(u8)]
54    pub enum ROption<T> {
55        ///
56        RSome(T),
57        ///
58        RNone,
59    }
60
61    // use std::ops::Deref;
62    use std::time::Duration;
63
64    pub use self::ROption::*;
65
66    #[allow(clippy::missing_const_for_fn)]
67    impl<T> ROption<T> {
68        #[inline]
69        pub const fn as_ref(&self) -> ROption<&T> {
70            match self {
71                RSome(v) => RSome(v),
72                RNone => RNone,
73            }
74        }
75        #[inline]
76        pub fn as_mut(&mut self) -> ROption<&mut T> {
77            match self {
78                RSome(v) => RSome(v),
79                RNone => RNone,
80            }
81        }
82        #[inline]
83        pub const fn is_some(&self) -> bool {
84            matches!(self, RSome { .. })
85        }
86        #[inline]
87        pub const fn is_none(&self) -> bool {
88            matches!(self, RNone { .. })
89        }
90        #[inline]
91        pub fn into_option(self) -> Option<T> {
92            // self.into()
93            match self {
94                RSome(v) => Option::Some(v),
95                RNone => Option::None,
96            }
97        }
98        #[inline]
99        pub fn unwrap(self) -> T {
100            self.into_option().unwrap()
101        }
102        #[inline]
103        pub fn unwrap_or(self, def: T) -> T {
104            match self {
105                RSome(x) => x,
106                RNone => def,
107            }
108        }
109        #[inline]
110        pub fn map<U, F>(self, f: F) -> ROption<U>
111        where
112            F: FnOnce(T) -> U,
113        {
114            match self {
115                RSome(x) => RSome(f(x)),
116                RNone => RNone,
117            }
118        }
119    }
120
121    impl<T> Default for ROption<T> {
122        fn default() -> Self {
123            RNone
124        }
125    }
126    impl<T> From<Option<T>> for ROption<T> {
127        fn from(opt: Option<T>) -> Self {
128            match opt {
129                Some(v) => RSome(v),
130                None => RNone,
131            }
132        }
133    }
134    impl<T> Into<Option<T>> for ROption<T> {
135        fn into(self) -> Option<T> {
136            match self {
137                RSome(v) => Some(v),
138                RNone => None,
139            }
140        }
141    }
142}
143
144// Re-export standard types for wasm target
145#[cfg(target_arch = "wasm32")]
146pub use std_types::*;
147
148// For WASM, define an empty StableAbi trait so that references compile.
149#[cfg(target_arch = "wasm32")]
150pub trait StableAbi {}