#[cfg(feature = "general_renderer")] use serde::Serialize; use crate::error::ChainProcessError; pub type ChainProcess = Result; #[derive(Debug)] pub struct AnyOutput { inner: Box, pub type_id: std::any::TypeId, } impl AnyOutput { #[cfg(feature = "general_renderer")] pub fn new(value: T) -> Self where T: Send + Serialize + 'static, { Self { inner: Box::new(value), type_id: std::any::TypeId::of::(), } } #[cfg(not(feature = "general_renderer"))] pub fn new(value: T) -> Self where T: Send + 'static, { Self { inner: Box::new(value), type_id: std::any::TypeId::of::(), } } pub fn downcast(self) -> Result { if self.type_id == std::any::TypeId::of::() { Ok(*self.inner.downcast::().unwrap()) } else { Err(self) } } pub fn is(&self) -> bool { self.type_id == std::any::TypeId::of::() } /// Route the output to the next Chain pub fn route_chain(self) -> ChainProcess { Ok(self) } /// Route the output to the Renderer, ending execution pub fn route_renderer(self) -> ChainProcess { Err(ChainProcessError::Broken(self)) } } impl std::ops::Deref for AnyOutput { type Target = dyn std::any::Any + Send + 'static; fn deref(&self) -> &Self::Target { &*self.inner } } impl std::ops::DerefMut for AnyOutput { fn deref_mut(&mut self) -> &mut Self::Target { &mut *self.inner } }