diff --git a/src/widget.rs b/src/widget.rs index d2ae61e..5d3faa4 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; use crate::widgets::{ - Background, Border, Either, Either3, Float, JoinSegment, Layer, Padding, Resize, + Background, Border, Either2, Either3, Float, JoinSegment, Layer, Padding, Resize, }; use crate::{Frame, Size}; @@ -39,12 +39,12 @@ pub trait WidgetExt: Sized { Border::new(self) } - fn first(self) -> Either { - Either::First(self) + fn first2(self) -> Either2 { + Either2::First(self) } - fn second(self) -> Either { - Either::Second(self) + fn second2(self) -> Either2 { + Either2::Second(self) } fn first3(self) -> Either3 { diff --git a/src/widgets/either.rs b/src/widgets/either.rs index 8c4f79a..ea74da4 100644 --- a/src/widgets/either.rs +++ b/src/widgets/either.rs @@ -2,123 +2,117 @@ use async_trait::async_trait; use crate::{AsyncWidget, Frame, Size, Widget}; -#[derive(Debug, Clone, Copy)] -pub enum Either { - First(I1), - Second(I2), +macro_rules! mk_either { + ( + pub enum $name:ident { + $( $constr:ident($ty:ident), )+ + } + ) => { + #[derive(Debug, Clone, Copy)] + pub enum $name< $( $ty ),+ > { + $( $constr($ty), )+ + } + + impl Widget for $name< $( $ty ),+ > + where + $( $ty: Widget, )+ + { + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + match self { + $( Self::$constr(w) => w.size(frame, max_width, max_height), )+ + } + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + match self { + $( Self::$constr(w) => w.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 { + match self { + $( Self::$constr(w) => w.size(frame, max_width, max_height).await, )+ + } + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + match self { + $( Self::$constr(w) => w.draw(frame).await, )+ + } + } + } + }; } -impl Widget for Either -where - I1: Widget, - I2: Widget, -{ - fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - match self { - Self::First(w) => w.size(frame, max_width, max_height), - Self::Second(w) => w.size(frame, max_width, max_height), - } - } - - fn draw(self, frame: &mut Frame) -> Result<(), E> { - match self { - Self::First(w) => w.draw(frame), - Self::Second(w) => w.draw(frame), - } +mk_either! { + pub enum Either2 { + First(I1), + Second(I2), } } -#[async_trait] -impl AsyncWidget for Either -where - I1: AsyncWidget + Send + Sync, - I2: AsyncWidget + Send + Sync, -{ - async fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - match self { - Self::First(w) => w.size(frame, max_width, max_height).await, - Self::Second(w) => w.size(frame, max_width, max_height).await, - } - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - match self { - Self::First(w) => w.draw(frame).await, - Self::Second(w) => w.draw(frame).await, - } +mk_either! { + pub enum Either3 { + First(I1), + Second(I2), + Third(I3), } } -#[derive(Debug, Clone, Copy)] -pub enum Either3 { - First(I1), - Second(I2), - Third(I3), -} - -impl Widget for Either3 -where - I1: Widget, - I2: Widget, - I3: Widget, -{ - fn size( - &self, - frame: &mut Frame, - max_width: Option, - max_height: Option, - ) -> Result { - match self { - Self::First(w) => w.size(frame, max_width, max_height), - Self::Second(w) => w.size(frame, max_width, max_height), - Self::Third(w) => w.size(frame, max_width, max_height), - } - } - - fn draw(self, frame: &mut Frame) -> Result<(), E> { - match self { - Self::First(w) => w.draw(frame), - Self::Second(w) => w.draw(frame), - Self::Third(w) => w.draw(frame), - } +mk_either! { + pub enum Either4 { + First(I1), + Second(I2), + Third(I3), + Fourth(I4), } } -#[async_trait] -impl AsyncWidget for Either3 -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 { - match self { - Self::First(w) => w.size(frame, max_width, max_height).await, - Self::Second(w) => w.size(frame, max_width, max_height).await, - Self::Third(w) => w.size(frame, max_width, max_height).await, - } - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - match self { - Self::First(w) => w.draw(frame).await, - Self::Second(w) => w.draw(frame).await, - Self::Third(w) => w.draw(frame).await, - } +mk_either! { + pub enum Either5 { + First(I1), + Second(I2), + Third(I3), + Fourth(I4), + Fifth(I5), + } +} + +mk_either! { + pub enum Either6 { + First(I1), + Second(I2), + Third(I3), + Fourth(I4), + Fifth(I5), + Sixth(I6), + } +} + +mk_either! { + pub enum Either7 { + First(I1), + Second(I2), + Third(I3), + Fourth(I4), + Fifth(I5), + Sixth(I6), + Seventh(I7), } } diff --git a/src/widgets/join.rs b/src/widgets/join.rs index 9b3d397..68ffd73 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::{Either, Either3}; +use super::{Either2, Either3}; // The following algorithm has three goals, listed in order of importance: // @@ -511,17 +511,17 @@ where } } -pub struct JoinH2(JoinH>); +pub struct JoinH2(JoinH>); impl JoinH2 { pub fn new(left: JoinSegment, right: JoinSegment) -> Self { Self(JoinH::new(vec![ JoinSegment { - inner: Either::First(left.inner), + inner: Either2::First(left.inner), weight: left.weight, }, JoinSegment { - inner: Either::Second(right.inner), + inner: Either2::Second(right.inner), weight: right.weight, }, ])) @@ -629,17 +629,17 @@ where } } -pub struct JoinV2(JoinV>); +pub struct JoinV2(JoinV>); impl JoinV2 { pub fn new(top: JoinSegment, bottom: JoinSegment) -> Self { Self(JoinV::new(vec![ JoinSegment { - inner: Either::First(top.inner), + inner: Either2::First(top.inner), weight: top.weight, }, JoinSegment { - inner: Either::Second(bottom.inner), + inner: Either2::Second(bottom.inner), weight: bottom.weight, }, ]))