113 lines
4.0 KiB
Rust
113 lines
4.0 KiB
Rust
use std::any::{Any, TypeId, type_name};
|
|
use std::future::Future;
|
|
use std::pin::Pin;
|
|
|
|
use async_once_cell::OnceCell;
|
|
use futures::future::LocalBoxFuture;
|
|
use futures::{AsyncRead, AsyncWrite, FutureExt};
|
|
use orchid_api_traits::{Coding, enc_vec};
|
|
use orchid_base::error::OrcRes;
|
|
use orchid_base::format::FmtUnit;
|
|
use orchid_base::name::Sym;
|
|
|
|
use crate::api;
|
|
use crate::atom::{
|
|
AtomCard, AtomCtx, AtomDynfo, AtomFactory, Atomic, AtomicFeaturesImpl, AtomicVariant, MethodSet,
|
|
MethodSetBuilder, err_not_callable, err_not_command, get_info,
|
|
};
|
|
use crate::context::ctx;
|
|
use crate::expr::Expr;
|
|
use crate::gen_expr::{GExpr, bot};
|
|
use crate::system_ctor::CtedObj;
|
|
|
|
pub struct ThinVariant;
|
|
impl AtomicVariant for ThinVariant {}
|
|
impl<A: ThinAtom + Atomic<Variant = ThinVariant>> AtomicFeaturesImpl<ThinVariant> for A {
|
|
fn _factory(self) -> AtomFactory {
|
|
AtomFactory::new(async move || {
|
|
let (id, _) = get_info::<A>(ctx().get::<CtedObj>().inst().card());
|
|
let mut buf = enc_vec(&id).await;
|
|
self.encode(Pin::new(&mut buf)).await;
|
|
api::Atom { drop: None, data: api::AtomData(buf), owner: ctx().sys_id() }
|
|
})
|
|
}
|
|
fn _info() -> Self::_Info { ThinAtomDynfo { msbuild: Self::reg_reqs(), ms: OnceCell::new() } }
|
|
type _Info = ThinAtomDynfo<Self>;
|
|
}
|
|
|
|
pub struct ThinAtomDynfo<T: ThinAtom> {
|
|
msbuild: MethodSetBuilder<T>,
|
|
ms: OnceCell<MethodSet<T>>,
|
|
}
|
|
impl<T: ThinAtom> AtomDynfo for ThinAtomDynfo<T> {
|
|
fn print<'a>(&self, AtomCtx(buf, _): AtomCtx<'a>) -> LocalBoxFuture<'a, FmtUnit> {
|
|
Box::pin(async move { T::decode(Pin::new(&mut &buf[..])).await.print().await })
|
|
}
|
|
fn tid(&self) -> TypeId { TypeId::of::<T>() }
|
|
fn name(&self) -> &'static str { type_name::<T>() }
|
|
fn decode<'a>(&'a self, AtomCtx(buf, ..): AtomCtx<'a>) -> LocalBoxFuture<'a, Box<dyn Any>> {
|
|
Box::pin(async { Box::new(T::decode(Pin::new(&mut &buf[..])).await) as Box<dyn Any> })
|
|
}
|
|
fn call<'a>(&'a self, AtomCtx(buf, ..): AtomCtx<'a>, arg: Expr) -> LocalBoxFuture<'a, GExpr> {
|
|
Box::pin(async move { T::decode(Pin::new(&mut &buf[..])).await.call(arg).await })
|
|
}
|
|
fn call_ref<'a>(&'a self, AtomCtx(buf, ..): AtomCtx<'a>, arg: Expr) -> LocalBoxFuture<'a, GExpr> {
|
|
Box::pin(async move { T::decode(Pin::new(&mut &buf[..])).await.call(arg).await })
|
|
}
|
|
fn handle_req<'a, 'm1: 'a, 'm2: 'a>(
|
|
&'a self,
|
|
AtomCtx(buf, _): AtomCtx<'a>,
|
|
key: Sym,
|
|
req: Pin<&'m1 mut dyn AsyncRead>,
|
|
rep: Pin<&'m2 mut dyn AsyncWrite>,
|
|
) -> LocalBoxFuture<'a, bool> {
|
|
Box::pin(async move {
|
|
let ms = self.ms.get_or_init(self.msbuild.pack()).await;
|
|
ms.dispatch(&T::decode(Pin::new(&mut &buf[..])).await, key, req, rep).await
|
|
})
|
|
}
|
|
fn command<'a>(
|
|
&'a self,
|
|
AtomCtx(buf, _): AtomCtx<'a>,
|
|
) -> LocalBoxFuture<'a, OrcRes<Option<GExpr>>> {
|
|
async move { T::decode(Pin::new(&mut &buf[..])).await.command().await }.boxed_local()
|
|
}
|
|
fn serialize<'a, 'b: 'a>(
|
|
&'a self,
|
|
ctx: AtomCtx<'a>,
|
|
write: Pin<&'b mut dyn AsyncWrite>,
|
|
) -> LocalBoxFuture<'a, Option<Vec<Expr>>> {
|
|
Box::pin(async {
|
|
T::decode(Pin::new(&mut &ctx.0[..])).await.encode(write).await;
|
|
Some(Vec::new())
|
|
})
|
|
}
|
|
fn deserialize<'a>(&'a self, data: &'a [u8], refs: &'a [Expr]) -> LocalBoxFuture<'a, api::Atom> {
|
|
assert!(refs.is_empty(), "Refs found when deserializing thin atom");
|
|
Box::pin(async { T::decode(Pin::new(&mut &data[..])).await._factory().build().await })
|
|
}
|
|
fn drop<'a>(&'a self, AtomCtx(buf, _): AtomCtx<'a>) -> LocalBoxFuture<'a, ()> {
|
|
Box::pin(async move {
|
|
let string_self = T::decode(Pin::new(&mut &buf[..])).await.print().await;
|
|
writeln!(ctx().logger(), "Received drop signal for non-drop atom {string_self:?}");
|
|
})
|
|
}
|
|
}
|
|
|
|
pub trait ThinAtom:
|
|
AtomCard<Data = Self> + Atomic<Variant = ThinVariant> + Coding + Send + Sync + 'static
|
|
{
|
|
#[allow(unused_variables)]
|
|
fn call(&self, arg: Expr) -> impl Future<Output = GExpr> {
|
|
async move { bot(err_not_callable().await) }
|
|
}
|
|
#[allow(unused_variables)]
|
|
fn command(&self) -> impl Future<Output = OrcRes<Option<GExpr>>> {
|
|
async move { Err(err_not_command().await) }
|
|
}
|
|
#[allow(unused_variables)]
|
|
fn print(&self) -> impl Future<Output = FmtUnit> {
|
|
async { format!("ThinAtom({})", type_name::<Self>()).into() }
|
|
}
|
|
}
|