From dcc5cd0c45a932b4a69491bc0d6d79b7fff8fa02 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 7 Mar 2021 18:14:58 +0100 Subject: Add variant for inspecting None Signed-off-by: Matthias Beyer --- src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 8c2fe74..9dfe3cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,3 +38,38 @@ impl OptionInspectRef for Option } } } + + +pub trait OptionInspectNone + where F: FnOnce(), +{ + fn inspect_none(self, f: F) -> Self; +} + +pub trait OptionInspectNoneRef + where F: FnOnce(), +{ + fn inspect_none(&self, f: F); +} + +impl OptionInspectNone for Option + where F: FnOnce() +{ + fn inspect_none(self, f: F) -> Self { + if let None = self.as_ref() { + (f)(); + } + + self + } +} + +impl OptionInspectNoneRef for Option + where F: FnOnce() +{ + fn inspect_none(&self, f: F) { + if let None = self { + (f)(); + } + } +} -- cgit v1.2.3