Files
orchid/orchid-std/src/std/option.rs
Lawrence Bethlenfalvy 32d6237dc5 task_local context over context objects
- interner impls logically separate from API in orchid-base (default host interner still in base for testing)
- error reporting, logging, and a variety of other features passed down via context in extension, not yet in host to maintain library-ish profile, should consider options
- no global spawn mechanic, the host has a spawn function but extensions only get a stash for enqueuing async work in sync callbacks which is then explicitly, manually, and with strict order popped and awaited
- still deadlocks nondeterministically for some ungodly reason
2026-01-01 14:54:29 +00:00

75 lines
2.3 KiB
Rust

use std::borrow::Cow;
use std::pin::Pin;
use futures::AsyncWrite;
use orchid_api_traits::Encode;
use orchid_base::error::mk_errv;
use orchid_base::interner::is;
use orchid_base::sym;
use orchid_extension::atom::{Atomic, ForeignAtom, TAtom};
use orchid_extension::atom_owned::{DeserializeCtx, OwnedAtom, OwnedVariant};
use orchid_extension::conv::{ToExpr, TryFromExpr};
use orchid_extension::expr::{Expr, ExprHandle};
use orchid_extension::gen_expr::{call, sym_ref};
use orchid_extension::tree::{GenMember, cnst, fun, prefix};
use crate::{OrcString, api};
#[derive(Clone)]
pub struct OptAtom(Option<Expr>);
impl Atomic for OptAtom {
type Data = Option<api::ExprTicket>;
type Variant = OwnedVariant;
}
impl OwnedAtom for OptAtom {
type Refs = Vec<Expr>;
async fn val(&self) -> Cow<'_, Self::Data> {
Cow::Owned(self.0.as_ref().map(|ex| ex.handle().ticket()))
}
async fn deserialize(mut ctx: impl DeserializeCtx, refs: Self::Refs) -> Self {
Self(ctx.read::<bool>().await.then(|| refs.into_iter().next().unwrap()))
}
async fn serialize(&self, write: Pin<&mut (impl AsyncWrite + ?Sized)>) -> Self::Refs {
self.0.is_some().encode(write).await.unwrap();
self.0.iter().cloned().collect()
}
}
pub struct OrcOpt<T>(pub Option<T>);
impl<T: TryFromExpr> TryFromExpr for OrcOpt<T> {
async fn try_from_expr(expr: Expr) -> orchid_base::error::OrcRes<Self> {
let atom = TAtom::<OptAtom>::try_from_expr(expr).await?;
match atom.value {
None => Ok(OrcOpt(None)),
Some(tk) => Ok(OrcOpt(Some(
T::try_from_expr(Expr::from_handle(ExprHandle::from_ticket(tk).await)).await?,
))),
}
}
}
impl<T: ToExpr + 'static> ToExpr for OrcOpt<T> {
async fn to_gen(self) -> orchid_extension::gen_expr::GExpr {
if let Some(val) = self.0 {
call(sym_ref(sym!(std::option::some)), [val.to_gen().await])
} else {
sym_ref(sym!(std::option::none))
}
}
}
pub fn gen_option_lib() -> Vec<GenMember> {
prefix("std::option", [
cnst(true, "none", OptAtom(None)),
fun(true, "some", async |ex: Expr| OptAtom(Some(ex))),
fun(true, "expect", async |opt: ForeignAtom, msg: OrcString| {
match OrcOpt::try_from_expr(opt.clone().ex()).await? {
OrcOpt(Some(ex)) => Ok::<Expr, _>(ex),
OrcOpt(None) =>
Err(mk_errv(is("Unwrapped std::option::none").await, msg.get_string().await.as_str(), [
opt.pos(),
])),
}
}),
])
}