Skip to content

Commit 9fba262

Browse files
committed
implement tuple<->array convertions via From
1 parent b7d8c88 commit 9fba262

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

library/core/src/tuple.rs

+40
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,26 @@ macro_rules! tuple_impls {
100100
}
101101
}
102102
}
103+
104+
#[stable(feature = "array_tuple_conv", since = "1.63.0")]
105+
impl<T> From<[T; count!($($T)+)]> for ($(${ignore(T)} T,)+) {
106+
#[inline]
107+
#[allow(non_snake_case)]
108+
fn from(array: [T; count!($($T)+)]) -> Self {
109+
let [$($T,)+] = array;
110+
($($T,)+)
111+
}
112+
}
113+
114+
#[stable(feature = "array_tuple_conv", since = "1.63.0")]
115+
impl<T> From<($(${ignore(T)} T,)+)> for [T; count!($($T)+)] {
116+
#[inline]
117+
#[allow(non_snake_case)]
118+
fn from(tuple: ($(${ignore(T)} T,)+)) -> Self {
119+
let ($($T,)+) = tuple;
120+
[$($T,)+]
121+
}
122+
}
103123
}
104124
}
105125

@@ -179,3 +199,23 @@ macro_rules! last_type {
179199
}
180200

181201
tuple_impls!(E D C B A Z Y X W V U T);
202+
203+
macro_rules! count {
204+
($($a:ident)*) => {
205+
0 $(${ignore(a)} + 1)*
206+
};
207+
}
208+
209+
#[stable(feature = "array_tuple_conv", since = "1.63.0")]
210+
impl<T> From<()> for [T; 0] {
211+
fn from((): ()) -> Self {
212+
[]
213+
}
214+
}
215+
216+
#[stable(feature = "array_tuple_conv", since = "1.63.0")]
217+
impl<T> From<[T; 0]> for () {
218+
fn from([]: [T; 0]) -> Self {
219+
()
220+
}
221+
}

0 commit comments

Comments
 (0)