partway through fixes, macro system needs resdesign

This commit is contained in:
2026-04-08 18:02:20 +02:00
parent 0909524dee
commit 9b4c7fa7d7
76 changed files with 1391 additions and 1065 deletions

View File

@@ -1,5 +1,4 @@
use std::any::{Any, TypeId, type_name};
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::future::Future;
@@ -19,7 +18,6 @@ use orchid_base::{
FmtCtx, FmtUnit, Format, IStr, OrcErrv, Pos, Receipt, ReqHandle, ReqReader, ReqReaderExt, Sym,
fmt, is, mk_errv, mk_errv_floating, take_first,
};
use task_local::task_local;
use trait_set::trait_set;
use crate::gen_expr::GExpr;
@@ -99,11 +97,11 @@ impl ForeignAtom {
/// Call an IPC method. If the type does not support the given method type,
/// this function returns [None]
pub async fn call<R: Request + UnderRoot<Root: AtomMethod>>(&self, r: R) -> Option<R::Response> {
let rep = (request(api::Fwd(
self.atom.clone(),
Sym::parse(<R as UnderRoot>::Root::NAME).await.unwrap().tok().to_api(),
enc_vec(&r.into_root()),
)))
let rep = (request(api::Fwd {
target: self.atom.clone(),
method: Sym::parse(<R as UnderRoot>::Root::NAME).await.unwrap().tok().to_api(),
body: enc_vec(&r.into_root()),
}))
.await?;
Some(R::Response::decode_slice(&mut &rep[..]))
}
@@ -111,26 +109,22 @@ impl ForeignAtom {
pub fn downcast<A: Atomic>(self) -> Result<TAtom<A>, NotTypAtom> {
let mut data = &self.atom.data.0[..];
let value = AtomTypeId::decode_slice(&mut data);
if cfg!(debug_assertions) {
let cted = dyn_cted();
let own_inst = cted.inst();
let owner_id = self.atom.owner;
let typ = type_name::<A>();
let owner = if sys_id() == owner_id {
own_inst.card()
} else {
(cted.deps().find(|s| s.id() == self.atom.owner))
.ok_or_else(|| NotTypAtom { expr: self.clone().ex(), pos: self.pos(), typ })?
.get_card()
};
let Some(ops) = owner.ops_by_atid(value) else {
panic!("{value:?} does not refer to an atom in {owner_id:?} when downcasting {typ}");
};
if ops.tid() != TypeId::of::<A>() {
panic!(
"{value:?} of {owner_id:?} refers to a type other than {typ}. System version mismatch?"
)
}
let cted = dyn_cted();
let own_inst = cted.inst();
let owner_id = self.atom.owner;
let typ = type_name::<A>();
let owner = if sys_id() == owner_id {
own_inst.card()
} else {
(cted.deps().find(|s| s.id() == self.atom.owner))
.ok_or_else(|| NotTypAtom { expr: self.clone().ex(), pos: self.pos(), typ })?
.get_card()
};
let Some(ops) = owner.ops_by_atid(value) else {
panic!("{value:?} does not refer to an atom in {owner_id:?} when downcasting {typ}");
};
if ops.tid() != TypeId::of::<A>() {
return Err(NotTypAtom { pos: self.pos.clone(), expr: self.ex(), typ });
}
let value = A::Data::decode_slice(&mut data);
Ok(TAtom { value, untyped: self })
@@ -187,10 +181,6 @@ pub trait AtomMethod: Coding + InHierarchy {
const NAME: &str;
}
task_local! {
pub(crate) static ATOM_WITHOUT_HANDLE_FINAL_IMPL: Rc<RefCell<Option<Box<dyn Any>>>>;
}
/// A handler for an [AtomMethod] on an [Atomic]. The [AtomMethod] must also be
/// registered in [Atomic::reg_methods]
pub trait Supports<M: AtomMethod>: Atomic {
@@ -199,19 +189,6 @@ pub trait Supports<M: AtomMethod>: Atomic {
hand: Box<dyn ReqHandle<'a> + '_>,
req: M,
) -> impl Future<Output = io::Result<Receipt<'a>>>;
fn handle_final<'a>(
self,
hand: Box<dyn ReqHandle<'a> + '_>,
req: M,
) -> impl Future<Output = io::Result<Receipt<'a>>> {
async move {
let rcpt = self.handle(hand, req).await;
let _ = ATOM_WITHOUT_HANDLE_FINAL_IMPL.try_with(|cell| cell.replace(Some(Box::new(self))));
rcpt
}
}
// TODO: default-implement the above somehow while calling OwnedAtom::free if
// necessary
}
trait HandleAtomMethod<A> {
@@ -220,11 +197,6 @@ trait HandleAtomMethod<A> {
atom: &'a A,
reader: Box<dyn ReqReader<'b> + 'a>,
) -> LocalBoxFuture<'a, ()>;
fn handle_final<'a, 'b: 'a>(
&'a self,
atom: A,
reader: Box<dyn ReqReader<'b> + 'a>,
) -> LocalBoxFuture<'a, ()>;
}
struct AtomMethodHandler<M, A>(PhantomData<M>, PhantomData<A>);
impl<M: AtomMethod, A: Supports<M>> HandleAtomMethod<A> for AtomMethodHandler<M, A> {
@@ -238,16 +210,6 @@ impl<M: AtomMethod, A: Supports<M>> HandleAtomMethod<A> for AtomMethodHandler<M,
let _ = Supports::<M>::handle(atom, reader.finish().await, req).await.unwrap();
})
}
fn handle_final<'a, 'b: 'a>(
&'a self,
atom: A,
mut reader: Box<dyn ReqReader<'b> + 'a>,
) -> LocalBoxFuture<'a, ()> {
Box::pin(async {
let req = reader.read_req::<M>().await.unwrap();
let _ = Supports::<M>::handle_final(atom, reader.finish().await, req).await.unwrap();
})
}
}
/// A collection of [Supports] impls for an [Atomic]. If a [Supports]
@@ -282,20 +244,6 @@ pub(crate) struct MethodSet<A: Atomic> {
handlers: HashMap<Sym, Rc<dyn HandleAtomMethod<A>>>,
}
impl<A: Atomic> MethodSet<A> {
pub(crate) async fn final_dispatch<'a>(
&self,
atom: A,
key: Sym,
req: Box<dyn ReqReader<'a> + 'a>,
) -> bool {
match self.handlers.get(&key) {
None => false,
Some(handler) => {
handler.handle_final(atom, req).await;
true
},
}
}
pub(crate) async fn dispatch<'a>(
&self,
atom: &'_ A,
@@ -353,11 +301,11 @@ impl<A: Atomic> TAtom<A> {
pub async fn call<R: Request + UnderRoot<Root: AtomMethod>>(&self, req: R) -> R::Response
where A: Supports<<R as UnderRoot>::Root> {
R::Response::decode_slice(
&mut &(request(api::Fwd(
self.untyped.atom.clone(),
Sym::parse(<R as UnderRoot>::Root::NAME).await.unwrap().tok().to_api(),
enc_vec(&req.into_root()),
)))
&mut &(request(api::Fwd {
target: self.untyped.atom.clone(),
method: Sym::parse(<R as UnderRoot>::Root::NAME).await.unwrap().tok().to_api(),
body: enc_vec(&req.into_root()),
}))
.await
.unwrap()[..],
)
@@ -389,12 +337,6 @@ pub trait AtomOps: 'static {
fn call<'a>(&'a self, ctx: AtomCtx<'a>, arg: Expr) -> LocalBoxFuture<'a, GExpr>;
fn call_ref<'a>(&'a self, ctx: AtomCtx<'a>, arg: Expr) -> LocalBoxFuture<'a, GExpr>;
fn print<'a>(&'a self, ctx: AtomCtx<'a>) -> LocalBoxFuture<'a, FmtUnit>;
fn handle_req<'a>(
&'a self,
ctx: AtomCtx<'a>,
key: Sym,
req: Box<dyn ReqReader<'a> + 'a>,
) -> LocalBoxFuture<'a, bool>;
fn handle_req_ref<'a>(
&'a self,
ctx: AtomCtx<'a>,
@@ -417,25 +359,25 @@ pub trait AtomOps: 'static {
trait_set! {
pub trait AtomFactoryFn = FnOnce() -> LocalBoxFuture<'static, api::LocalAtom> + DynClone;
}
pub(crate) struct AtomFactory(Box<dyn AtomFactoryFn>);
pub(crate) struct AtomFactory(Box<dyn AtomFactoryFn>, String);
impl AtomFactory {
pub fn new(f: impl AsyncFnOnce() -> api::LocalAtom + Clone + 'static) -> Self {
Self(Box::new(|| f().boxed_local()))
pub fn new(name: String, f: impl AsyncFnOnce() -> api::LocalAtom + Clone + 'static) -> Self {
Self(Box::new(|| f().boxed_local()), name)
}
pub async fn build(self) -> api::LocalAtom { (self.0)().await }
}
impl Clone for AtomFactory {
fn clone(&self) -> Self { AtomFactory(clone_box(&*self.0)) }
fn clone(&self) -> Self { AtomFactory(clone_box(&*self.0), self.1.clone()) }
}
impl fmt::Debug for AtomFactory {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "AtomFactory") }
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "AtomFactory<{}>", self.1) }
}
impl fmt::Display for AtomFactory {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "AtomFactory") }
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{self:?}") }
}
impl Format for AtomFactory {
async fn print<'a>(&'a self, _c: &'a (impl FmtCtx + ?Sized + 'a)) -> FmtUnit {
"AtomFactory".to_string().into()
self.to_string().into()
}
}