From bdc1549268854b8d031fbd4636b1f6103f6564b4 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 18 Feb 2023 20:56:36 +0100 Subject: [PATCH] Create Join[HV]{2,7} via macros --- src/widgets/join.rs | 336 ++++++++++++++++++-------------------------- 1 file changed, 136 insertions(+), 200 deletions(-) diff --git a/src/widgets/join.rs b/src/widgets/join.rs index 68ffd73..1acbf84 100644 --- a/src/widgets/join.rs +++ b/src/widgets/join.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use crate::{AsyncWidget, Frame, Pos, Size, Widget}; -use super::{Either2, Either3}; +use super::{Either2, Either3, Either4, Either5, Either6, Either7}; // The following algorithm has three goals, listed in order of importance: // @@ -511,238 +511,174 @@ where } } -pub struct JoinH2(JoinH>); +macro_rules! mk_join { + ( + $name:ident: $base:ident + $either:ident { + $( $arg:ident: $constr:ident ($ty:ident), )+ + } + ) => { + pub struct $name< $( $ty ),+ >($base<$either< $( $ty ),+ >>); -impl JoinH2 { - pub fn new(left: JoinSegment, right: JoinSegment) -> Self { - Self(JoinH::new(vec![ - JoinSegment { - inner: Either2::First(left.inner), - weight: left.weight, - }, - JoinSegment { - inner: Either2::Second(right.inner), - weight: right.weight, - }, - ])) + impl< $( $ty ),+ > $name< $( $ty ),+ > { + pub fn new( $( $arg: JoinSegment<$ty> ),+ ) -> Self { + Self($base::new(vec![ $( + JoinSegment { + inner: $either::$constr($arg.inner), + weight: $arg.weight, + }, + )+ ])) + } + } + + impl Widget for $name< $( $ty ),+ > + where + $( $ty: Widget, )+ + { + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame) + } + } + + #[async_trait] + impl AsyncWidget for $name< $( $ty ),+ > + where + $( $ty: AsyncWidget + Send + Sync, )+ + { + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame).await + } + } + }; +} + +mk_join! { + JoinH2: JoinH + Either2 { + first: First(I1), + second: Second(I2), } } -impl Widget for JoinH2 -where - I1: Widget, - I2: Widget, -{ - fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.size(frame, max_width, max_height) - } - - fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.draw(frame) +mk_join! { + JoinH3: JoinH + Either3 { + first: First(I1), + second: Second(I2), + third: Third(I3), } } -#[async_trait] -impl AsyncWidget for JoinH2 -where - I1: AsyncWidget + Send + Sync, - I2: AsyncWidget + Send + Sync, -{ - async fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.size(frame, max_width, max_height).await - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.draw(frame).await +mk_join! { + JoinH4: JoinH + Either4 { + first: First(I1), + second: Second(I2), + third: Third(I3), + fourth: Fourth(I4), } } -pub struct JoinH3(JoinH>); - -impl JoinH3 { - pub fn new(left: JoinSegment, middle: JoinSegment, right: JoinSegment) -> Self { - Self(JoinH::new(vec![ - JoinSegment { - inner: Either3::First(left.inner), - weight: left.weight, - }, - JoinSegment { - inner: Either3::Second(middle.inner), - weight: middle.weight, - }, - JoinSegment { - inner: Either3::Third(right.inner), - weight: right.weight, - }, - ])) +mk_join! { + JoinH5: JoinH + Either5 { + first: First(I1), + second: Second(I2), + third: Third(I3), + fourth: Fourth(I4), + fifth: Fifth(I5), } } -impl Widget for JoinH3 -where - I1: Widget, - I2: Widget, - I3: Widget, -{ - fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.size(frame, max_width, max_height) - } - - fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.draw(frame) +mk_join! { + JoinH6: JoinH + Either6 { + first: First(I1), + second: Second(I2), + third: Third(I3), + fourth: Fourth(I4), + fifth: Fifth(I5), + sixth: Sixth(I6), } } -#[async_trait] -impl AsyncWidget for JoinH3 -where - I1: AsyncWidget + Send + Sync, - I2: AsyncWidget + Send + Sync, - I3: AsyncWidget + Send + Sync, -{ - async fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.size(frame, max_width, max_height).await - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.draw(frame).await +mk_join! { + JoinH7: JoinH + Either7 { + first: First(I1), + second: Second(I2), + third: Third(I3), + fourth: Fourth(I4), + fifth: Fifth(I5), + sixth: Sixth(I6), + seventh: Seventh(I7), } } -pub struct JoinV2(JoinV>); - -impl JoinV2 { - pub fn new(top: JoinSegment, bottom: JoinSegment) -> Self { - Self(JoinV::new(vec![ - JoinSegment { - inner: Either2::First(top.inner), - weight: top.weight, - }, - JoinSegment { - inner: Either2::Second(bottom.inner), - weight: bottom.weight, - }, - ])) +mk_join! { + JoinV2: JoinV + Either2 { + first: First(I1), + second: Second(I2), } } -impl Widget for JoinV2 -where - I1: Widget, - I2: Widget, -{ - fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.size(frame, max_width, max_height) - } - - fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.draw(frame) +mk_join! { + JoinV3: JoinV + Either3 { + first: First(I1), + second: Second(I2), + third: Third(I3), } } -#[async_trait] -impl AsyncWidget for JoinV2 -where - I1: AsyncWidget + Send + Sync, - I2: AsyncWidget + Send + Sync, -{ - async fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.size(frame, max_width, max_height).await - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.draw(frame).await +mk_join! { + JoinV4: JoinV + Either4 { + first: First(I1), + second: Second(I2), + third: Third(I3), + fourth: Fourth(I4), } } -pub struct JoinV3(JoinV>); - -impl JoinV3 { - pub fn new(top: JoinSegment, middle: JoinSegment, bottom: JoinSegment) -> Self { - Self(JoinV::new(vec![ - JoinSegment { - inner: Either3::First(top.inner), - weight: top.weight, - }, - JoinSegment { - inner: Either3::Second(middle.inner), - weight: middle.weight, - }, - JoinSegment { - inner: Either3::Third(bottom.inner), - weight: bottom.weight, - }, - ])) +mk_join! { + JoinV5: JoinV + Either5 { + first: First(I1), + second: Second(I2), + third: Third(I3), + fourth: Fourth(I4), + fifth: Fifth(I5), } } -impl Widget for JoinV3 -where - I1: Widget, - I2: Widget, - I3: Widget, -{ - fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.size(frame, max_width, max_height) - } - - fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.draw(frame) +mk_join! { + JoinV6: JoinV + Either6 { + first: First(I1), + second: Second(I2), + third: Third(I3), + fourth: Fourth(I4), + fifth: Fifth(I5), + sixth: Sixth(I6), } } -#[async_trait] -impl AsyncWidget for JoinV3 -where - I1: AsyncWidget + Send + Sync, - I2: AsyncWidget + Send + Sync, - I3: AsyncWidget + Send + Sync, -{ - async fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - self.0.size(frame, max_width, max_height).await - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.0.draw(frame).await +mk_join! { + JoinV7: JoinV + Either7 { + first: First(I1), + second: Second(I2), + third: Third(I3), + fourth: Fourth(I4), + fifth: Fifth(I5), + sixth: Sixth(I6), + seventh: Seventh(I7), } }