Create Join[HV]{2,7} via macros

This commit is contained in:
Joscha 2023-02-18 20:56:36 +01:00
parent 204540f375
commit bdc1549268

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::{Either2, Either3}; use super::{Either2, Either3, Either4, Either5, Either6, Either7};
// The following algorithm has three goals, listed in order of importance: // The following algorithm has three goals, listed in order of importance:
// //
@ -511,28 +511,29 @@ where
} }
} }
pub struct JoinH2<I1, I2>(JoinH<Either2<I1, I2>>); macro_rules! mk_join {
(
impl<I1, I2> JoinH2<I1, I2> { $name:ident: $base:ident + $either:ident {
pub fn new(left: JoinSegment<I1>, right: JoinSegment<I2>) -> Self { $( $arg:ident: $constr:ident ($ty:ident), )+
Self(JoinH::new(vec![
JoinSegment {
inner: Either2::First(left.inner),
weight: left.weight,
},
JoinSegment {
inner: Either2::Second(right.inner),
weight: right.weight,
},
]))
} }
} ) => {
pub struct $name< $( $ty ),+ >($base<$either< $( $ty ),+ >>);
impl<E, I1, I2> Widget<E> for JoinH2<I1, I2> impl< $( $ty ),+ > $name< $( $ty ),+ > {
where pub fn new( $( $arg: JoinSegment<$ty> ),+ ) -> Self {
I1: Widget<E>, Self($base::new(vec![ $(
I2: Widget<E>, JoinSegment {
{ inner: $either::$constr($arg.inner),
weight: $arg.weight,
},
)+ ]))
}
}
impl<E, $( $ty ),+ > Widget<E> for $name< $( $ty ),+ >
where
$( $ty: Widget<E>, )+
{
fn size( fn size(
&self, &self,
frame: &mut Frame, frame: &mut Frame,
@ -545,14 +546,13 @@ where
fn draw(self, frame: &mut Frame) -> Result<(), E> { fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.draw(frame) self.0.draw(frame)
} }
} }
#[async_trait] #[async_trait]
impl<E, I1, I2> AsyncWidget<E> for JoinH2<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,
frame: &mut Frame, frame: &mut Frame,
@ -565,184 +565,120 @@ where
async fn draw(self, frame: &mut Frame) -> Result<(), E> { async fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.draw(frame).await self.0.draw(frame).await
} }
}
};
} }
pub struct JoinH3<I1, I2, I3>(JoinH<Either3<I1, I2, I3>>); mk_join! {
JoinH2: JoinH + Either2 {
impl<I1, I2, I3> JoinH3<I1, I2, I3> { first: First(I1),
pub fn new(left: JoinSegment<I1>, middle: JoinSegment<I2>, right: JoinSegment<I3>) -> Self { second: Second(I2),
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,
},
]))
} }
} }
impl<E, I1, I2, I3> Widget<E> for JoinH3<I1, I2, I3> mk_join! {
where JoinH3: JoinH + Either3 {
I1: Widget<E>, first: First(I1),
I2: Widget<E>, second: Second(I2),
I3: Widget<E>, third: Third(I3),
{
fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.0.size(frame, max_width, max_height)
}
fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.draw(frame)
} }
} }
#[async_trait] mk_join! {
impl<E, I1, I2, I3> AsyncWidget<E> for JoinH3<I1, I2, I3> JoinH4: JoinH + Either4 {
where first: First(I1),
I1: AsyncWidget<E> + Send + Sync, second: Second(I2),
I2: AsyncWidget<E> + Send + Sync, third: Third(I3),
I3: AsyncWidget<E> + Send + Sync, fourth: Fourth(I4),
{
async fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.0.size(frame, max_width, max_height).await
}
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.draw(frame).await
} }
} }
pub struct JoinV2<I1, I2>(JoinV<Either2<I1, I2>>); mk_join! {
JoinH5: JoinH + Either5 {
impl<I1, I2> JoinV2<I1, I2> { first: First(I1),
pub fn new(top: JoinSegment<I1>, bottom: JoinSegment<I2>) -> Self { second: Second(I2),
Self(JoinV::new(vec![ third: Third(I3),
JoinSegment { fourth: Fourth(I4),
inner: Either2::First(top.inner), fifth: Fifth(I5),
weight: top.weight,
},
JoinSegment {
inner: Either2::Second(bottom.inner),
weight: bottom.weight,
},
]))
} }
} }
impl<E, I1, I2> Widget<E> for JoinV2<I1, I2> mk_join! {
where JoinH6: JoinH + Either6 {
I1: Widget<E>, first: First(I1),
I2: Widget<E>, second: Second(I2),
{ third: Third(I3),
fn size( fourth: Fourth(I4),
&self, fifth: Fifth(I5),
frame: &mut Frame, sixth: Sixth(I6),
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.0.size(frame, max_width, max_height)
}
fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.draw(frame)
} }
} }
#[async_trait] mk_join! {
impl<E, I1, I2> AsyncWidget<E> for JoinV2<I1, I2> JoinH7: JoinH + Either7 {
where first: First(I1),
I1: AsyncWidget<E> + Send + Sync, second: Second(I2),
I2: AsyncWidget<E> + Send + Sync, third: Third(I3),
{ fourth: Fourth(I4),
async fn size( fifth: Fifth(I5),
&self, sixth: Sixth(I6),
frame: &mut Frame, seventh: Seventh(I7),
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.0.size(frame, max_width, max_height).await
}
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.draw(frame).await
} }
} }
pub struct JoinV3<I1, I2, I3>(JoinV<Either3<I1, I2, I3>>); mk_join! {
JoinV2: JoinV + Either2 {
impl<I1, I2, I3> JoinV3<I1, I2, I3> { first: First(I1),
pub fn new(top: JoinSegment<I1>, middle: JoinSegment<I2>, bottom: JoinSegment<I3>) -> Self { second: Second(I2),
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,
},
]))
} }
} }
impl<E, I1, I2, I3> Widget<E> for JoinV3<I1, I2, I3> mk_join! {
where JoinV3: JoinV + Either3 {
I1: Widget<E>, first: First(I1),
I2: Widget<E>, second: Second(I2),
I3: Widget<E>, third: Third(I3),
{
fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.0.size(frame, max_width, max_height)
}
fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.draw(frame)
} }
} }
#[async_trait] mk_join! {
impl<E, I1, I2, I3> AsyncWidget<E> for JoinV3<I1, I2, I3> JoinV4: JoinV + Either4 {
where first: First(I1),
I1: AsyncWidget<E> + Send + Sync, second: Second(I2),
I2: AsyncWidget<E> + Send + Sync, third: Third(I3),
I3: AsyncWidget<E> + Send + Sync, fourth: Fourth(I4),
{ }
async fn size( }
&self,
frame: &mut Frame, mk_join! {
max_width: Option<u16>, JoinV5: JoinV + Either5 {
max_height: Option<u16>, first: First(I1),
) -> Result<Size, E> { second: Second(I2),
self.0.size(frame, max_width, max_height).await third: Third(I3),
} fourth: Fourth(I4),
fifth: Fifth(I5),
async fn draw(self, frame: &mut Frame) -> Result<(), E> { }
self.0.draw(frame).await }
mk_join! {
JoinV6: JoinV + Either6 {
first: First(I1),
second: Second(I2),
third: Third(I3),
fourth: Fourth(I4),
fifth: Fifth(I5),
sixth: Sixth(I6),
}
}
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),
} }
} }