Skip to content

Commit ddc72fe

Browse files
committed
Add OptionExt with is_none_or to rustc_data_structures
1 parent 7592c13 commit ddc72fe

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

compiler/rustc_data_structures/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ pub mod work_queue;
9494

9595
mod atomic_ref;
9696
mod hashes;
97+
mod option_ext;
9798

9899
pub use atomic_ref::AtomicRef;
100+
pub use option_ext::OptionExt;
99101

100102
#[inline(never)]
101103
#[cold]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// Extension for [`Option`] that adds handy methods missing from it.
2+
pub trait OptionExt {
3+
type T;
4+
5+
fn is_none_or(self, f: impl FnOnce(Self::T) -> bool) -> bool;
6+
}
7+
8+
impl<T> OptionExt for Option<T> {
9+
type T = T;
10+
11+
/// Returns `true` is `self` is `None` or the value inside matches a predicate `f`.
12+
fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool {
13+
match self {
14+
None => true,
15+
Some(x) => f(x),
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)