about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-12-01 10:50:22 +0100
committerGitHub <noreply@github.com>2021-12-01 10:50:22 +0100
commitce197e2bceca00372c172a02a966b96287476c55 (patch)
treec9ea56c75b2c5ce11db09ba35d50ba517fc73c42
parentc09c16c0dff47b1ec143c266c3c06acdc84a4257 (diff)
parent2e8358e1ab933c6599b40294c0b33a1a17d19a23 (diff)
downloadrust-ce197e2bceca00372c172a02a966b96287476c55.tar.gz
rust-ce197e2bceca00372c172a02a966b96287476c55.zip
Rollup merge of #91346 - ibraheemdev:result-inspect, r=dtolnay
Add `Option::inspect` and `Result::{inspect, inspect_err}`

```rust
// core::result

impl Result<T, E> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
    pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self;
}

// core::option

impl Option<T> {
    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self;
}
```
-rw-r--r--library/core/src/option.rs25
-rw-r--r--library/core/src/result.rs47
2 files changed, 72 insertions, 0 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index baf9948857b..4eeb5e43943 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -848,6 +848,31 @@ impl<T> Option<T> {
         }
     }
 
+    /// Calls the provided closure with a reference to the contained value (if [`Some`]).
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(result_option_inspect)]
+    ///
+    /// let v = vec![1, 2, 3, 4, 5];
+    ///
+    /// // prints "got: 4"
+    /// let x: Option<&usize> = v.get(3).inspect(|x| println!("got: {}", x));
+    ///
+    /// // prints nothing
+    /// let x: Option<&usize> = v.get(5).inspect(|x| println!("got: {}", x));
+    /// ```
+    #[inline]
+    #[unstable(feature = "result_option_inspect", issue = "91345")]
+    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self {
+        if let Some(ref x) = self {
+            f(x);
+        }
+
+        self
+    }
+
     /// Returns the provided default result (if none),
     /// or applies a function to the contained value (if any).
     ///
diff --git a/library/core/src/result.rs b/library/core/src/result.rs
index 8fec2e928aa..a494c089f68 100644
--- a/library/core/src/result.rs
+++ b/library/core/src/result.rs
@@ -854,6 +854,53 @@ impl<T, E> Result<T, E> {
         }
     }
 
+    /// Calls the provided closure with a reference to the contained value (if [`Ok`]).
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(result_option_inspect)]
+    ///
+    /// let x: u8 = "4"
+    ///     .parse::<u8>()
+    ///     .inspect(|x| println!("original: {}", x))
+    ///     .map(|x| x.pow(3))
+    ///     .expect("failed to parse number");
+    /// ```
+    #[inline]
+    #[unstable(feature = "result_option_inspect", issue = "91345")]
+    pub fn inspect<F: FnOnce(&T)>(self, f: F) -> Self {
+        if let Ok(ref t) = self {
+            f(t);
+        }
+
+        self
+    }
+
+    /// Calls the provided closure with a reference to the contained error (if [`Err`]).
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(result_option_inspect)]
+    ///
+    /// use std::{fs, io};
+    ///
+    /// fn read() -> io::Result<String> {
+    ///     fs::read_to_string("address.txt")
+    ///         .inspect_err(|e| eprintln!("failed to read file: {}", e))
+    /// }
+    /// ```
+    #[inline]
+    #[unstable(feature = "result_option_inspect", issue = "91345")]
+    pub fn inspect_err<F: FnOnce(&E)>(self, f: F) -> Self {
+        if let Err(ref e) = self {
+            f(e);
+        }
+
+        self
+    }
+
     /////////////////////////////////////////////////////////////////////////
     // Iterator constructors
     /////////////////////////////////////////////////////////////////////////