From 1448f31cf4badc97f49f4e0c09d855fd18358963 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 3 May 2021 19:41:08 +0200 Subject: [PATCH] add AnyViewPort --- nested/src/core/port.rs | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/nested/src/core/port.rs b/nested/src/core/port.rs index 5ee9220..ebfe35f 100644 --- a/nested/src/core/port.rs +++ b/nested/src/core/port.rs @@ -158,3 +158,72 @@ where V::Msg: Clone { } */ +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +#[derive(Debug, Clone)] +pub struct AnyViewPort { + view: Arc, + observers: Arc +} + +impl AnyViewPort { + pub fn downcast(self) -> Result, AnyViewPort> { + match ( + self.view.clone().downcast::>>>(), + self.observers.clone().downcast::>>() + ) { + (Ok(view), Ok(observers)) => Ok(ViewPort{view, observers}), + _ => Err(self) + } + } +} + +impl From> for AnyViewPort { + fn from(port: ViewPort) -> Self { + AnyViewPort { + view: port.view as Arc, + observers: port.observers as Arc + } + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +#[derive(Debug, Clone)] +pub struct AnyOuterViewPort(AnyViewPort); + +#[derive(Debug, Clone)] +pub struct AnyInnerViewPort(AnyViewPort); + +impl AnyOuterViewPort { + pub fn downcast(self) -> Result, AnyViewPort> { + Ok(OuterViewPort(self.0.downcast::()?)) + } +} + +impl From> for AnyOuterViewPort { + fn from(port: OuterViewPort) -> Self { + AnyOuterViewPort(AnyViewPort{ + view: port.0.view as Arc, + observers: port.0.observers as Arc + }) + } +} + +impl AnyInnerViewPort { + pub fn downcast(self) -> Result, AnyViewPort> { + Ok(InnerViewPort(self.0.downcast::()?)) + } +} + +impl From> for AnyInnerViewPort { + fn from(port: InnerViewPort) -> Self { + AnyInnerViewPort(AnyViewPort{ + view: port.0.view as Arc, + observers: port.0.observers as Arc + }) + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> +