summaryrefslogtreecommitdiff
path: root/engine/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/util.rs')
-rw-r--r--engine/src/util.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/engine/src/util.rs b/engine/src/util.rs
index 0c81c71..f18a9c7 100644
--- a/engine/src/util.rs
+++ b/engine/src/util.rs
@@ -68,6 +68,32 @@ impl<Key: Ord, Value> Default for MapVec<Key, Value>
}
}
+pub trait OptionExt<T>
+{
+ /// Substitute for the currently experimental function
+ /// [`Option::get_or_try_insert_with`].
+ /// See https://github.com/rust-lang/rust/issues/143648
+ fn get_or_try_insert_with_fn<Err>(
+ &mut self,
+ func: impl Fn() -> Result<T, Err>,
+ ) -> Result<&mut T, Err>;
+}
+
+impl<T> OptionExt<T> for Option<T>
+{
+ fn get_or_try_insert_with_fn<Err>(
+ &mut self,
+ func: impl FnOnce() -> Result<T, Err>,
+ ) -> Result<&mut T, Err>
+ {
+ if let None = self {
+ *self = Some(func()?);
+ }
+
+ Ok(unsafe { self.as_mut().unwrap_unchecked() })
+ }
+}
+
macro_rules! try_option {
($expr: expr) => {
match $expr {