templater: implement TemplateProperty for tuples up to 4-ary

A method call with arguments will be combined to (self, args...) tuple.
Unary tuple is useless, but is implemented for consistency.

This tuple_impls! macro is based off the serde one as the Rust core one
requires unstable feature.
This commit is contained in:
Yuya Nishihara 2023-02-03 18:53:20 +09:00
parent a4be118981
commit 54de6168f2

View file

@ -200,6 +200,28 @@ impl<C, P: TemplateProperty<C> + ?Sized> TemplateProperty<C> for Box<P> {
}
}
// Implement TemplateProperty for tuples
macro_rules! tuple_impls {
($( ( $($n:tt $T:ident),+ ) )+) => {
$(
impl<C, $($T: TemplateProperty<C>,)+> TemplateProperty<C> for ($($T,)+) {
type Output = ($($T::Output,)+);
fn extract(&self, context: &C) -> Self::Output {
($(self.$n.extract(context),)+)
}
}
)+
}
}
tuple_impls! {
(0 T0)
(0 T0, 1 T1)
(0 T0, 1 T1, 2 T2)
(0 T0, 1 T1, 2 T2, 3 T3)
}
/// Adapter to drop template context.
pub struct Literal<O>(pub O);