elemental_object/
context.rs1use std::sync::Arc;
2
3use tokio::task_local;
4
5use crate::pool::ObjectPool;
6
7#[derive(Clone)]
8pub struct ObjectContext {
9 inner: Arc<ObjectPool>,
10}
11
12task_local! {
13 pub static CONTEXT: Arc<ObjectPool>;
14}
15
16impl ObjectContext {
17 pub fn new() -> Self {
18 Self {
19 inner: Arc::new(ObjectPool::new()),
20 }
21 }
22
23 pub async fn run<Fut>(self, f: Fut) -> Fut::Output
24 where
25 Fut: Future,
26 {
27 CONTEXT.scope(self.inner.clone(), f).await
28 }
29
30 pub async fn shutdown(self) {
31 CONTEXT
32 .scope(self.inner.clone(), async {
33 let pool = CONTEXT.with(|pool| pool.clone());
34 pool.shutdown().await;
35 })
36 .await;
37 }
38}
39
40impl Default for ObjectContext {
41 fn default() -> Self {
42 Self::new()
43 }
44}