//! This crate adds the missing Option::inspect function via an extension trait //! pub trait OptionInspect where F: FnOnce(&T), T: Sized, { fn inspect(self, f: F) -> Self; } pub trait OptionInspectRef where F: FnOnce(&T), T: Sized, { fn inspect(&self, f: F); } impl OptionInspect for Option where F: FnOnce(&T), T: Sized, { fn inspect(self, f: F) -> Self { if let Some(ref o) = self.as_ref() { (f)(o); } self } } impl OptionInspectRef for Option where F: FnOnce(&T), T: Sized, { fn inspect(&self, f: F) { if let Some(ref o) = self { (f)(o); } } } 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)(); } } }