pi_async_runtime_core/context/
record.rs1use crate::{definitions::*, query::RuntimeQuery};
2use pi_async_task::definitions::TaskOwnerId;
3use pi_async_task::TaskThread;
4use std::{
5 any::{Any, TypeId},
6 cell::{Cell, RefCell},
7 rc::{Rc, Weak},
8};
9
10struct ContextFrame {
12 runtime_id: RuntimeId,
13 handle_type: TypeId,
14 handle: Rc<dyn Any>,
15 local: Option<LocalRecord>,
16 previous: Option<Rc<ContextFrame>>,
17 active: Cell<bool>,
18}
19struct LocalRecord {
20 owner: TaskOwnerId,
21 value: Weak<dyn Any>,
22 valid: Rc<Cell<bool>>,
23}
24thread_local! {
25 static CURRENT_RUNTIME:RefCell<Option<Rc<ContextFrame>>>=const { RefCell::new(None) };
26 static ACTIVE_DRIVE:Cell<bool>=const { Cell::new(false) };
27}
28
29pub struct LocalBinding {
35 frame: Rc<ContextFrame>,
36 valid: Rc<Cell<bool>>,
37}
38impl Drop for LocalBinding {
39 fn drop(&mut self) {
40 local_binding_drop_inner(self)
41 }
42}
43
44pub fn bind_current<H: RuntimeQuery + Clone + Send + Sync + 'static, L: 'static>(
46 handle: H,
47 owner: &TaskThread,
48 local: &Rc<L>,
49) -> pi_result::RawResult<LocalBinding, CurrentRuntimeAccessError> {
50 context_bind_inner(handle, owner, local)
51}
52pub fn try_current<H: Clone + 'static>() -> pi_result::RawResult<H, CurrentRuntimeAccessError> {
55 context_current_inner()
56}
57pub fn try_with_current<H: 'static, R>(
61 action: impl FnOnce(&H) -> R,
62) -> pi_result::RawResult<R, CurrentRuntimeAccessError> {
63 context_borrow_inner(action)
64}
65pub fn with_selected<H: RuntimeQuery + Clone + Send + Sync + 'static, R>(
69 handle: H,
70 action: impl FnOnce() -> R,
71) -> pi_result::RawResult<R, CurrentRuntimeAccessError> {
72 context_select_inner(handle, action)
73}
74pub fn try_with_current_local<H: 'static, L: 'static, R>(
77 action: impl FnOnce(&H, &L) -> R,
78) -> pi_result::RawResult<R, LocalRuntimeAccessError> {
79 context_local_inner(action)
80}
81
82pub fn try_with_local_for<L: 'static, R>(
85 runtime_id: RuntimeId,
86 action: impl FnOnce(&L) -> R,
87) -> pi_result::RawResult<R, LocalRuntimeAccessError> {
88 context_local_for_inner(runtime_id, action)
89}
90
91pub struct DriveScope {
96 previous: bool,
97 local: std::marker::PhantomData<Rc<()>>,
98}
99impl DriveScope {
100 pub fn enter() -> Self {
102 drive_scope_enter_inner()
103 }
104 pub fn is_active() -> bool {
107 drive_scope_active_inner()
108 }
109}
110impl Drop for DriveScope {
111 fn drop(&mut self) {
112 drive_scope_drop_inner(self)
113 }
114}
115struct SelectionGuard {
116 frame: Rc<ContextFrame>,
117}
118impl Drop for SelectionGuard {
119 fn drop(&mut self) {
120 selection_drop_inner(self)
121 }
122}
123include!("record_inner.rs");