Skip to content

Instantly share code, notes, and snippets.

@cynecx
Created May 27, 2024 02:00
Show Gist options
  • Select an option

  • Save cynecx/85f0dbc29873ad76ce3b7ce947daf18a to your computer and use it in GitHub Desktop.

Select an option

Save cynecx/85f0dbc29873ad76ce3b7ce947daf18a to your computer and use it in GitHub Desktop.
Tuple Fn
mod private {
pub trait Sealed<A, R> {}
}
pub trait TupleFn<A, R>: private::Sealed<A, R> {
fn as_tuple_fn(self) -> impl FnMut(A) -> R;
}
macro_rules! impl_tuple_fns {
(__emit $($arg:ident),*) => {
impl<F, R, $($arg,)*> TupleFn<($($arg,)*), R> for F
where
F: FnMut($($arg,)*) -> R
{
#[inline]
fn as_tuple_fn(mut self) -> impl FnMut(($($arg,)*)) -> R {
#[allow(non_snake_case)]
move |($($arg,)*)| self($($arg,)*)
}
}
impl<F, R, $($arg,)*> private::Sealed<($($arg,)*), R> for F
where
F: FnMut($($arg,)*) -> R
{}
};
($head:ident, $($tail:ident),+) => {
impl_tuple_fns!($($tail),+);
impl_tuple_fns!(__emit $head, $($tail),+);
};
($single:ident) => {
impl_tuple_fns!(__emit $single);
};
}
impl_tuple_fns!(M, L, K, J, I, H, G, E, D, C, B, A);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment