diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-03-07 18:14:58 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-03-07 18:14:59 +0100 |
commit | dcc5cd0c45a932b4a69491bc0d6d79b7fff8fa02 (patch) | |
tree | 43f03e01c1546fd0abc8d1e4eb21d8afc68a128a | |
parent | 61ee13132538785edc6f8c520ca8f18fb5efb309 (diff) |
Add variant for inspecting None
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/lib.rs | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -38,3 +38,38 @@ impl<F, T> OptionInspectRef<F, T> for Option<T> } } } + + +pub trait OptionInspectNone<F> + where F: FnOnce(), +{ + fn inspect_none(self, f: F) -> Self; +} + +pub trait OptionInspectNoneRef<F> + where F: FnOnce(), +{ + fn inspect_none(&self, f: F); +} + +impl<F, T> OptionInspectNone<F> for Option<T> + where F: FnOnce() +{ + fn inspect_none(self, f: F) -> Self { + if let None = self.as_ref() { + (f)(); + } + + self + } +} + +impl<F, T> OptionInspectNoneRef<F> for Option<T> + where F: FnOnce() +{ + fn inspect_none(&self, f: F) { + if let None = self { + (f)(); + } + } +} |