1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
//! Extension for [`std::option::Option`].
/// Extension methods for [`std::option::Option`].
pub trait OptionExt<T> {
/// Converts an `Option` to a `Result`.
///
/// Unlike the standard library,
/// the value of `Option` is treated as `Result::Err` instead of `Result::Ok`.
///
/// Arguments passed to `err_or` are eagerly evaluated;
/// if you are passing the result of a function call,
/// it is recommended to use [`OptionExt::err_or_else`], which is lazily evaluated.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::option::OptionExt;
///
/// assert_eq!(Some("err").err_or(0), Err("err"));
/// assert_eq!(None::<&str>.err_or(0), Ok(0));
/// ```
fn err_or<U>(self, ok: U) -> Result<U, T>;
/// Converts an `Option` to a `Result`.
///
/// Unlike the standard library,
/// the value of `Option` is treated as `Result::Err` instead of `Result::Ok`.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::option::OptionExt;
///
/// assert_eq!(Some("err").err_or_else(|| 0), Err("err"));
/// assert_eq!(None::<&str>.err_or_else(|| 0), Ok(0));
/// ```
fn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T>;
/// Converts an `Option` to a `Result`.
///
/// Unlike the standard library,
/// the value of `Option` is treated as `Result::Err` instead of `Result::Ok`.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::option::OptionExt;
///
/// assert_eq!(Some("err").err_or_default::<usize>(), Err("err"));
/// assert_eq!(None::<&str>.err_or_default::<usize>(), Ok(0));
/// ```
fn err_or_default<U: Default>(self) -> Result<U, T>;
/// Returns `true` if the `Option` is a `None` or the value inside of it matches a predicate.
///
/// This is equivalent to `option.is_some_and(!f)`.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::option::OptionExt;
///
/// assert_eq!(None::<usize>.is_none_or(|&i| i == 2), true);
/// assert_eq!(Some(5).is_none_or(|&i| i == 2), false);
/// ```
fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool;
/// Returns nothing, consuming the value contained.
///
/// ## Panics
///
/// Panics if the value is a `Some` with a custom panic message provided by `msg`.
///
/// ## Example
///
/// ```rust,should_panic
/// use rs_std_ext::option::OptionExt;
///
/// let x: Option<&str> = Some("10");
/// x.expect_none("fruits are healthy");// panics with `fruits are healthy`
/// ```
///
/// ## Recommended Message Style
///
/// See the documentation for [`Option::expect`][option_expect] for more information.
///
/// [option_expect]: std::option::Option#method.expect
fn expect_none(self, msg: &str);
/// Tap into an `Option` and modify its value.
///
/// This method captures the `Option` in order not to break the caller-chain.
///
/// ## Example
///
/// ```rust
/// use rs_std_ext::option::OptionExt;
///
/// let mut x: Option<String> = Some("4".into());
/// assert_eq!(x.tap_mut(|s| s.push('2')), Some("42".into()));
/// ```
fn tap_mut(self, f: impl FnOnce(&mut T)) -> Option<T>;
}
impl<T> OptionExt<T> for Option<T> {
fn err_or<U>(self, ok: U) -> Result<U, T> {
match self {
Some(t) => Err(t),
None => Ok(ok),
}
}
fn err_or_else<U>(self, f: impl FnOnce() -> U) -> Result<U, T> {
match self {
Some(t) => Err(t),
None => Ok(f()),
}
}
fn err_or_default<U: Default>(self) -> Result<U, T> {
match self {
Some(t) => Err(t),
None => Ok(U::default()),
}
}
fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool {
match self {
Some(t) => f(t),
None => true,
}
}
fn expect_none(self, msg: &str) {
if self.is_some() {
panic!("{}", msg)
}
}
fn tap_mut(mut self, f: impl FnOnce(&mut T)) -> Option<T> {
if let Some(val) = &mut self {
f(val)
}
self
}
}