use anyhow::Error;
use async_trait::async_trait;
use crb_core::{mpsc, watch};
use crb_runtime::{Context, Controller, Failures, Interruptor, ManagedContext, Runtime};
pub trait MorphContext: Context + 'static {
fn morph(&mut self, next: impl Morph<Context = Self>);
fn next_state(&mut self) -> Option<Box<dyn Morph<Context = Self>>>;
}
pub struct MorphSession {
next_state: Option<Box<dyn Morph<Context = Self>>>,
}
impl MorphContext for MorphSession {
fn morph(&mut self, next: impl Morph<Context = Self>) {
self.next_state = Some(Box::new(next));
}
fn next_state(&mut self) -> Option<Box<dyn Morph<Context = Self>>> {
self.next_state.take()
}
}
impl Context for MorphSession {
type Address = ();
fn address(&self) -> &Self::Address {
&()
}
}
pub trait Morph: Send + 'static {
type Context;
}
pub struct MorphRuntime<C> {
context: C,
morphed: Box<dyn Morph<Context = C>>,
}
#[async_trait]
impl<C> Runtime for MorphRuntime<C>
where
C: MorphContext,
{
type Context = C;
fn get_interruptor(&mut self) -> Box<dyn Interruptor> {
todo!()
}
async fn routine(mut self) -> Failures {
loop {
if let Some(morphed) = self.context.next_state() {
self.morphed = morphed;
}
}
todo!()
}
fn context(&self) -> &Self::Context {
&self.context
}
}