use crate::{definitions::*, query::RuntimeQuery};
use pi_async_task::definitions::TaskOwnerId;
use pi_async_task::TaskThread;
use std::{
any::{Any, TypeId},
cell::{Cell, RefCell},
rc::{Rc, Weak},
};
struct ContextFrame {
runtime_id: RuntimeId,
handle_type: TypeId,
handle: Rc<dyn Any>,
local: Option<LocalRecord>,
previous: Option<Rc<ContextFrame>>,
active: Cell<bool>,
}
struct LocalRecord {
owner: TaskOwnerId,
value: Weak<dyn Any>,
valid: Rc<Cell<bool>>,
}
thread_local! {
static CURRENT_RUNTIME:RefCell<Option<Rc<ContextFrame>>>=const { RefCell::new(None) };
static ACTIVE_DRIVE:Cell<bool>=const { Cell::new(false) };
}
pub struct LocalBinding {
frame: Rc<ContextFrame>,
valid: Rc<Cell<bool>>,
}
impl Drop for LocalBinding {
fn drop(&mut self) {
local_binding_drop_inner(self)
}
}
pub fn bind_current<H: RuntimeQuery + Clone + Send + Sync + 'static, L: 'static>(
handle: H,
owner: &TaskThread,
local: &Rc<L>,
) -> pi_result::RawResult<LocalBinding, CurrentRuntimeAccessError> {
context_bind_inner(handle, owner, local)
}
pub fn try_current<H: Clone + 'static>() -> pi_result::RawResult<H, CurrentRuntimeAccessError> {
context_current_inner()
}
pub fn try_with_current<H: 'static, R>(
action: impl FnOnce(&H) -> R,
) -> pi_result::RawResult<R, CurrentRuntimeAccessError> {
context_borrow_inner(action)
}
pub fn with_selected<H: RuntimeQuery + Clone + Send + Sync + 'static, R>(
handle: H,
action: impl FnOnce() -> R,
) -> pi_result::RawResult<R, CurrentRuntimeAccessError> {
context_select_inner(handle, action)
}
pub fn try_with_current_local<H: 'static, L: 'static, R>(
action: impl FnOnce(&H, &L) -> R,
) -> pi_result::RawResult<R, LocalRuntimeAccessError> {
context_local_inner(action)
}
pub fn try_with_local_for<L: 'static, R>(
runtime_id: RuntimeId,
action: impl FnOnce(&L) -> R,
) -> pi_result::RawResult<R, LocalRuntimeAccessError> {
context_local_for_inner(runtime_id, action)
}
pub struct DriveScope {
previous: bool,
local: std::marker::PhantomData<Rc<()>>,
}
impl DriveScope {
pub fn enter() -> Self {
drive_scope_enter_inner()
}
pub fn is_active() -> bool {
drive_scope_active_inner()
}
}
impl Drop for DriveScope {
fn drop(&mut self) {
drive_scope_drop_inner(self)
}
}
struct SelectionGuard {
frame: Rc<ContextFrame>,
}
impl Drop for SelectionGuard {
fn drop(&mut self) {
selection_drop_inner(self)
}
}
include!("record_inner.rs");