about summary refs log tree commit diff
path: root/library/core/src/tuple.rs
diff options
context:
space:
mode:
authorMaybe Waffle <waffle.lapkin@gmail.com>2022-05-31 22:15:50 +0400
committerMaybe Waffle <waffle.lapkin@gmail.com>2023-05-02 14:37:40 +0000
commit9fba2622a018c69c9dcff378be1d47e63948e127 (patch)
tree417076153cb883f9c5961c995718bfa76c49ba5b /library/core/src/tuple.rs
parentb7d8c88b64843d05761576aa093a34a8bc176817 (diff)
downloadrust-9fba2622a018c69c9dcff378be1d47e63948e127.tar.gz
rust-9fba2622a018c69c9dcff378be1d47e63948e127.zip
implement tuple<->array convertions via `From`
Diffstat (limited to 'library/core/src/tuple.rs')
-rw-r--r--library/core/src/tuple.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/library/core/src/tuple.rs b/library/core/src/tuple.rs
index 75d7a3f4005..da3384f5177 100644
--- a/library/core/src/tuple.rs
+++ b/library/core/src/tuple.rs
@@ -100,6 +100,26 @@ macro_rules! tuple_impls {
                 }
             }
         }
+
+        #[stable(feature = "array_tuple_conv", since = "1.63.0")]
+        impl<T> From<[T; count!($($T)+)]> for ($(${ignore(T)} T,)+) {
+            #[inline]
+            #[allow(non_snake_case)]
+            fn from(array: [T; count!($($T)+)]) -> Self {
+                let [$($T,)+] = array;
+                ($($T,)+)
+            }
+        }
+
+        #[stable(feature = "array_tuple_conv", since = "1.63.0")]
+        impl<T> From<($(${ignore(T)} T,)+)> for [T; count!($($T)+)] {
+            #[inline]
+            #[allow(non_snake_case)]
+            fn from(tuple: ($(${ignore(T)} T,)+)) -> Self {
+                let ($($T,)+) = tuple;
+                [$($T,)+]
+            }
+        }
     }
 }
 
@@ -179,3 +199,23 @@ macro_rules! last_type {
 }
 
 tuple_impls!(E D C B A Z Y X W V U T);
+
+macro_rules! count {
+    ($($a:ident)*) => {
+        0 $(${ignore(a)} + 1)*
+    };
+}
+
+#[stable(feature = "array_tuple_conv", since = "1.63.0")]
+impl<T> From<()> for [T; 0] {
+    fn from((): ()) -> Self {
+        []
+    }
+}
+
+#[stable(feature = "array_tuple_conv", since = "1.63.0")]
+impl<T> From<[T; 0]> for () {
+    fn from([]: [T; 0]) -> Self {
+        ()
+    }
+}