forked from Orchid/orchid
temp commit
This commit is contained in:
@@ -3,7 +3,6 @@ use std::num::{NonZero, NonZeroU16};
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::{fmt, ops};
|
||||
|
||||
use async_once_cell::OnceCell;
|
||||
use async_std::sync::RwLock;
|
||||
use hashbrown::HashMap;
|
||||
use orchid_api::SysId;
|
||||
@@ -13,17 +12,17 @@ use orchid_base::interner::Interner;
|
||||
use crate::api;
|
||||
use crate::atom::WeakAtomHand;
|
||||
use crate::expr_store::ExprStore;
|
||||
use crate::parsed::Root;
|
||||
use crate::system::{System, WeakSystem};
|
||||
use crate::tree::WeakRoot;
|
||||
|
||||
pub struct CtxData {
|
||||
pub i: Rc<Interner>,
|
||||
pub i: Interner,
|
||||
pub spawn: Spawner,
|
||||
pub systems: RwLock<HashMap<api::SysId, WeakSystem>>,
|
||||
pub system_id: RefCell<NonZeroU16>,
|
||||
pub owned_atoms: RwLock<HashMap<api::AtomId, WeakAtomHand>>,
|
||||
pub common_exprs: ExprStore,
|
||||
pub root: OnceCell<Weak<Root>>,
|
||||
pub root: RwLock<WeakRoot>,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct Ctx(Rc<CtxData>);
|
||||
@@ -31,30 +30,39 @@ impl ops::Deref for Ctx {
|
||||
type Target = CtxData;
|
||||
fn deref(&self) -> &Self::Target { &self.0 }
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct WeakCtx(Weak<CtxData>);
|
||||
impl WeakCtx {
|
||||
#[must_use]
|
||||
pub fn try_upgrade(&self) -> Option<Ctx> { Some(Ctx(self.0.upgrade()?)) }
|
||||
#[must_use]
|
||||
pub fn upgrade(&self) -> Ctx { self.try_upgrade().expect("Ctx manually kept alive until exit") }
|
||||
}
|
||||
impl Ctx {
|
||||
#[must_use]
|
||||
pub fn new(spawn: Spawner) -> Self {
|
||||
Self(Rc::new(CtxData {
|
||||
spawn,
|
||||
i: Rc::default(),
|
||||
i: Interner::default(),
|
||||
systems: RwLock::default(),
|
||||
system_id: RefCell::new(NonZero::new(1).unwrap()),
|
||||
owned_atoms: RwLock::default(),
|
||||
common_exprs: ExprStore::default(),
|
||||
root: OnceCell::default(),
|
||||
root: RwLock::default(),
|
||||
}))
|
||||
}
|
||||
#[must_use]
|
||||
pub(crate) async fn system_inst(&self, id: api::SysId) -> Option<System> {
|
||||
self.systems.read().await.get(&id).and_then(WeakSystem::upgrade)
|
||||
}
|
||||
#[must_use]
|
||||
pub(crate) fn next_sys_id(&self) -> api::SysId {
|
||||
let mut g = self.system_id.borrow_mut();
|
||||
*g = g.checked_add(1).unwrap_or(NonZeroU16::new(1).unwrap());
|
||||
SysId(*g)
|
||||
}
|
||||
pub async fn set_root(&self, root: Weak<Root>) {
|
||||
assert!(self.root.get().is_none(), "Root already assigned");
|
||||
self.root.get_or_init(async { root }).await;
|
||||
}
|
||||
#[must_use]
|
||||
pub fn downgrade(&self) -> WeakCtx { WeakCtx(Rc::downgrade(&self.0)) }
|
||||
}
|
||||
impl fmt::Debug for Ctx {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
|
||||
Reference in New Issue
Block a user