Created
May 27, 2024 02:00
-
-
Save cynecx/85f0dbc29873ad76ce3b7ce947daf18a to your computer and use it in GitHub Desktop.
Tuple Fn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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