stdont/option.rs
1/// Extensions for [`Option<T>`].
2///
3/// See also [ACP #212](https://github.com/rust-lang/libs-team/issues/212)
4pub trait OptionExt<T> {
5 /// Returns `true` if the option is a [`None`] or the value inside of it matches a predicate.
6 ///
7 /// ```rust
8 /// # use stdont::OptionExt as _;
9 ///
10 /// let x: Option<u32> = Some(2);
11 /// assert_eq!(x.is_none_or(|x| x > 1), true);
12 ///
13 /// let x: Option<u32> = Some(0);
14 /// assert_eq!(x.is_none_or(|x| x > 1), false);
15 ///
16 /// let x: Option<u32> = None;
17 /// assert_eq!(x.is_none_or(|x| x > 1), true);
18 /// ```
19 #[allow(clippy::wrong_self_convention)]
20 fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool;
21}
22
23impl<T> OptionExt<T> for Option<T> {
24 #[inline]
25 fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool {
26 match self {
27 None => true,
28 Some(x) => f(x),
29 }
30 }
31}