diff options
| author | HampusM <hampus@hampusmat.com> | 2026-04-23 03:07:01 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-04-23 03:07:01 +0200 |
| commit | 6474394f49b67377c9fef66f51af61f108ae3cce (patch) | |
| tree | 60acc11667e315c3226dac82f130361bbec5a834 /engine/src/util.rs | |
| parent | 7699a1f498dd41d9abfc907d36e37dfed7a761c0 (diff) | |
Diffstat (limited to 'engine/src/util.rs')
| -rw-r--r-- | engine/src/util.rs | 26 |
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 { |
