Skip to main content

nil_util/
ops.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use std::ops::{ControlFlow, Try};
5
6pub trait TryElse<T> {
7  fn unwrap_or_try_else<F, R>(self, f: F) -> R
8  where
9    F: FnOnce() -> R,
10    R: Try<Output = T>;
11}
12
13impl<T, U> TryElse<T> for U
14where
15  U: Try<Output = T>,
16{
17  fn unwrap_or_try_else<F, R>(self, f: F) -> R
18  where
19    F: FnOnce() -> R,
20    R: Try<Output = T>,
21  {
22    match self.branch() {
23      ControlFlow::Continue(value) => R::from_output(value),
24      ControlFlow::Break(..) => f(),
25    }
26  }
27}