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 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<W>(self) -> Either<Self, W> {
|
||||
Either::First(self)
|
||||
fn first2<W2>(self) -> Either2<Self, W2> {
|
||||
Either2::First(self)
|
||||
}
|
||||
|
||||
fn second<W>(self) -> Either<W, Self> {
|
||||
Either::Second(self)
|
||||
fn second2<W1>(self) -> Either2<W1, Self> {
|
||||
Either2::Second(self)
|
||||
}
|
||||
|
||||
fn first3<W2, W3>(self) -> Either3<Self, W2, W3> {
|
||||
|
|
|
|||
|
|
@ -2,123 +2,117 @@ use async_trait::async_trait;
|
|||
|
||||
use crate::{AsyncWidget, Frame, Size, Widget};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Either<I1, I2> {
|
||||
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<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>
|
||||
where
|
||||
I1: Widget<E>,
|
||||
I2: 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),
|
||||
}
|
||||
}
|
||||
|
||||
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<E, I1, I2> AsyncWidget<E> for Either<I1, I2>
|
||||
where
|
||||
I1: AsyncWidget<E> + Send + Sync,
|
||||
I2: 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,
|
||||
}
|
||||
}
|
||||
|
||||
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<I1, I2, I3> {
|
||||
First(I1),
|
||||
Second(I2),
|
||||
Third(I3),
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
mk_either! {
|
||||
pub enum Either4 {
|
||||
First(I1),
|
||||
Second(I2),
|
||||
Third(I3),
|
||||
Fourth(I4),
|
||||
}
|
||||
}
|
||||
|
||||
#[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> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<I1, I2>(JoinH<Either<I1, I2>>);
|
||||
pub struct JoinH2<I1, I2>(JoinH<Either2<I1, I2>>);
|
||||
|
||||
impl<I1, I2> JoinH2<I1, I2> {
|
||||
pub fn new(left: JoinSegment<I1>, right: JoinSegment<I2>) -> 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<I1, I2>(JoinV<Either<I1, I2>>);
|
||||
pub struct JoinV2<I1, I2>(JoinV<Either2<I1, I2>>);
|
||||
|
||||
impl<I1, I2> JoinV2<I1, I2> {
|
||||
pub fn new(top: JoinSegment<I1>, bottom: JoinSegment<I2>) -> 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,
|
||||
},
|
||||
]))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue