diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-12-22 20:48:20 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-12-22 20:51:45 +0100 |
commit | 61ee13132538785edc6f8c520ca8f18fb5efb309 (patch) | |
tree | 2cc3c865990dc5e8def2728a64ae967da2ff03d6 /src |
v0.1.0
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..8c2fe74 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,40 @@ +//! This crate adds the missing Option::inspect function via an extension trait +//! + +pub trait OptionInspect<F, T> + where F: FnOnce(&T), + T: Sized +{ + fn inspect(self, f: F) -> Self; +} + +pub trait OptionInspectRef<F, T> + where F: FnOnce(&T), + T: Sized +{ + fn inspect(&self, f: F); +} + +impl<F, T> OptionInspect<F, T> for Option<T> + where F: FnOnce(&T), + T: Sized +{ + fn inspect(self, f: F) -> Self { + if let Some(ref o) = self.as_ref() { + (f)(&o); + } + + self + } +} + +impl<F, T> OptionInspectRef<F, T> for Option<T> + where F: FnOnce(&T), + T: Sized +{ + fn inspect(&self, f: F) { + if let Some(ref o) = self { + (f)(&o); + } + } +} |