summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs40
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);
+ }
+ }
+}