Skip to content

Commit c3cf3b3

Browse files
committed
Implement ToJson for all tuples
1 parent 035914e commit c3cf3b3

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/libserialize/json.rs

+26-14
Original file line numberDiff line numberDiff line change
@@ -2015,25 +2015,37 @@ impl ToJson for String {
20152015
fn to_json(&self) -> Json { String((*self).clone()) }
20162016
}
20172017

2018-
impl<A: ToJson, B: ToJson> ToJson for (A, B) {
2019-
fn to_json(&self) -> Json {
2020-
match *self {
2021-
(ref a, ref b) => {
2022-
List(vec![a.to_json(), b.to_json()])
2018+
macro_rules! tuple_impl {
2019+
// use variables to indicate the arity of the tuple
2020+
($($tyvar:ident),* ) => {
2021+
// the trailing commas are for the 1 tuple
2022+
impl<
2023+
$( $tyvar : ToJson ),*
2024+
> ToJson for ( $( $tyvar ),* , ) {
2025+
2026+
#[inline]
2027+
#[allow(uppercase_variables)]
2028+
fn to_json(&self) -> Json {
2029+
match *self {
2030+
($(ref $tyvar),*,) => List(vec![$($tyvar.to_json()),*])
2031+
}
20232032
}
20242033
}
20252034
}
20262035
}
20272036

2028-
impl<A: ToJson, B: ToJson, C: ToJson> ToJson for (A, B, C) {
2029-
fn to_json(&self) -> Json {
2030-
match *self {
2031-
(ref a, ref b, ref c) => {
2032-
List(vec![a.to_json(), b.to_json(), c.to_json()])
2033-
}
2034-
}
2035-
}
2036-
}
2037+
tuple_impl!{A}
2038+
tuple_impl!{A, B}
2039+
tuple_impl!{A, B, C}
2040+
tuple_impl!{A, B, C, D}
2041+
tuple_impl!{A, B, C, D, E}
2042+
tuple_impl!{A, B, C, D, E, F}
2043+
tuple_impl!{A, B, C, D, E, F, G}
2044+
tuple_impl!{A, B, C, D, E, F, G, H}
2045+
tuple_impl!{A, B, C, D, E, F, G, H, I}
2046+
tuple_impl!{A, B, C, D, E, F, G, H, I, J}
2047+
tuple_impl!{A, B, C, D, E, F, G, H, I, J, K}
2048+
tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L}
20372049

20382050
impl<'a, A: ToJson> ToJson for &'a [A] {
20392051
fn to_json(&self) -> Json { List(self.iter().map(|elt| elt.to_json()).collect()) }

0 commit comments

Comments
 (0)