Create Either{2,7} via macros
This commit is contained in:
parent
b27cb81642
commit
204540f375
3 changed files with 113 additions and 119 deletions
|
|
@ -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> {
|
||||||
|
|
|
||||||
|
|
@ -2,123 +2,117 @@ use async_trait::async_trait;
|
||||||
|
|
||||||
use crate::{AsyncWidget, Frame, Size, Widget};
|
use crate::{AsyncWidget, Frame, Size, Widget};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
macro_rules! mk_either {
|
||||||
pub enum Either<I1, I2> {
|
(
|
||||||
First(I1),
|
pub enum $name:ident {
|
||||||
Second(I2),
|
$( $constr:ident($ty:ident), )+
|
||||||
|
}
|
||||||
|
) => {
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum $name< $( $ty ),+ > {
|
||||||
|
$( $constr($ty), )+
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E, $( $ty ),+> Widget<E> for $name< $( $ty ),+ >
|
||||||
|
where
|
||||||
|
$( $ty: Widget<E>, )+
|
||||||
|
{
|
||||||
|
fn size(
|
||||||
|
&self,
|
||||||
|
frame: &mut Frame,
|
||||||
|
max_width: Option<u16>,
|
||||||
|
max_height: Option<u16>,
|
||||||
|
) -> Result<Size, E> {
|
||||||
|
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<E, $( $ty ),+> AsyncWidget<E> for $name< $( $ty ),+ >
|
||||||
|
where
|
||||||
|
$( $ty: 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::$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<E, I1, I2> Widget<E> for Either<I1, I2>
|
mk_either! {
|
||||||
where
|
pub enum Either2 {
|
||||||
I1: Widget<E>,
|
First(I1),
|
||||||
I2: Widget<E>,
|
Second(I2),
|
||||||
{
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(self, frame: &mut Frame) -> Result<(), E> {
|
|
||||||
match self {
|
|
||||||
Self::First(w) => w.draw(frame),
|
|
||||||
Self::Second(w) => w.draw(frame),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
mk_either! {
|
||||||
impl<E, I1, I2> AsyncWidget<E> for Either<I1, I2>
|
pub enum Either3 {
|
||||||
where
|
First(I1),
|
||||||
I1: AsyncWidget<E> + Send + Sync,
|
Second(I2),
|
||||||
I2: AsyncWidget<E> + Send + Sync,
|
Third(I3),
|
||||||
{
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
mk_either! {
|
||||||
pub enum Either3<I1, I2, I3> {
|
pub enum Either4 {
|
||||||
First(I1),
|
First(I1),
|
||||||
Second(I2),
|
Second(I2),
|
||||||
Third(I3),
|
Third(I3),
|
||||||
}
|
Fourth(I4),
|
||||||
|
|
||||||
impl<E, I1, I2, I3> Widget<E> for Either3<I1, I2, I3>
|
|
||||||
where
|
|
||||||
I1: Widget<E>,
|
|
||||||
I2: Widget<E>,
|
|
||||||
I3: Widget<E>,
|
|
||||||
{
|
|
||||||
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> {
|
|
||||||
match self {
|
|
||||||
Self::First(w) => w.draw(frame),
|
|
||||||
Self::Second(w) => w.draw(frame),
|
|
||||||
Self::Third(w) => w.draw(frame),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
mk_either! {
|
||||||
impl<E, I1, I2, I3> AsyncWidget<E> for Either3<I1, I2, I3>
|
pub enum Either5 {
|
||||||
where
|
First(I1),
|
||||||
I1: AsyncWidget<E> + Send + Sync,
|
Second(I2),
|
||||||
I2: AsyncWidget<E> + Send + Sync,
|
Third(I3),
|
||||||
I3: AsyncWidget<E> + Send + Sync,
|
Fourth(I4),
|
||||||
{
|
Fifth(I5),
|
||||||
async fn size(
|
}
|
||||||
&self,
|
}
|
||||||
frame: &mut Frame,
|
|
||||||
max_width: Option<u16>,
|
mk_either! {
|
||||||
max_height: Option<u16>,
|
pub enum Either6 {
|
||||||
) -> Result<Size, E> {
|
First(I1),
|
||||||
match self {
|
Second(I2),
|
||||||
Self::First(w) => w.size(frame, max_width, max_height).await,
|
Third(I3),
|
||||||
Self::Second(w) => w.size(frame, max_width, max_height).await,
|
Fourth(I4),
|
||||||
Self::Third(w) => w.size(frame, max_width, max_height).await,
|
Fifth(I5),
|
||||||
}
|
Sixth(I6),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
|
|
||||||
match self {
|
mk_either! {
|
||||||
Self::First(w) => w.draw(frame).await,
|
pub enum Either7 {
|
||||||
Self::Second(w) => w.draw(frame).await,
|
First(I1),
|
||||||
Self::Third(w) => w.draw(frame).await,
|
Second(I2),
|
||||||
}
|
Third(I3),
|
||||||
|
Fourth(I4),
|
||||||
|
Fifth(I5),
|
||||||
|
Sixth(I6),
|
||||||
|
Seventh(I7),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
},
|
},
|
||||||
]))
|
]))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue