Skip to content

Commit

Permalink
Impl DigestAs for tuples
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Varlakov <[email protected]>
  • Loading branch information
survived committed Aug 22, 2024
1 parent cf054b8 commit 14a58eb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
39 changes: 39 additions & 0 deletions udigest/src/as_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,42 @@ where
U::digest_as(value.as_ref(), encoder)
}
}

macro_rules! impl_for_tuples {
($($t:ident, $as:ident),*) => {
impl<$($t, $as),*> DigestAs<($($t,)*)> for ($($as,)*)
where
$($as: DigestAs<$t>),*
{
fn digest_as<Buf: Buffer>(value: &($($t,)*), encoder: encoding::EncodeValue<Buf>) {
#[allow(non_snake_case)]
let ($($t,)*) = value;
(
$(As::<&$t, &$as>::new($t),)*
).unambiguously_encode(encoder)
}
}
};
}

#[rustfmt::skip]
mod tuples {
use super::*;

impl_for_tuples!(T, U);
impl_for_tuples!(T0, As0, T1, As1);
impl_for_tuples!(T0, As0, T1, As1, T2, As2);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7, T8, As8);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7, T8, As8, T9, As9);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7, T8, As8, T9, As9, T10, As10);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7, T8, As8, T9, As9, T10, As10, T11, As11);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7, T8, As8, T9, As9, T10, As10, T11, As11, T12, As12);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7, T8, As8, T9, As9, T10, As10, T11, As11, T12, As12, T13, As13);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7, T8, As8, T9, As9, T10, As10, T11, As11, T12, As12, T13, As13, T14, As14);
impl_for_tuples!(T0, As0, T1, As1, T2, As2, T3, As3, T4, As4, T5, As5, T6, As6, T7, As7, T8, As8, T9, As9, T10, As10, T11, As11, T12, As12, T13, As13, T14, As14, T15, As15);
}
29 changes: 18 additions & 11 deletions udigest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,10 @@ impl<T: Digestable, E: Digestable> Digestable for Result<T, E> {

macro_rules! digestable_tuple {
($($letter:ident),+) => {
impl<$($letter: Digestable),+> Digestable for ($($letter),+) {
impl<$($letter: Digestable),+> Digestable for ($($letter,)+) {
fn unambiguously_encode<BUF: Buffer>(&self, encoder: encoding::EncodeValue<BUF>) {
#[allow(non_snake_case)]
let ($($letter),+) = self;
let ($($letter,)+) = self;
let mut list = encoder.encode_list();
$(
let item_encoder = list.add_item();
Expand All @@ -494,16 +494,23 @@ macro_rules! digestable_tuple {
};
}

macro_rules! digestable_tuples {
($letter:ident) => {};
($letter:ident, $($others:ident),+) => {
digestable_tuple!($letter, $($others),+);
digestable_tuples!($($others),+);
}
}

// We support tuples with up to 16 elements
digestable_tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
digestable_tuple!(A);
digestable_tuple!(A, B);
digestable_tuple!(A, B, C);
digestable_tuple!(A, B, C, D);
digestable_tuple!(A, B, C, D, E);
digestable_tuple!(A, B, C, D, E, F);
digestable_tuple!(A, B, C, D, E, F, G);
digestable_tuple!(A, B, C, D, E, F, G, H);
digestable_tuple!(A, B, C, D, E, F, G, H, I);
digestable_tuple!(A, B, C, D, E, F, G, H, I, J);
digestable_tuple!(A, B, C, D, E, F, G, H, I, J, K);
digestable_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);
digestable_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M);
digestable_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
digestable_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
digestable_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);

fn unambiguously_encode_iter<B: Buffer, T: Digestable>(
encoder: encoding::EncodeValue<B>,
Expand Down

0 comments on commit 14a58eb

Please sign in to comment.