option_inspect_none/
lib.rs1pub trait OptionInspectNone<T> {
2 fn inspect_none(self, inspector_function: impl FnOnce()) -> Self;
3}
4
5impl<T> OptionInspectNone<T> for Option<T> {
6 fn inspect_none(self, inspector_function: impl FnOnce()) -> Self {
7 match &self {
8 Some(_) => (),
9 None => inspector_function(),
10 }
11 self
12 }
13}
14
15impl<T> OptionInspectNone<T> for &Option<T> {
16 fn inspect_none(self, inspector_function: impl FnOnce()) -> Self {
17 match &self {
18 Some(_) => (),
19 None => inspector_function(),
20 }
21 self
22 }
23}
24
25#[cfg(test)]
26mod tests {
27 use crate::OptionInspectNone;
28
29 #[test]
30 fn inspect_none_on_some() {
31 let mut inspector_function_called = false;
32 Some(()).inspect_none(|| inspector_function_called = true);
33 assert!(!inspector_function_called);
34 }
35
36 #[test]
37 fn inspect_none_on_none() {
38 let mut inspector_function_called = false;
39 None::<()>.inspect_none(|| inspector_function_called = true);
40 assert!(inspector_function_called);
41 }
42}