1use std::sync::Arc;
6use std::sync::atomic::{AtomicBool, Ordering};
7
8#[derive(Clone, Debug, Default)]
11pub struct Ctx {
12 cancelled: Arc<AtomicBool>,
13 language: Option<Arc<str>>,
17}
18
19impl Ctx {
20 pub fn new() -> Self {
22 Ctx::default()
23 }
24
25 pub fn cancelled(&self) -> bool {
27 self.cancelled.load(Ordering::Relaxed)
28 }
29
30 pub fn cancel(&self) {
32 self.cancelled.store(true, Ordering::Relaxed);
33 }
34
35 pub fn raw(&self) -> Arc<AtomicBool> {
37 Arc::clone(&self.cancelled)
38 }
39
40 pub fn with_language(&self, lang: &str) -> Ctx {
42 let mut c = self.clone();
43 c.language = Some(Arc::from(lang));
44 c
45 }
46
47 pub fn language(&self) -> Option<&str> {
49 self.language.as_deref()
50 }
51}
52
53#[cfg(feature = "http-stack")]
54impl Ctx {
55 pub async fn cancelled_async(&self) {
57 let raw = self.raw();
58 tokio::task::yield_now().await;
59 while !raw.load(Ordering::Relaxed) {
60 tokio::time::sleep(std::time::Duration::from_millis(50)).await;
61 }
62 }
63}