Create Either{2,7} via macros

This commit is contained in:
Joscha 2023-02-18 20:37:41 +01:00
parent b27cb81642
commit 204540f375
3 changed files with 113 additions and 119 deletions

View file

@ -1,7 +1,7 @@
use async_trait::async_trait; use async_trait::async_trait;
use crate::widgets::{ 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}; use crate::{Frame, Size};
@ -39,12 +39,12 @@ pub trait WidgetExt: Sized {
Border::new(self) Border::new(self)
} }
fn first<W>(self) -> Either<Self, W> { fn first2<W2>(self) -> Either2<Self, W2> {
Either::First(self) Either2::First(self)
} }
fn second<W>(self) -> Either<W, Self> { fn second2<W1>(self) -> Either2<W1, Self> {
Either::Second(self) Either2::Second(self)
} }
fn first3<W2, W3>(self) -> Either3<Self, W2, W3> { fn first3<W2, W3>(self) -> Either3<Self, W2, W3> {

View file

@ -2,16 +2,20 @@ use async_trait::async_trait;
use crate::{AsyncWidget, Frame, Size, Widget}; use crate::{AsyncWidget, Frame, Size, Widget};
macro_rules! mk_either {
(
pub enum $name:ident {
$( $constr:ident($ty:ident), )+
}
) => {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Either<I1, I2> { pub enum $name< $( $ty ),+ > {
First(I1), $( $constr($ty), )+
Second(I2),
} }
impl<E, I1, I2> Widget<E> for Either<I1, I2> impl<E, $( $ty ),+> Widget<E> for $name< $( $ty ),+ >
where where
I1: Widget<E>, $( $ty: Widget<E>, )+
I2: Widget<E>,
{ {
fn size( fn size(
&self, &self,
@ -20,24 +24,21 @@ where
max_height: Option<u16>, max_height: Option<u16>,
) -> Result<Size, E> { ) -> Result<Size, E> {
match self { match self {
Self::First(w) => w.size(frame, max_width, max_height), $( Self::$constr(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> { fn draw(self, frame: &mut Frame) -> Result<(), E> {
match self { match self {
Self::First(w) => w.draw(frame), $( Self::$constr(w) => w.draw(frame), )+
Self::Second(w) => w.draw(frame),
} }
} }
} }
#[async_trait] #[async_trait]
impl<E, I1, I2> AsyncWidget<E> for Either<I1, I2> impl<E, $( $ty ),+> AsyncWidget<E> for $name< $( $ty ),+ >
where where
I1: AsyncWidget<E> + Send + Sync, $( $ty: AsyncWidget<E> + Send + Sync, )+
I2: AsyncWidget<E> + Send + Sync,
{ {
async fn size( async fn size(
&self, &self,
@ -46,79 +47,72 @@ where
max_height: Option<u16>, max_height: Option<u16>,
) -> Result<Size, E> { ) -> Result<Size, E> {
match self { match self {
Self::First(w) => w.size(frame, max_width, max_height).await, $( Self::$constr(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> { async fn draw(self, frame: &mut Frame) -> Result<(), E> {
match self { match self {
Self::First(w) => w.draw(frame).await, $( Self::$constr(w) => w.draw(frame).await, )+
Self::Second(w) => w.draw(frame).await,
} }
} }
} }
};
}
mk_either! {
pub enum Either2 {
First(I1),
Second(I2),
}
}
#[derive(Debug, Clone, Copy)] mk_either! {
pub enum Either3<I1, I2, I3> { pub enum Either3 {
First(I1), First(I1),
Second(I2), Second(I2),
Third(I3), Third(I3),
} }
}
impl<E, I1, I2, I3> Widget<E> for Either3<I1, I2, I3> mk_either! {
where pub enum Either4 {
I1: Widget<E>, First(I1),
I2: Widget<E>, Second(I2),
I3: Widget<E>, Third(I3),
{ Fourth(I4),
fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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> { mk_either! {
match self { pub enum Either5 {
Self::First(w) => w.draw(frame), First(I1),
Self::Second(w) => w.draw(frame), Second(I2),
Self::Third(w) => w.draw(frame), Third(I3),
} Fourth(I4),
} Fifth(I5),
}
#[async_trait]
impl<E, I1, I2, I3> AsyncWidget<E> for Either3<I1, I2, I3>
where
I1: AsyncWidget<E> + Send + Sync,
I2: AsyncWidget<E> + Send + Sync,
I3: AsyncWidget<E> + Send + Sync,
{
async fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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> { mk_either! {
match self { pub enum Either6 {
Self::First(w) => w.draw(frame).await, First(I1),
Self::Second(w) => w.draw(frame).await, Second(I2),
Self::Third(w) => w.draw(frame).await, 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),
}
} }

View file

@ -4,7 +4,7 @@ use async_trait::async_trait;
use crate::{AsyncWidget, Frame, Pos, Size, Widget}; 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: // The following algorithm has three goals, listed in order of importance:
// //
@ -511,17 +511,17 @@ where
} }
} }
pub struct JoinH2<I1, I2>(JoinH<Either<I1, I2>>); pub struct JoinH2<I1, I2>(JoinH<Either2<I1, I2>>);
impl<I1, I2> JoinH2<I1, I2> { impl<I1, I2> JoinH2<I1, I2> {
pub fn new(left: JoinSegment<I1>, right: JoinSegment<I2>) -> Self { pub fn new(left: JoinSegment<I1>, right: JoinSegment<I2>) -> Self {
Self(JoinH::new(vec![ Self(JoinH::new(vec![
JoinSegment { JoinSegment {
inner: Either::First(left.inner), inner: Either2::First(left.inner),
weight: left.weight, weight: left.weight,
}, },
JoinSegment { JoinSegment {
inner: Either::Second(right.inner), inner: Either2::Second(right.inner),
weight: right.weight, weight: right.weight,
}, },
])) ]))
@ -629,17 +629,17 @@ where
} }
} }
pub struct JoinV2<I1, I2>(JoinV<Either<I1, I2>>); pub struct JoinV2<I1, I2>(JoinV<Either2<I1, I2>>);
impl<I1, I2> JoinV2<I1, I2> { impl<I1, I2> JoinV2<I1, I2> {
pub fn new(top: JoinSegment<I1>, bottom: JoinSegment<I2>) -> Self { pub fn new(top: JoinSegment<I1>, bottom: JoinSegment<I2>) -> Self {
Self(JoinV::new(vec![ Self(JoinV::new(vec![
JoinSegment { JoinSegment {
inner: Either::First(top.inner), inner: Either2::First(top.inner),
weight: top.weight, weight: top.weight,
}, },
JoinSegment { JoinSegment {
inner: Either::Second(bottom.inner), inner: Either2::Second(bottom.inner),
weight: bottom.weight, weight: bottom.weight,
}, },
])) ]))