about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2024-09-25 13:23:06 +0200
committerUrgau <urgau@numericable.fr>2024-09-26 10:43:56 +0200
commit1e241045873cf4aebb5f6fd7ef3a91f32196dbf1 (patch)
treea53e62857beaec5eb3a02f98f97d1c4c43213c29
parent702987f75b74f789ba227ee04a3d7bb1680c2309 (diff)
downloadrust-1e241045873cf4aebb5f6fd7ef3a91f32196dbf1.tar.gz
rust-1e241045873cf4aebb5f6fd7ef3a91f32196dbf1.zip
Add `[Option<T>; N]::transpose`
-rw-r--r--library/core/src/option.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index 30c667e2494..f8c631851e7 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -2545,3 +2545,27 @@ impl<T> Option<Option<T>> {
         }
     }
 }
+
+impl<T, const N: usize> [Option<T>; N] {
+    /// Transposes a `[Option<T>; N]` into a `Option<[T; N]>`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(option_array_transpose)]
+    /// # use std::option::Option;
+    ///
+    /// let data = [Some(0); 1000];
+    /// let data: Option<[u8; 1000]> = data.transpose();
+    /// assert_eq!(data, Some([0; 1000]));
+    ///
+    /// let data = [Some(0), None];
+    /// let data: Option<[u8; 2]> = data.transpose();
+    /// assert_eq!(data, None);
+    /// ```
+    #[inline]
+    #[unstable(feature = "option_array_transpose", issue = "130828")]
+    pub fn transpose(self) -> Option<[T; N]> {
+        self.try_map(core::convert::identity)
+    }
+}