1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
//! An extension to add [`inspect`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.inspect) method to [`Option`] and [`Result`] types. pub use self::prelude::*; pub mod prelude { pub trait OptionInspector<T> { /// Does something with the contained [`Some`] value element of an [`Option`], passing the value on. /// /// # Examples /// ```rust /// use respector::prelude::*; /// /// assert_eq!(Some(10).inspect(|x| println!("Some({})", x)), Some(10)); // Prints `Some(10)`. /// assert_eq!(None::<i32>.inspect(|x| println!("Some({})", x)), None); // Prints nothing. /// ``` fn inspect<F: FnMut(&T)>(self, f: F) -> Option<T>; } impl<T> OptionInspector<T> for Option<T> { fn inspect<F: FnMut(&T)>(self, mut f: F) -> Option<T> { self.map(|it| { f(&it); it }) } } pub trait ResultInspector<T, E> { /// Does something with the contained [`Ok`] value element of a [`Result`], passing the value on. /// /// # Examples /// ```rust /// use respector::prelude::*; /// /// assert_eq!( /// Ok::<_, ()>(10).inspect(|x| println!("Ok({})", x)), /// Ok(10) /// ); // Prints `Ok(10)`. /// assert_eq!( /// Err::<(), _>(10).inspect(|x| println!("Ok({:?})", x)), /// Err(10) /// ); // Prints nothing. /// ``` fn inspect<F: FnMut(&T)>(self, f: F) -> Result<T, E>; /// Does something with the contained [`Err`] value element of a [`Result`], passing the value on. /// /// # Examples /// ```rust /// use respector::prelude::*; /// /// assert_eq!( /// Err::<(), _>(10).inspect_err(|x| println!("Err({})", x)), /// Err(10) /// ); // Prints `Err(10)`. /// assert_eq!( /// Ok::<_, ()>(10).inspect_err(|x| println!("Err({:?})", x)), /// Ok(10) /// ); // Prints nothing. /// ``` fn inspect_err<F: FnMut(&E)>(self, f: F) -> Result<T, E>; } impl<T, E> ResultInspector<T, E> for Result<T, E> { fn inspect<F: FnMut(&T)>(self, mut f: F) -> Result<T, E> { self.map(|it| { f(&it); it }) } fn inspect_err<F: FnMut(&E)>(self, mut f: F) -> Result<T, E> { self.map_err(|err| { f(&err); err }) } } }