exec working up to halt
clean shutdown doesn't for some reason
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
use std::borrow::Cow;
|
||||
use std::marker::PhantomData;
|
||||
use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
|
||||
use futures::channel::mpsc::{Receiver, Sender, channel};
|
||||
use futures::future::Fuse;
|
||||
use futures::channel::mpsc::{Sender, channel};
|
||||
use futures::lock::Mutex;
|
||||
use futures::{FutureExt, SinkExt, StreamExt, select};
|
||||
use futures::stream::{self, LocalBoxStream};
|
||||
use futures::{FutureExt, SinkExt, StreamExt};
|
||||
use never::Never;
|
||||
use orchid_base::error::OrcRes;
|
||||
use orchid_base::format::{FmtCtx, FmtUnit};
|
||||
|
||||
use crate::atom::Atomic;
|
||||
use crate::atom_owned::{OwnedAtom, OwnedVariant};
|
||||
@@ -19,35 +19,30 @@ use crate::gen_expr::{GExpr, arg, call, lambda, seq};
|
||||
enum Command {
|
||||
Execute(GExpr, Sender<Expr>),
|
||||
Register(GExpr, Sender<Expr>),
|
||||
Halt(GExpr),
|
||||
}
|
||||
|
||||
struct BuilderCoroutineData {
|
||||
work: Fuse<Pin<Box<dyn Future<Output = GExpr>>>>,
|
||||
cmd_recv: Receiver<Command>,
|
||||
name: Option<String>,
|
||||
receiver: Mutex<LocalBoxStream<'static, Command>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct BuilderCoroutine(Rc<Mutex<BuilderCoroutineData>>);
|
||||
struct BuilderCoroutine(Rc<BuilderCoroutineData>);
|
||||
impl BuilderCoroutine {
|
||||
pub async fn run(self) -> GExpr {
|
||||
let cmd = {
|
||||
let this = &mut *self.0.lock().await;
|
||||
select! {
|
||||
ret_val = &mut this.work => { return ret_val },
|
||||
cmd_res = this.cmd_recv.next().fuse() => match cmd_res {
|
||||
Some(cmd) => cmd,
|
||||
None => return (&mut this.work).await
|
||||
},
|
||||
}
|
||||
};
|
||||
let cmd = self.0.receiver.lock().await.next().await;
|
||||
match cmd {
|
||||
Command::Execute(expr, reply) => call([
|
||||
None => panic!("Before the stream ends, we should have gotten a Halt"),
|
||||
Some(Command::Halt(expr)) => expr,
|
||||
Some(Command::Execute(expr, reply)) => call([
|
||||
lambda(0, [seq([
|
||||
arg(0),
|
||||
call([Replier { reply, builder: self }.to_expr().await, arg(0)]),
|
||||
])]),
|
||||
expr,
|
||||
]),
|
||||
Command::Register(expr, reply) =>
|
||||
Some(Command::Register(expr, reply)) =>
|
||||
call([Replier { reply, builder: self }.to_expr().await, expr]),
|
||||
}
|
||||
}
|
||||
@@ -66,16 +61,29 @@ impl OwnedAtom for Replier {
|
||||
type Refs = Never;
|
||||
async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(()) }
|
||||
async fn call(mut self, arg: Expr) -> GExpr {
|
||||
let _ = self.reply.send(arg).await;
|
||||
self.reply.send(arg).await.expect("What the heck");
|
||||
std::mem::drop(self.reply);
|
||||
self.builder.run().await
|
||||
}
|
||||
async fn print_atom<'a>(&'a self, _: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit {
|
||||
match &self.builder.0.name {
|
||||
None => "BuilderCoroutine".into(),
|
||||
Some(name) => format!("BuilderCoroutine({name})").into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn exec<R: ToExpr>(f: impl for<'a> AsyncFnOnce(ExecHandle<'a>) -> R + 'static) -> GExpr {
|
||||
pub async fn exec<R: ToExpr>(
|
||||
debug: impl AsRef<str>,
|
||||
f: impl for<'a> AsyncFnOnce(ExecHandle<'a>) -> R + 'static,
|
||||
) -> GExpr {
|
||||
let (cmd_snd, cmd_recv) = channel(0);
|
||||
let work =
|
||||
async { f(ExecHandle(cmd_snd, PhantomData)).await.to_expr().await }.boxed_local().fuse();
|
||||
let coro = BuilderCoroutine(Rc::new(Mutex::new(BuilderCoroutineData { cmd_recv, work })));
|
||||
let halt = async { Command::Halt(f(ExecHandle(cmd_snd, PhantomData)).await.to_expr().await) }
|
||||
.into_stream();
|
||||
let coro = BuilderCoroutine(Rc::new(BuilderCoroutineData {
|
||||
name: Some(debug.as_ref().to_string()),
|
||||
receiver: Mutex::new(stream::select(halt, cmd_recv).boxed_local()),
|
||||
}));
|
||||
coro.run().await
|
||||
}
|
||||
|
||||
@@ -84,12 +92,12 @@ static WEIRD_DROP_ERR: &str = "Coroutine dropped while we are being polled someh
|
||||
pub struct ExecHandle<'a>(Sender<Command>, PhantomData<&'a ()>);
|
||||
impl ExecHandle<'_> {
|
||||
pub async fn exec<T: TryFromExpr>(&mut self, val: impl ToExpr) -> OrcRes<T> {
|
||||
let (reply_snd, mut reply_recv) = channel(0);
|
||||
let (reply_snd, mut reply_recv) = channel(1);
|
||||
self.0.send(Command::Execute(val.to_expr().await, reply_snd)).await.expect(WEIRD_DROP_ERR);
|
||||
T::try_from_expr(reply_recv.next().await.expect(WEIRD_DROP_ERR)).await
|
||||
}
|
||||
pub async fn register(&mut self, val: impl ToExpr) -> Expr {
|
||||
let (reply_snd, mut reply_recv) = channel(0);
|
||||
let (reply_snd, mut reply_recv) = channel(1);
|
||||
self.0.send(Command::Register(val.to_expr().await, reply_snd)).await.expect(WEIRD_DROP_ERR);
|
||||
reply_recv.next().await.expect(WEIRD_DROP_ERR)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user